Skip to content

Commit 30ad229

Browse files
author
Peter Pilgrim
committed
Update to code. Is there way to create JMS resource dynamically with a managed Glassfish server? No. The only way is to manually administer GlassFish with the required JMS resources at the command line or use the Admin UI web interface. #3 (Gradle down from 2 to 1 failures. Force the JVM to be invoked per test in build.gradle file)
1 parent 0cf8443 commit 30ad229

5 files changed

Lines changed: 45 additions & 53 deletions

File tree

ch09/jms-async/build.gradle

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,10 @@ sourceSets {
107107
}
108108
}
109109

110+
test {
111+
forkEvery = 1
112+
}
113+
110114
task(run, dependsOn: 'classes', type: JavaExec) {
111115
description = 'Runs the main application'
112116
main = 'je7hb.jms.async.App'

ch09/jms-async/src/test/java/je7hb/jms/async/AsynchronousJMSMessageArquillianTest.java

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ public void onException(Message msg, Exception ex) {
7676
@Test
7777
@RunAsClient
7878
public void shouldFire() throws JMSException, InterruptedException, NamingException {
79-
Properties properties = new Properties();
79+
final Properties properties = new Properties();
8080
// properties.put("com.sun.appserv.iiop.endpoints", "localhost:7676");
8181
// properties.put("org.omg.CORBA.ORBInitialHost", "localhost");
8282
// properties.put("org.omg.CORBA.ORBInitialPort", "3700");
@@ -95,23 +95,21 @@ public void shouldFire() throws JMSException, InterruptedException, NamingExcept
9595

9696
System.out.println("=====================================================================");
9797
System.out.println("=====================================================================");
98-
InitialContext jndiContext = new InitialContext(properties);
98+
final InitialContext jndiContext = new InitialContext(properties);
9999
System.out.printf("\t jndiContext=%s\n", jndiContext);
100100
System.out.println("=====================================================================");
101101
System.out.println("=====================================================================");
102102

103-
ConnectionFactory connectionFactory =
104-
(ConnectionFactory)jndiContext.lookup("jms/demoConnectionFactory");
103+
final ConnectionFactory connectionFactory = (ConnectionFactory)jndiContext.lookup("jms/demoConnectionFactory");
105104

106-
Queue queue = (Queue)jndiContext.lookup("jms/demoQueue");
105+
final Queue queue = (Queue)jndiContext.lookup("jms/demoQueue");
107106

108107
assertNotNull(connectionFactory);
109108
assertNotNull(queue);
110109

111-
Connection connection = connectionFactory.createConnection();
112-
JMSContext context = connectionFactory.createContext(
113-
Session.AUTO_ACKNOWLEDGE );
114-
JMSProducer producer = context.createProducer();
110+
final Connection connection = connectionFactory.createConnection();
111+
final JMSContext context = connectionFactory.createContext(Session.AUTO_ACKNOWLEDGE );
112+
final JMSProducer producer = context.createProducer();
115113

116114
messages.clear();
117115

@@ -121,9 +119,8 @@ public void shouldFire() throws JMSException, InterruptedException, NamingExcept
121119
producer.send(queue, "world");
122120
producer.send(queue, "asynchronously");
123121

124-
Thread.sleep(2000);
122+
Thread.sleep(1774);
125123

126124
System.out.println("Done");
127-
128125
}
129126
}

ch09/jms-async/src/test/java/je7hb/jms/async/AsynchronousJMSMessageTest.java

Lines changed: 21 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import javax.naming.InitialContext;
3232
import java.io.File;
3333
import java.util.ArrayList;
34+
import java.util.Hashtable;
3435
import java.util.List;
3536
import java.util.Properties;
3637

@@ -102,22 +103,7 @@ public void onException(Message msg, Exception ex) {
102103

103104
@Test
104105
public void shouldFireMessageAsynchronously() throws Exception {
105-
106-
// String strLine = "";
107-
// try {
108-
// // Get the object of DataInputStream
109-
// InputStreamReader isr = new InputStreamReader(System.in);
110-
// BufferedReader br = new BufferedReader(isr);
111-
// String line = "";
112-
// while ((line = br.readLine()) != null && !line.equals("exit") )
113-
// strLine += br.readLine() + "~";
114-
//
115-
// isr.close();
116-
// } catch (IOException ioe) {
117-
// ioe.printStackTrace();
118-
// }
119-
120-
Properties properties = new Properties();
106+
// final Properties properties = new Properties();
121107
// properties.put("com.sun.appserv.iiop.endpoints", "localhost:7676");
122108
// properties.put("org.omg.CORBA.ORBInitialHost", "localhost");
123109
// properties.put("org.omg.CORBA.ORBInitialPort", "3700");
@@ -126,30 +112,34 @@ public void shouldFireMessageAsynchronously() throws Exception {
126112
// properties.put("java.naming.factory.url.pkgs", "com.sun.enterprise.naming");
127113
// properties.put("java.naming.factory.state", "com.sun.corba.ee.impl.presentation.rmi.JNDIStateFactoryImpl");
128114

115+
final Hashtable properties = new Hashtable(2);
129116
properties.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.enterprise.naming.SerialInitContextFactory");
130-
properties.put(Context.URL_PKG_PREFIXES, "com.sun.enterprise.naming");
131-
properties.put(Context.STATE_FACTORIES, "com.sun.corba.ee.impl.presentation.rmi.JNDIStateFactoryImpl");
132-
// properties.put("org.omg.CORBA.ORBInitialHost", "localhost");
133-
// properties.put("org.omg.CORBA.ORBInitialPort", "3700");
134-
properties.put(Context.PROVIDER_URL, "mq://localhost:7676"); // vm://localhost:
135-
// properties.put(Context.PROVIDER_URL, "iiop://localhost:7676"); // vm://localhost:
117+
properties.put(Context.PROVIDER_URL, "mq://localhost:7676");
118+
properties.put("java.naming.factory.url.pkgs", "com.sun.enterprise.naming");
119+
properties.put("java.naming.factory.state", "com.sun.corba.ee.impl.presentation.rmi.JNDIStateFactoryImpl");
120+
121+
// properties.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.enterprise.naming.SerialInitContextFactory");
122+
// properties.put(Context.URL_PKG_PREFIXES, "com.sun.enterprise.naming");
123+
// properties.put(Context.STATE_FACTORIES, "com.sun.corba.ee.impl.presentation.rmi.JNDIStateFactoryImpl");
124+
//// properties.put("org.omg.CORBA.ORBInitialHost", "localhost");
125+
//// properties.put("org.omg.CORBA.ORBInitialPort", "3700");
126+
// properties.put(Context.PROVIDER_URL, "mq://localhost:7676"); // vm://localhost:
127+
//// properties.put(Context.PROVIDER_URL, "iiop://localhost:7676"); // vm://localhost:
136128

137129
System.out.println("=====================================================================");
138130
System.out.println("=====================================================================");
139-
InitialContext jndiContext = new InitialContext(properties);
131+
final InitialContext jndiContext = new InitialContext(properties);
140132
System.out.printf("\t jndiContext=%s\n", jndiContext);
141133
System.out.println("=====================================================================");
142134
System.out.println("=====================================================================");
143135

144-
ConnectionFactory connectionFactory =
145-
(ConnectionFactory)jndiContext.lookup("jms/demoConnectionFactory");
136+
final ConnectionFactory connectionFactory = (ConnectionFactory)jndiContext.lookup("java:comp:jms/demoConnectionFactory");
137+
138+
final Queue queue = (Queue)jndiContext.lookup("jms/demoQueue");
146139

147-
Queue queue = (Queue)jndiContext.lookup("jms/demoQueue");
140+
final JMSContext context = connectionFactory.createContext(Session.AUTO_ACKNOWLEDGE );
148141

149-
Connection connection = connectionFactory.createConnection();
150-
JMSContext context = connectionFactory.createContext(
151-
Session.AUTO_ACKNOWLEDGE );
152-
JMSProducer producer = context.createProducer();
142+
final JMSProducer producer = context.createProducer();
153143

154144
messages.clear();
155145

@@ -159,7 +149,7 @@ public void shouldFireMessageAsynchronously() throws Exception {
159149
producer.send(queue, "world");
160150
producer.send(queue, "asynchronously");
161151

162-
Thread.sleep(2000);
152+
Thread.sleep(1000);
163153

164154
System.out.println("Done");
165155
}

ch09/jms-async/src/test/java/je7hb/jms/async/PayloadCheckAsyncTest.java

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public class PayloadCheckAsyncTest {
5050

5151
@Deployment(testable = false)
5252
public static JavaArchive createDeployment() {
53-
JavaArchive jar = ShrinkWrap.create(JavaArchive.class)
53+
final JavaArchive jar = ShrinkWrap.create(JavaArchive.class)
5454
.addClasses(PayloadCheckAsync.class)
5555
// .addAsResource(
5656
// "test-persistence.xml",
@@ -90,22 +90,26 @@ public void onException(Message msg, Exception ex) {
9090
@RunAsClient
9191
public void shouldFireMessageAsynchronously() throws Exception {
9292

93-
Hashtable properties = new Hashtable(2);
93+
System.out.print("Waiting ...");
94+
Thread.sleep(1000);
95+
System.out.println("Ok");
96+
97+
final Hashtable properties = new Hashtable(2);
9498
properties.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.enterprise.naming.SerialInitContextFactory");
9599
properties.put(Context.PROVIDER_URL, "mq://localhost:7676");
96100
properties.put("java.naming.factory.url.pkgs", "com.sun.enterprise.naming");
97101
properties.put("java.naming.factory.state", "com.sun.corba.ee.impl.presentation.rmi.JNDIStateFactoryImpl");
98102

99-
InitialContext jndiContext = new InitialContext(properties);
103+
final InitialContext jndiContext = new InitialContext(properties);
100104

101-
ConnectionFactory connectionFactory =
105+
final ConnectionFactory connectionFactory =
102106
(ConnectionFactory)jndiContext.lookup("jms/demoConnectionFactory");
103107

104-
Queue queue = (Queue)jndiContext.lookup("jms/demoQueue");
108+
final Queue queue = (Queue)jndiContext.lookup("jms/demoQueue");
105109

106-
JMSContext context = connectionFactory.createContext(
110+
final JMSContext context = connectionFactory.createContext(
107111
Session.AUTO_ACKNOWLEDGE );
108-
JMSProducer producer = context.createProducer();
112+
final JMSProducer producer = context.createProducer();
109113

110114
messages.clear();
111115

ch09/jms-async/src/test/java/je7hb/jms/async/PayloadCheckTest.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,8 @@ public class PayloadCheckTest {
4444

4545
@Deployment
4646
public static JavaArchive createDeployment() {
47-
JavaArchive jar = ShrinkWrap.create(JavaArchive.class)
47+
final JavaArchive jar = ShrinkWrap.create(JavaArchive.class)
4848
.addClasses(PayloadCheck.class)
49-
// .addAsResource(
50-
// "test-persistence.xml",
51-
// "META-INF/persistence.xml")
5249
.addAsResource(
5350
new File("src/test/resources-glassfish-managed/glassfish-resources.xml"),
5451
"glassfish-resources.xml")

0 commit comments

Comments
 (0)