JMS Simple Instance

Source: Internet
Author: User

Use ActiveMQ as the JMS provider and write a simple demo.

The main is to simulate the appearance of a chat room, the following is the source code

Package com.mycompany.app;import java.io.bufferedreader;import java.io.inputstreamreader;import  javax.jms.JMSException;import javax.jms.Message;import javax.jms.MessageListener;import  javax.jms.session;import javax.jms.textmessage;import javax.jms.topic;import  javax.jms.topicconnection;import javax.jms.topicpublisher;import javax.jms.topicsession;import  javax.jms.topicsubscriber;import org.apache.activemq.activemqconnection;import  org.apache.activemq.activemqconnectionfactory;/** * hello world! * */public  class app implements messagelistener {         private topicsession pubsession;             private TopicPublisher publisher;             private topicconnection connection;            private string username;             private static string user  = ActiveMQConnection.DEFAULT_USER;             private static String password = ActiveMQConnection.DEFAULT_PASSWORD;             private static String  url = activemqconnection.default_broker_url;             public app (String username)  throws Exception {                 this.username =  username;//Chat Room User name         ActiveMQConnectionFactory factory  = new activemqconnectionfaCtory (user, password, url);         //Create a link factory for ACTIVEMQ          connection = factory.createtopicconnection ();         pubsession = connection.createtopicsession (false,  Session.auto_acknowledge);                 topicsession subsession = connection.createtopicsession (false, Session.AUTO_ Acknowledge);           topic topic =  Subsession.createtopic ("MyTopic");        publisher =  Pubsession.createpublisher (Topic);                 topicsubscriber subscriber = subsession.createsubscriber (topic,  Null, true);    &Nbsp;    subscriber.setmessagelistener (This);         connection.start ();    }             public static void main (String[] args)  throws Exception {                 app app =  new app (Args[0]);                 bufferedreader reader = new bufferedreader (New InputStreamReader ( system.in));                 while  (True)  {                         string line = reader.readline ();                          if  ("Exit". Equals (line))  {                 app.close ();             } else {                 app.writemessage (line);             }        }    }             public void onmessage (Message msg)  {                 TextMessage  message =  (TextMessage)  msg;                 try {                         system.out.println (Message.gettext ());         } catch  (jmsexception e)  {             e.printstacktrace ();        }     }            public void  writemessage (String text)  throws Exception {                 TextMessage message =  Pubsession.createtextmessage ();         message.settext (username +   " : "  + text);         publisher.publish ( message);     } &nbsP;  public void close ()  throws Exception {         connection.close ();     }}


Installing ActiveMQ

Mac installation is relatively simplebrew install activemq

The ActiveMQ will be installed by default /usr/local/Cellar/activemq .

Run activemq setup ~/.activemqrc to specify activemq the environment configuration file first.

activemq startthe run can be started in a separate process activemq .

$ activemq startinfo:loading '/usr/local/cellar/activemq/5.11.1/libexec/bin/env ' info:using java '/Library/Java/ Javavirtualmachines/jdk1.7.0_71.jdk/contents/home/bin/java ' Info:starting-inspect logfiles specified in Logging.properties and log4j.properties to get Detailsinfo:pidfile created: '/USR/LOCAL/CELLAR/ACTIVEMQ/5.11.1/ Libexec/data/activemq.pid ' (PID ' 4880 ')


There are two ways to terminate the ActiveMQ operation. One is to use ACTIVEMQ stop.

The other is the killing process of violence, that is, kill 4880.

Run

Start 3 main methods with Maven:

MVN exec:java-dexec.mainclass= "com.mycompany.app.App"-dexec.args= "rcx1" MVN exec:java-dexec.mainclass= " Com.mycompany.app.App "-dexec.args=" rcx2 "mvn exec:java-dexec.mainclass=" com.mycompany.app.App "-dexec.args=" rcx3 "

Then in one of them say a word, the other will receive information.

Rcx2rcx1:hellohello tworcx3: Hello everyone//Rcx1hellorcx2:hello tworcx3: Hello everyone//rcx3 everyone good Rcx1:hellorcx2:hello

"Reference" Java Message service

JMS Simple Instance

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.