| 
					
				 | 
			
			
				@@ -2,16 +2,6 @@ package com.redhat.training.stock.receive; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 import java.util.UUID; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-import javax.jms.Connection; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-import javax.jms.ConnectionFactory; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-import javax.jms.JMSException; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-import javax.jms.Topic; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-import javax.jms.TopicSubscriber; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-import javax.jms.Session; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-import javax.jms.TextMessage; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-import javax.naming.InitialContext; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-import javax.naming.NamingException; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				- 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 import org.slf4j.Logger; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 import org.slf4j.LoggerFactory; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
	
		
			
				| 
					
				 | 
			
			
				@@ -19,62 +9,51 @@ public class Subscribe { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				     public static void main(String... args) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				         Logger LOG = LoggerFactory.getLogger(Subscribe.class); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        // prepare connection objects 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        InitialContext ctx; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        ConnectionFactory cf; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        Connection c; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        Session s; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        TopicSubscriber ts; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        Topic t; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-         
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        // TODO: declare JMS objects 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				         try { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            ctx = new InitialContext(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            cf = (ConnectionFactory)ctx.lookup("ConnectionFactory"); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            t = (Topic)ctx.lookup("topic/stocks"); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        } catch (NamingException ne) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            // TODO: initialise context, connection factory, and destination 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        } catch (Exception ne) { // TODO: be specific about exception 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				             ne.printStackTrace(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				             return; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				         } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        // parameter num. 1: is this consumer durable (boolean, default is false) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        boolean durable = false; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        if (args.length > 0 && args[0] != "") { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            durable = Boolean.parseBoolean(args[0]); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        // parameter num. 2: consumer ID (default is to generate a new ID) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				         String cid; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        if (args.length == 0 || args[0] == "") { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        if (args.length < 2 || args[1] == "") { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				             cid = UUID.randomUUID().toString(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				         } else { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            cid = args[0]; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            cid = args[1]; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				         } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        String sid = "stockticks"; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        try { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            c = cf.createConnection(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            c.setClientID(cid); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            c.start(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            s = c.createSession(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            ts = s.createDurableSubscriber(t, sid); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        // subscription ID - always the same 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        String sid = "stockticks"; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            LOG.info("Created a durable subscriber (ID " + cid + " for topic " + t.getTopicName() + " (SID " + sid + ")"); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        } catch (JMSException je) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        try { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            // TODO: create and start the connection, obtain session and consumer 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            // TODO: if the subscription is to be durable, set client ID 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            // TODO: create the appropriate type of consumer, durable or non-durable 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            LOG.info("Created a (non)? durable consumer with ID x for destination y"); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        } catch (Exception je) { // TODO: be specific about exception 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				             je.printStackTrace(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				             return; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				         } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				         while (true) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				             try { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-                TextMessage tm = (TextMessage)ts.receive(1000); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				- 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-                if (tm != null) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-                    LOG.info("Got: " + tm.getText()); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-                } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                // TODO: wait up to 1 second to receive message 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-                continue; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            } catch (JMSException je) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                // TODO: print the message in the log if not null 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                LOG.info("Got: " + null); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            } catch (Exception je) { // TODO: Specific exception type 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                 je.printStackTrace(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				             } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				- 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            try { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-                Thread.sleep(1000); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            } catch (InterruptedException e) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-                e.printStackTrace(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				         } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				     } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 } 
			 |