Browse Source

convert to exercise

Grega Bremec 3 years ago
parent
commit
3f1b66fca0

+ 12 - 41
java-jms-plain/metrics-collector/src/main/java/com/redhat/training/collector/CollectMetric.java

@@ -1,15 +1,5 @@
 package com.redhat.training.collector;
 
-import javax.jms.Connection;
-import javax.jms.ConnectionFactory;
-import javax.jms.JMSException;
-import javax.jms.MessageConsumer;
-import javax.jms.Queue;
-import javax.jms.Session;
-import javax.jms.TextMessage;
-import javax.naming.InitialContext;
-import javax.naming.NamingException;
-
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -17,52 +7,33 @@ public class CollectMetric {
     public static void main(String... args) {
         Logger LOG = LoggerFactory.getLogger(CollectMetric.class);
 
-        // prepare connection objects
-        InitialContext ctx;
-        ConnectionFactory cf;
-        Connection c;
-        Session s;
-        MessageConsumer mc;
-        Queue q;
-        
+        // TODO: prepare connection objects
+
+        // TODO: declare JMS objects
+
         try {
-            ctx = new InitialContext();
-            cf = (ConnectionFactory)ctx.lookup("ConnectionFactory");
-            q = (Queue)ctx.lookup("queue/metrics");
-        } catch (NamingException ne) {
+            // TODO: initialise context, connection factory, and destination
+        } catch (Exception ne) { // TODO: be specific about exception
             ne.printStackTrace();
             return;
         }
 
         try {
-            c = cf.createConnection();
-            c.start();
-
-            s = c.createSession();
-            mc = s.createConsumer(q);
-        } catch (JMSException je) {
+            // TODO: create and start the connection, obtain session and consumer
+        } catch (Exception je) { // TODO: be specific about exception
             je.printStackTrace();
             return;
         }
 
         while (true) {
             try {
-                TextMessage tm = (TextMessage)mc.receive(1000);
+                // TODO: wait up to 1 second to receive message
 
-                if (tm != null) {
-                    LOG.info("Got: " + tm.getText());
-                }
-
-                continue;
-            } catch (JMSException je) {
+                // TODO: print the message in the log
+                LOG.info("Got: " + null);
+            } catch (Exception je) { // TODO: Specific exception type
                 je.printStackTrace();
             }
-
-            try {
-                Thread.sleep(1000);
-            } catch (InterruptedException e) {
-                e.printStackTrace();
-            }
         }
     }
 }

+ 9 - 31
java-jms-plain/metrics-sender/src/main/java/com/redhat/training/sender/SendMetric.java

@@ -3,16 +3,6 @@ package com.redhat.training.sender;
 import java.lang.management.ManagementFactory;
 import java.lang.management.ThreadMXBean;
 
-import javax.jms.Connection;
-import javax.jms.ConnectionFactory;
-import javax.jms.JMSException;
-import javax.jms.MessageProducer;
-import javax.jms.Queue;
-import javax.jms.Session;
-import javax.jms.TextMessage;
-import javax.naming.InitialContext;
-import javax.naming.NamingException;
-
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -26,30 +16,18 @@ public class SendMetric {
         long threadId = Thread.currentThread().getId();
         long oldTime = 0;
 
-        // prepare connection objects
-        InitialContext ctx;
-        ConnectionFactory cf;
-        Connection c;
-        Session s;
-        MessageProducer mp;
-        Queue q;
-        
+        // TODO: declare JMS objects
+
         try {
-            ctx = new InitialContext();
-            cf = (ConnectionFactory)ctx.lookup("ConnectionFactory");
-            q = (Queue)ctx.lookup("queue/metrics");
-        } catch (NamingException ne) {
+            // TODO: initialise context, connection factory, and destination
+        } catch (Exception ne) { // TODO: be specific about exception
             ne.printStackTrace();
             return;
         }
 
         try {
-            c = cf.createConnection();
-            c.start();
-
-            s = c.createSession();
-            mp = s.createProducer(q);
-        } catch (JMSException je) {
+            // TODO: create and start the connection, obtain session and producer
+        } catch (Exception je) { // TODO: be specific about exception
             je.printStackTrace();
             return;
         }
@@ -61,13 +39,13 @@ public class SendMetric {
             LOG.info(report);
 
             try {
-                TextMessage tm = s.createTextMessage(report);
-                mp.send(tm);
-            } catch (JMSException je) {
+                // TODO: create a message and send it
+            } catch (Exception je) { // TODO: be specific about exception
                 je.printStackTrace();
             }
 
             oldTime = newTime;
+
             try {
                 Thread.sleep(1000);
             } catch (InterruptedException e) {