View Javadoc
1   package ejava.examples.jms10.jmsmechanics;
2   
3   import static org.junit.Assert.assertNotNull;
4   
5   import javax.jms.Connection;
6   import javax.jms.ConnectionFactory;
7   import javax.jms.Destination;
8   import javax.jms.JMSException;
9   import javax.naming.Context;
10  import javax.naming.InitialContext;
11  import javax.naming.NamingException;
12  
13  import org.junit.AfterClass;
14  import org.junit.BeforeClass;
15  import org.slf4j.Logger;
16  import org.slf4j.LoggerFactory;
17  
18  import ejava.examples.jms10.jmsmechanics.MessageCatcher;
19  
20  
21  public class JMSTestBase {
22  	private static final Logger logger = LoggerFactory.getLogger(JMSTestBase.class);
23      protected static boolean jmsEmbedded = Boolean.parseBoolean( 
24  		System.getProperty("jms.embedded", "true"));
25      protected int msgCount = Integer.parseInt(System.getProperty("multi.message.count", "20"));
26      private static String connFactoryJNDI = 
27  		System.getProperty("jndi.name.connFactory", "jms/RemoteConnectionFactory");
28      protected static String queueJNDI = System.getProperty("jndi.name.testQueue",
29              "jms/queue/ejava/examples/jmsMechanics/queue1");
30      protected static String topicJNDI = System.getProperty("jndi.name.testTopic",
31              "jms/topic/ejava/examples/jmsMechanics/topic1");
32  
33      protected static String adminUser = System.getProperty("admin.user", "admin1");
34      protected static String adminPassword = System.getProperty("admin.password", "password1!");
35      protected static String user = System.getProperty("user", "user1");
36      protected static String password = System.getProperty("password", "password1!");
37  
38      private static ArtemisServer server; //used when JMS server embedded in JVM
39      private static Context jndi;     //used when JMS server remote in JBoss
40      private static ConnectionFactory connFactory;
41      protected static Connection connection;    
42  
43      @BeforeClass
44  	public static final void setUpClass() throws Exception {
45          logger.info("connFactoryJNDI={}", connFactoryJNDI);
46          logger.info("jndi.name.testQueue={}", queueJNDI);
47          logger.info("jndi.name.testTopic={}", topicJNDI);
48  
49          logger.debug("getting jndi initial context");
50          jndi = new InitialContext();    
51          logger.debug("jndi=" + jndi.getEnvironment());
52  
53          if (jmsEmbedded) {
54  			logger.info("using embedded JMS server");
55  			server = new ArtemisServer();
56  			server.start();			
57  		}
58          connFactory=(ConnectionFactory) jndi.lookup(connFactoryJNDI);
59          assertNotNull("connFactory not found:" + connFactoryJNDI, connFactory);
60   		connection = createConnection();
61  		connection.start();
62  	}
63  	
64  	@AfterClass
65  	public static final void tearDownClass() throws Exception {
66  		if (connection != null) {
67  			connection.stop();
68  			connection.close();
69  			connection = null;
70  		}
71  		if (server != null) {
72  			server.stop();
73  			server=null;
74  		}
75  		if (jndi != null) {
76  			jndi.close();
77  			jndi=null;
78  		}
79  	}
80  
81  	protected static Connection createConnection() throws JMSException {
82  		return connFactory.createConnection(user, password);
83  	}
84  	
85  	protected Object lookup(String name) throws NamingException {
86  		logger.debug("lookup:" + name);
87  		return jndi.lookup(name);	
88  	}
89  	
90  	protected MessageCatcher createCatcher(String name, Destination destination) {
91          MessageCatcher catcher = new MessageCatcher(name);
92          catcher.setConnFactory(connFactory);
93          catcher.setDestination(destination);
94          catcher.setUser(user);
95          catcher.setPassword(password);
96          return catcher;
97  	}
98  	
99  	protected void startCatcher(MessageCatcher catcher) throws Exception {
100         new Thread(catcher).start();
101         while (catcher.isStarted() != true) {
102             logger.debug(String.format("waiting for %s to start", catcher.getName()));
103             Thread.sleep(2000);
104         }
105 	}
106 	
107 	protected void shutdownCatcher(MessageCatcher catcher) throws Exception {
108         	if (catcher != null) {
109     	        for (int i=0; !catcher.isStarted() && i< 10; i++) {
110     	            logger.debug(String.format("waiting for %s to start", catcher.getName()));
111     	            Thread.sleep(2000);
112     	        }
113     	        catcher.stop();
114     	        for (int i=0; !catcher.isStopped() && i<10; i++) {
115     	            logger.debug(String.format("waiting for %s to stop", catcher.getName()));
116     	            Thread.sleep(2000);
117     	        }
118         	}		
119 	}
120 }