Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.gateways.adapters;
- import java.util.*;
- import javax.jms.*;
- import javax.naming.*;
- import com.pojo.messages.MessageBody;
- public class JMSPublisher {
- // we start by setting up our properties for the object, including the basic connection values for
- // the messaging service -- you'll note that the _providerURL and _contextFactory properties match the
- // value set in the messaging-config.xml file
- public String _providerURL = "tcp://localhost:61616";
- public String _contextFactory = "org.apache.activemq.jndi.ActiveMQInitialContextFactory";
- public TopicSession _pubSession;
- public TopicPublisher _publisher;
- public TopicConnection _connection;
- public ObjectMessage _message;
- public JMSPublisher(){
- }
- /* A bit too much here to go into but, basically,
- * we setup our connection properties and create a context
- * 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)
- * 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
- * next, we create and populate the object message with the actual message we want (our POJO) to send
- * finally, we publish the message to our topic
- */
- public void publishMessage(MessageBody _msg, String _topic, String _selector){
- try{
- Properties p = new Properties();
- p.put(Context.PROVIDER_URL, _providerURL);
- p.put(Context.INITIAL_CONTEXT_FACTORY, _contextFactory);
- Context context = new InitialContext(p);
- Object tmp = context.lookup("TopicConnectionFactory");
- TopicConnectionFactory factory = (TopicConnectionFactory) tmp;
- _connection = factory.createTopicConnection();
- _pubSession = _connection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);
- Topic topic = (Topic) context.lookup(_topic);
- _publisher = _pubSession.createPublisher(topic);
- // It's an ObjectMessage (see messaging-config.xml) message
- _message = _pubSession.createObjectMessage();
- _message.setObject(_msg);
- // This is key! If you need to filter a message, we set the selector value here
- _message.setStringProperty("subscriberSelector", _selector);
- // Finally, we actually publish the message to our ActiveMQ topic
- _publisher.publish(topic, _message, Message.DEFAULT_DELIVERY_MODE, Message.DEFAULT_PRIORITY, 5 * 60 * 1000);
- }
- catch (Exception e){
- e.printStackTrace();
- }
- }
- // I use this message on the back-end to setup my topics when my application starts up (fired from onApplicationStart in my Application.cfc
- public void initMessages(String _topic){
- try{
- Properties p = new Properties();
- p.put(Context.PROVIDER_URL, _providerURL);
- p.put(Context.INITIAL_CONTEXT_FACTORY, _contextFactory);
- Context context = new InitialContext(p);
- Object tmp = context.lookup("TopicConnectionFactory");
- TopicConnectionFactory factory = (TopicConnectionFactory) tmp;
- _connection = factory.createTopicConnection();
- _pubSession = _connection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);
- Topic topic = (Topic) context.lookup(_topic);
- _publisher = _pubSession.createPublisher(topic);
- _publisher.close();
- }
- catch (Exception e){
- e.printStackTrace();
- }
- }
- }
Add Comment
Please, Sign In to add comment