Guest User

Untitled

a guest
Dec 12th, 2018
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.62 KB | None | 0 0
  1. import javax.jms.Connection;
  2. import javax.jms.Destination;
  3. import javax.jms.ExceptionListener;
  4. import javax.jms.JMSException;
  5. import javax.jms.Message;
  6. import javax.jms.MessageConsumer;
  7. import javax.jms.MessageListener;
  8. import javax.jms.Session;
  9. import javax.jms.TextMessage;
  10.  
  11. import org.apache.activemq.ActiveMQConnection;
  12. import org.apache.activemq.ActiveMQConnectionFactory;
  13.  
  14. /**
  15. * Class Receiver is an example program that uses Java Message Service (JMS) to
  16. * receive messages from the Sender program.
  17. */
  18. public class Receiver
  19. {
  20. private static String user = ActiveMQConnection.DEFAULT_USER;
  21. private static String password = ActiveMQConnection.DEFAULT_PASSWORD;
  22. private static String url = ActiveMQConnection.DEFAULT_BROKER_URL;
  23. private static String subject = "TEST.SENDRECEIVE";
  24.  
  25. public static void main
  26. (String[] args)
  27. throws Exception
  28. {
  29. // Create the connection.
  30. System.out.printf ("Creating connection ...%n");
  31. ActiveMQConnectionFactory connectionFactory =
  32. new ActiveMQConnectionFactory("tcp://localhost:61616");
  33. Connection connection = connectionFactory.createConnection();
  34. connection.setExceptionListener (new ExceptionListener()
  35. {
  36. public void onException (JMSException exc)
  37. {
  38. exc.printStackTrace (System.err);
  39. System.exit (1);
  40. }
  41. });
  42. connection.start();
  43.  
  44. // Create the session.
  45. System.out.printf ("Creating session ...%n");
  46. Session session = connection.createSession
  47. (false, // No transactions
  48. Session.AUTO_ACKNOWLEDGE);
  49. Destination destination = session.createQueue (subject);
  50.  
  51. // Create the consumer.
  52. System.out.printf ("Creating consumer ...%n");
  53. MessageConsumer consumer = session.createConsumer (destination);
  54.  
  55. // Receive and print messages.
  56. System.out.printf ("Receiving messages ...%n");
  57. consumer.setMessageListener (new MessageListener()
  58. {
  59. public void onMessage (Message msg)
  60. {
  61. if (msg instanceof TextMessage)
  62. {
  63. try
  64. {
  65. System.out.printf ("%s%n",
  66. ((TextMessage)msg).getText());
  67. }
  68. catch (JMSException exc)
  69. {
  70. System.out.printf ("Could not get message text%n");
  71. }
  72. }
  73. else
  74. {
  75. System.out.printf ("Unknown message type%n");
  76. }
  77. }
  78. });
  79. }
  80. }
Add Comment
Please, Sign In to add comment