Grega Bremec 3 éve
szülő
commit
923104825f

+ 1 - 3
java-jms-plain/metrics-collector/src/main/java/com/redhat/training/collector/CollectMetric.java

@@ -7,8 +7,6 @@ public class CollectMetric {
     public static void main(String... args) {
     public static void main(String... args) {
         Logger LOG = LoggerFactory.getLogger(CollectMetric.class);
         Logger LOG = LoggerFactory.getLogger(CollectMetric.class);
 
 
-        // TODO: prepare connection objects
-
         // TODO: declare JMS objects
         // TODO: declare JMS objects
 
 
         try {
         try {
@@ -29,7 +27,7 @@ public class CollectMetric {
             try {
             try {
                 // TODO: wait up to 1 second to receive message
                 // TODO: wait up to 1 second to receive message
 
 
-                // TODO: print the message in the log
+                // TODO: print the message in the log if not null
                 LOG.info("Got: " + null);
                 LOG.info("Got: " + null);
             } catch (Exception je) { // TODO: Specific exception type
             } catch (Exception je) { // TODO: Specific exception type
                 je.printStackTrace();
                 je.printStackTrace();

+ 25 - 46
java-jms-plain/stock-subscriber/src/main/java/com/redhat/training/stock/receive/Subscribe.java

@@ -2,16 +2,6 @@ package com.redhat.training.stock.receive;
 
 
 import java.util.UUID;
 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.Logger;
 import org.slf4j.LoggerFactory;
 import org.slf4j.LoggerFactory;
 
 
@@ -19,62 +9,51 @@ public class Subscribe {
     public static void main(String... args) {
     public static void main(String... args) {
         Logger LOG = LoggerFactory.getLogger(Subscribe.class);
         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 {
         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();
             ne.printStackTrace();
             return;
             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;
         String cid;
-        if (args.length == 0 || args[0] == "") {
+        if (args.length < 2 || args[1] == "") {
             cid = UUID.randomUUID().toString();
             cid = UUID.randomUUID().toString();
         } else {
         } 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();
             je.printStackTrace();
             return;
             return;
         }
         }
 
 
         while (true) {
         while (true) {
             try {
             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();
                 je.printStackTrace();
             }
             }
-
-            try {
-                Thread.sleep(1000);
-            } catch (InterruptedException e) {
-                e.printStackTrace();
-            }
         }
         }
     }
     }
 }
 }

+ 8 - 32
java-jms-plain/stock-updater/src/main/java/com/redhat/training/stock/send/Update.java

@@ -2,16 +2,6 @@ package com.redhat.training.stock.send;
 
 
 import java.util.Random;
 import java.util.Random;
 
 
-import javax.jms.Connection;
-import javax.jms.ConnectionFactory;
-import javax.jms.JMSException;
-import javax.jms.MessageProducer;
-import javax.jms.Topic;
-import javax.jms.Session;
-import javax.jms.TextMessage;
-import javax.naming.InitialContext;
-import javax.naming.NamingException;
-
 import org.slf4j.Logger;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.slf4j.LoggerFactory;
 
 
@@ -26,31 +16,18 @@ public class Update {
             "VMW"
             "VMW"
         };
         };
 
 
-        // prepare connection objects
-        InitialContext ctx;
-        ConnectionFactory cf;
-        Connection c;
-        Session s;
-        MessageProducer mp;
-        Topic t;
-        
+        // TODO: declare JMS objects
+
         try {
         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();
             ne.printStackTrace();
             return;
             return;
         }
         }
 
 
         try {
         try {
-            c = cf.createConnection();
-            c.start();
-
-            s = c.createSession();
-            mp = s.createProducer(t);
-
-        } catch (JMSException je) {
+            // TODO: create and start the connection, obtain session and producer
+        } catch (Exception je) { // TODO: be specific about exception
             je.printStackTrace();
             je.printStackTrace();
             return;
             return;
         }
         }
@@ -61,9 +38,8 @@ public class Update {
             LOG.info(report);
             LOG.info(report);
 
 
             try {
             try {
-                TextMessage tm = s.createTextMessage(report);
-                mp.send(tm);
-            } catch (JMSException je) {
+                // TODO: send a new message to destination
+            } catch (Exception je) { // TODO: be specific about exception
                 je.printStackTrace();
                 je.printStackTrace();
             }
             }