Advertisement
Guest User

Untitled

a guest
Sep 28th, 2016
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.35 KB | None | 0 0
  1. package com.amq.concumer;
  2.  
  3.  
  4. import java.util.Properties;
  5. import javax.jms.*;
  6. import javax.naming.*;
  7.  
  8. public class AMW {
  9.  
  10.  
  11. private String queueName = "adstream.yadn.nodes.LVO1V-GDNSTOR02";
  12. private String user = "system";
  13. private String password = "manager";
  14. //private String url = "ssl://hostname:61616";
  15. private String url = "tcp://10.44.127.90:61616";
  16.  
  17. private boolean transacted;
  18. private boolean isRunning = false;
  19.  
  20. public static void main(String[] args) throws NamingException, JMSException {
  21. AMW consumer = new AMW();
  22. consumer.run();
  23. }
  24.  
  25. public AMW() {
  26. /** For SSL connections only, add the following: **/
  27. // System.setProperty("javax.net.ssl.keyStore", "path/to/client.ks");
  28. // System.setProperty("javax.net.ssl.keyStorePassword", "password");
  29. // System.setProperty("javax.net.ssl.trustStore", "path/to/client.ts");
  30.  
  31. }
  32.  
  33. public void run() throws NamingException, JMSException {
  34. isRunning = true;
  35.  
  36. //JNDI properties
  37. Properties props = new Properties();
  38. props.setProperty(Context.INITIAL_CONTEXT_FACTORY, "org.apache.activemq.jndi.ActiveMQInitialContextFactory");
  39. props.setProperty(Context.PROVIDER_URL, url);
  40.  
  41. //specify queue propertyname as queue.jndiname
  42. props.setProperty("queue.slQueue", queueName);
  43.  
  44. javax.naming.Context ctx = new InitialContext(props);
  45. ConnectionFactory connectionFactory = (ConnectionFactory) ctx.lookup("ConnectionFactory");
  46. Connection connection = connectionFactory.createConnection(user, password);
  47. connection.start();
  48.  
  49. Session session = connection.createSession(transacted, Session.AUTO_ACKNOWLEDGE);
  50.  
  51. Destination destination = (Destination) ctx.lookup("slQueue");
  52. //Using Message selector ObjectClass = ‘AlarmImpl’
  53. MessageConsumer consumer = session.createConsumer(destination);
  54.  
  55. while (isRunning) {
  56. System.out.println("Waiting for message...");
  57. Message message = consumer.receive(1000);
  58. if (message != null && message instanceof TextMessage) {
  59. TextMessage txtMsg = (TextMessage) message;
  60. System.out.println("Received: " + txtMsg.getText());
  61. }
  62. }
  63. System.out.println("Closing connection");
  64. consumer.close();
  65. session.close();
  66. connection.close();
  67. }
  68. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement