Guest User

Untitled

a guest
Jul 17th, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.28 KB | None | 0 0
  1. package com.gateways.adapters;
  2.  
  3. import java.util.*;
  4. import javax.jms.*;
  5. import javax.naming.*;
  6. import com.pojo.messages.MessageBody;
  7.  
  8. public class JMSPublisher {
  9. // we start by setting up our properties for the object, including the basic connection values for
  10. // the messaging service -- you'll note that the _providerURL and _contextFactory properties match the
  11. // value set in the messaging-config.xml file
  12. public String _providerURL = "tcp://localhost:61616";
  13. public String _contextFactory = "org.apache.activemq.jndi.ActiveMQInitialContextFactory";
  14. public TopicSession _pubSession;
  15. public TopicPublisher _publisher;
  16. public TopicConnection _connection;
  17. public ObjectMessage _message;
  18.  
  19. public JMSPublisher(){
  20.  
  21. }
  22. /* A bit too much here to go into but, basically,
  23. * we setup our connection properties and create a context
  24. * next, we use the context to get our connection factory (TopicConnectionFactory is the name we assigned to our factory in our context.xml file ... in the Resource elements)
  25. * next, we create a topic session and get our topic (the topic is passed into the method so that we can dynamically get different topics with the same method
  26. * next, we create and populate the object message with the actual message we want (our POJO) to send
  27. * finally, we publish the message to our topic
  28. */
  29. public void publishMessage(MessageBody _msg, String _topic, String _selector){
  30. try{
  31. Properties p = new Properties();
  32. p.put(Context.PROVIDER_URL, _providerURL);
  33. p.put(Context.INITIAL_CONTEXT_FACTORY, _contextFactory);
  34. Context context = new InitialContext(p);
  35.  
  36. Object tmp = context.lookup("TopicConnectionFactory");
  37. TopicConnectionFactory factory = (TopicConnectionFactory) tmp;
  38.  
  39. _connection = factory.createTopicConnection();
  40. _pubSession = _connection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);
  41.  
  42. Topic topic = (Topic) context.lookup(_topic);
  43. _publisher = _pubSession.createPublisher(topic);
  44. // It's an ObjectMessage (see messaging-config.xml) message
  45. _message = _pubSession.createObjectMessage();
  46. _message.setObject(_msg);
  47. // This is key! If you need to filter a message, we set the selector value here
  48. _message.setStringProperty("subscriberSelector", _selector);
  49. // Finally, we actually publish the message to our ActiveMQ topic
  50. _publisher.publish(topic, _message, Message.DEFAULT_DELIVERY_MODE, Message.DEFAULT_PRIORITY, 5 * 60 * 1000);
  51. }
  52. catch (Exception e){
  53. e.printStackTrace();
  54. }
  55. }
  56. // I use this message on the back-end to setup my topics when my application starts up (fired from onApplicationStart in my Application.cfc
  57. public void initMessages(String _topic){
  58. try{
  59. Properties p = new Properties();
  60. p.put(Context.PROVIDER_URL, _providerURL);
  61. p.put(Context.INITIAL_CONTEXT_FACTORY, _contextFactory);
  62. Context context = new InitialContext(p);
  63.  
  64. Object tmp = context.lookup("TopicConnectionFactory");
  65. TopicConnectionFactory factory = (TopicConnectionFactory) tmp;
  66.  
  67. _connection = factory.createTopicConnection();
  68. _pubSession = _connection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);
  69.  
  70. Topic topic = (Topic) context.lookup(_topic);
  71. _publisher = _pubSession.createPublisher(topic);
  72.  
  73. _publisher.close();
  74. }
  75. catch (Exception e){
  76. e.printStackTrace();
  77. }
  78. }
  79.  
  80. }
Add Comment
Please, Sign In to add comment