From 90a4d6bc045a5b14972219d0bd0923385ff1414a Mon Sep 17 00:00:00 2001 From: samishal Date: Tue, 15 May 2018 09:06:34 +0100 Subject: [PATCH] Reworked the StompClient example Added Java util logger. Moved the BrokerURI and queue name fields into string contants. Added a fianlly clause to handle the closing of Connection,Session and Consumer --- README.md | 63 +++++++++++++++++++++++++++++++++---------------------- 1 file changed, 38 insertions(+), 25 deletions(-) diff --git a/README.md b/README.md index 920e5ad..8e991cd 100644 --- a/README.md +++ b/README.md @@ -16,22 +16,32 @@ compile group: 'org.fusesource.stompjms', name: 'stompjms-client', version: '1.1 ``` java package darwinStomp; +import java.util.logging.Level; +import java.util.logging.Logger; +import javax.jms.Connection; +import javax.jms.JMSException; +import javax.jms.MessageConsumer; +import javax.jms.Queue; +import javax.jms.Session; import org.fusesource.stomp.jms.StompJmsConnectionFactory; -import javax.jms.*; public class StompClient implements Runnable { - public static void main(String[] args) throws Exception { + private static final Logger LOG = Logger.getLogger(StompClient.class.getName()); + + private final String BROKER_URI = "tcp://datafeeds.nationalrail.co.uk:61613"; + private final String QUEUE_NAME = "Your security key from My Feeds"; + + public static void main(String[] args) { new StompClient().run(); } @Override public void run() { - String brokerUri = "tcp://datafeeds.nationalrail.co.uk:61613"; - String QUEUE_NAME = "Your security key from My Feeds" StompJmsConnectionFactory connectionFactory = new StompJmsConnectionFactory(); - connectionFactory.setBrokerURI(brokerUri); + + connectionFactory.setBrokerURI(this.BROKER_URI); Connection connection = null; Session session = null; @@ -39,44 +49,47 @@ public class StompClient implements Runnable { try { connection = connectionFactory.createConnection("d3user", "d3password"); - connection.start(); session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); - Queue queue = session.createQueue(QUEUE_NAME); + Queue queue = session.createQueue(this.QUEUE_NAME); consumer = session.createConsumer(queue); - System.out.println("Connected to STOMP " + brokerUri); + System.out.println("Connected to STOMP " + this.BROKER_URI); consumer.setMessageListener(new MessageHandler()); - while (!Thread.interrupted()) {} + while (!Thread.interrupted()) { + } - try { - if (consumer != null) { + } catch (JMSException ex) { + Logger.getLogger(StompClient.class.getName()).log(Level.SEVERE, null, ex); + System.out.println("Thread interupted by exception or shutdown"); + } finally { + if (consumer != null) { + try { consumer.close(); + } catch (JMSException ex) { + LOG.log(Level.SEVERE, null, ex); } - - if (session != null) { + } + if (session != null) { + try { session.close(); + } catch (JMSException ex) { + LOG.log(Level.SEVERE, null, ex); } - - if (connection != null) { + } + if (connection != null) { + try { connection.close(); - connection = null; + } catch (JMSException ex) { + LOG.log(Level.SEVERE, null, ex); } - } catch (JMSException ex) { - System.out.println("Got exception on shutdown"); - ex.printStackTrace(); } - - System.out.println("Thread was interrupted!"); - - - } catch (JMSException e) { - e.printStackTrace(); } } } + ``` ##### src/main/java/darwinStomp/MessageHandler