Thứ Sáu, 7 tháng 6, 2013

JBOSS JMS – SENDER & LISTENER A TOPIC




1.       Create a Java project and set up lib as below images
Note: Must be correct libs and location of libs

2.       Create 2 classes as below code
Listener Class
/*
Use source code downloads, example commands,
and any other techniques at your own risk.
No warranty is provided.
*/
import java.util.Properties;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.TextMessage;
import javax.jms.Topic;
import javax.jms.TopicConnection;
import javax.jms.TopicConnectionFactory;
import javax.jms.TopicSession;
import javax.jms.TopicSubscriber;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
public class Listener implements MessageListener {
                String url_;
                String name_;
                TopicConnection conn = null;
                TopicSession session = null;
                Topic topic = null;
                public Listener(String url, String name) {
                                super();
                                url_ = url;
                                name_ = name;
                                try {
                                                this.initializeListener();
                                } catch (Exception e) {
                                                System.out.println("Error creating listener: " + e);
                                                e.printStackTrace();
                                }
                }
                public void onMessage(Message msg) {
                                TextMessage tm = (TextMessage) msg;
                                try {
                                                System.out.println("Incoming message: " + tm.getText());
                                } catch (Exception e) {
                                                e.printStackTrace();
                                }
                }
                private void initializeListener() throws JMSException, NamingException {
                                Properties props = new Properties();
                props.setProperty(Context.INITIAL_CONTEXT_FACTORY,"org.jnp.interfaces.NamingContextFactory");
                                props.setProperty(Context.URL_PKG_PREFIXES, "org.jboss.naming:org.jnp.interfaces");
                                props.setProperty(Context.PROVIDER_URL, url_);
                                props.setProperty(Context.SECURITY_CREDENTIALS, "bms");
                                props.setProperty(Context.SECURITY_AUTHENTICATION, "bms");
                                Context context = new InitialContext(props);
                                System.out.println("performing lookup...");
                                Object tmp = context.lookup("ConnectionFactory");
                                System.out.println("lookup completed, making topic");
                                TopicConnectionFactory tcf = (TopicConnectionFactory) tmp;
                                conn = tcf.createTopicConnection("bms","bms");
                                topic = (Topic) context.lookup(name_);
                                session = conn.createTopicSession(false, TopicSession.AUTO_ACKNOWLEDGE);
                                conn.start();
                                TopicSubscriber recv = session.createSubscriber(topic);
                                recv.setMessageListener(this);
                }
                public void disconnect() throws JMSException {
                                if(conn != null) {
                                                conn.stop();
                                }
                                if(session != null) {
                                                session.close();
                                }
                                if(conn != null) {
                                                conn.close();
                                }
                }
                public static void main(String args[]) {
                                System.out.println("Starting JMS Example Listener");
                                System.out.println("Program will be active for 1 minute.");
                                //change these values to your situtation:
                                Listener listener = new Listener("jnp://10.26.72.56:1099", "/jms/bms/NonlinearDataUpdateNotificationTopic");
                                //leave it open for 2 minutes:
                                try {
                                                Thread.sleep(120000);
                                } catch(Exception e) {
                                                System.out.println("Error sleeping: " + e);
                                                e.printStackTrace();
                                }
                                try {
                                                listener.disconnect();
                                } catch(Exception e) {
                                                System.out.println("Error terminating listener JMS objects: " + e);
                                                e.printStackTrace();
                                }
                                System.out.println("Done listening");
                }
}
Sender Class
/*
Use source code downloads, example commands,
and any other techniques at your own risk.
No warranty is provided.
*/
import java.util.Properties;
import javax.jms.JMSException;
import javax.jms.TextMessage;
import javax.jms.Topic;
import javax.jms.TopicConnection;
import javax.jms.TopicConnectionFactory;
import javax.jms.TopicPublisher;
import javax.jms.TopicSession;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
public class Sender {
                String url_;
                String name_;
                TopicConnection conn = null;
                TopicSession session = null;
                Topic topic = null;
                public Sender(String url, String name) throws JMSException, NamingException {
                                url_ = url;
                                name_ = name;
                                this.initializeSender();
                }
                private void initializeSender() throws JMSException, NamingException {
                                Properties props = new Properties();                                                                                     props.setProperty(Context.INITIAL_CONTEXT_FACTORY,"org.jnp.interfaces.NamingContextFactory");
                                props.setProperty(Context.URL_PKG_PREFIXES, "org.jboss.naming:org.jnp.interfaces");
                                props.setProperty(Context.PROVIDER_URL, url_);
                                props.setProperty(Context.SECURITY_CREDENTIALS, "bms");
                                props.setProperty(Context.SECURITY_AUTHENTICATION, "bms");
                                Context context = new InitialContext(props);
                                TopicConnectionFactory tcf = (TopicConnectionFactory) context.lookup("ConnectionFactory");
                                conn = tcf.createTopicConnection("bms","bms");
                                topic = (Topic) context.lookup(name_);
                                session = conn.createTopicSession(false, TopicSession.AUTO_ACKNOWLEDGE);
                                conn.start();
                }
                 public void send(String text) throws JMSException, NamingException {
                     // Send a text msg
                                 try {
                     TopicPublisher send = session.createPublisher(topic);
                     TextMessage tm = session.createTextMessage(text);
                     send.publish(tm);
                     send.close();
                                 }
                                 catch (Exception e) {
                                                 System.out.println(e.getMessage());
                                 }
                 }
                public void disconnect() throws JMSException {
                                if(conn != null) {
                                                conn.stop();
                                }
                                if(session != null) {
                                                session.close();
                                }
                                if(conn != null) {
                                                conn.close();
                                }
                }
                public String getTopicName() {
                                return name_;
                }
                public String getTopicURL() {
                                return url_;
                }
                public static void main(String args[]) throws Exception {
                                System.out.println("Starting JMS Example Sender");
                Sender sender = new Sender("jnp://10.26.72.56:1099", "/jms/bms/NonlinearDataUpdateNotificationTopic");
                System.out.println("Sending list of Adam Sandler Movies " + sender.url_);
                sender.send("Schedule 1995");
                sender.send("Schedule 1996");
                sender.send("Schedule 1998");
                sender.send("Schedule 1999");
                sender.send("Schedule 2002");
                sender.send("Schedule 2004");
sender.send("Schedule 2003");
                sender.disconnect();
System.out.println("JMS Example Sender Complete - list sent");
     }
}

Không có nhận xét nào:

Đăng nhận xét