JMS Consumer Mode

Source: Internet
Author: User

The full name of JMS is the Java message service, or Java messaging services. It is used primarily for messaging between producers and consumers, where producers are responsible for generating messages, and consumers are responsible for receiving messages. To apply it to the actual business requirements we can use the producer to generate a message and send it at a specific time, and the corresponding consumer will complete the corresponding business logic after receiving the corresponding message.

Apache official website Download activemq (http://activemq.apache.org/download.html), unzip the run Bin directory under Activemq.bat file startup Activemq

There are two types of message delivery, one is point-to-point , that is, one producer and one consumer, and the other is the publish/Subscribe mode, where a producer generates a message and sends it, which can be received by multiple consumers.

1. Peer-to-peer message mode (point-to-point Messaging)

The following JMS objects are required in a point-to-point message pattern:

A. Queue – a provider-named queue object that the client will use for this named queue object

B. Queue link Factory (queueconnectionfactory) – Client uses a queue link factory to create a link queue

Connectionqueue to obtain a link to the JMS point-to-point message provider.

C. Link queue (Connectionqueue) – an active link queue exists between the client and the point-to-point message provider

, customers use it to create one or more JMS queue sessions (queuesession)

D. Queue session (queuesession) – The sender and recipient (Queuesenderand) used to create a queue message

QueueReceiver)

E. Message sender (Queuesender or MessageProducer) – Send a message to a queue that has already been declared

F. Message recipients (QueueReceiver or Messageconsumer) – accept messages that have been sent to the specified queue

2. Publish subscription model (Publish–subscribe mode)

A. Theme topic (Destination) – a provider-named theme object that the client will use for this named Subject Object

B. Topic link Factory (topciconnectionfactory) – Clients create linked topics using the topic link Factory

Connectiontopic to obtain a link to the JMS message pub/sub provider.

C. Link theme (Connectiontopic) – an active link topic exists between the Publisher and the Subscriber

D. session (Topicsession) – The Publisher and Subscriber (Topicpublisher and) used to create the topic message

Topicsubscribers)

E. Message sender MessageProducer) – Send a message to a topic that has already been declared

F. Message recipient (Messageconsumer) – Accept messages that have been sent to the specified topic

Using perceptual data as an example

Activemq.properties configuration file:

Topic=csp.jxmessages
ipaddress=10.100.70.102
#ipaddress =localhost
port=61616
Username=user
Password=user

Consumer mode:

 Packagecom.ship;ImportJava.sql.DriverManager;Importjava.sql.SQLException;Importjava.sql.Statement;Importjava.util.Properties;Importjavax.annotation.PostConstruct;ImportJavax.annotation.PreDestroy;Importjavax.jms.Connection;Importjavax.jms.ConnectionFactory;Importjavax.jms.Destination;Importjavax.jms.JMSException;ImportJavax.jms.Message;ImportJavax.jms.MessageConsumer;ImportJavax.jms.MessageListener;Importjavax.jms.Session;ImportJavax.jms.TextMessage;Importorg.apache.activemq.ActiveMQConnectionFactory;ImportOrg.springframework.stereotype.Controller;ImportCom.common.utils.PropertyLoader; @Controller Public classJmsconsumerImplementsMessageListener {PrivateJava.sql.Connection con =NULL; Private StaticJmsconsumer instance =NULL;  PublicJmsconsumer () {if(Instance! =NULL)            Throw Newruntimeexception (); Instance= This; }     PublicJmsconsumer getinstance () {returninstance; } @Override Public voidonMessage (Message message) {if(Messageinstanceoftextmessage) {TextMessage txtmsg=(textmessage) message; Try{String content=Txtmsg.gettext (); System.out.println ("Data is: \ n" +content); String[] Arrcontent= Content.split ("\ n"); //Con.setautocommit (FALSE); //Statement Statement = Con.createstatement ();                 for(inti = 0; i < arrcontent.length; i++) {string[] shipinfo= Arrcontent[i].split (","); //System.out.println (shipinfo[0]);//Test Vessel Name//Stored Procedures//String sql = "Call Ganzhi ()"; //Add Batch processing//statement.addbatch (SQL);                }                //batch processing because of a read/write delay//Statement.executebatch (); //Con.commit (); //statement.close ();}Catch(Exception e) {e.printstacktrace (); } }} @PostConstruct Public voidinit () {Try {            //Database ConnectionBuildsqlcon (); //JMS ProcessingBuildjmscon (); } Catch(Exception e) {e.printstacktrace (); }    }    Private voidBuildsqlcon () {//TODO auto-generated Method StubProperties Jdbcprop =Propertyloader.getpropertiesfromclasspath ("Jdbc.properties", "UTF-8"); String Driverclassname= Jdbcprop.getproperty ("Jdbc.driverclassname"); String Jdbcurl= Jdbcprop.getproperty ("Jdbc.url"); String uname= Jdbcprop.getproperty ("Jdbc.username"); String pwd= Jdbcprop.getproperty ("Jdbc.password"); Try{class.forname (driverclassname); Con= Drivermanager.getconnection (Jdbcurl + "&user=" +uname+ "&password=" +pwd); } Catch(Exception e) {//TODO auto-generated Catch blockE.printstacktrace (); }    }     Public voidBuildjmscon () {//the main process for consumersConnection Connection =NULL; Properties Properties=Propertyloader.getpropertiesfromclasspath ("Activemq.properties", "UTF-8"); String Topic= Properties.getproperty ("topic"); String username= Properties.getproperty ("username"); String Password= Properties.getproperty ("Password"); String IPAddress= Properties.getproperty ("IPAddress"); String Port= Properties.getproperty ("Port"); String Brokerurl= "failover://tcp://" + ipaddress + ":" +Port; Try {            //1. Initialize the connection plantConnectionFactory ConnectionFactory =Newactivemqconnectionfactory (username, password, brokerurl); //2. Create ConnectionConnection =connectionfactory.createconnection (); //3. Open the connectionConnection.start (); System.out.println ("The connection was successful ........."); //4. Create SessionSession session = Connection.createsession (false, Session.auto_acknowledge); //5. Create a message destinationDestination Destination =session.createtopic (topic); //6. Create a consumerMessageconsumer consumer =Session.createconsumer (destination); //7. Configuring MonitoringConsumer.setmessagelistener (getinstance ()); } Catch(jmsexception e) {e.printstacktrace (); }} @PreDestroy Public voiddestroy () {if(Con! =NULL)            Try{con.close (); } Catch(SQLException e) {e.printstacktrace (); }    }}

Test:

The data are:
Zhejiang Yuhang goods 01803,120.16185,30.098183,0.0,140,140,2016-06-02 09:40:13:000,,wm2hz803244,1,0
Zhejiang Fuyang Goods 00759,119.844083,29.932533,null,0,0,2016-06-02 09:40:13:000,,wm2hz804658,1,0
Zhejiang Changxing goods 1952,120.320117,30.630767,0.0,0,0,2016-06-02 09:40:13:000,,wm2hz820795,1,0
Zhejiang Fuyang Goods 00759,119.844083,29.932533,0.0,0,0,2016-06-02 09:40:13:000,,wm2hz804658,1,0
Zhejiang Yuecheng goods 0677,118.859033,32.171567,14.0,50,50,2016-06-02 09:40:13:000,,wm2hz814170,1,0
Zhejiang Qianjiang Goods 35077,120.122567,30.400967,0.0,340,340,2016-06-02 09:40:13:000,,wm2hz802275,1,0
Zhejiang Yuecheng goods 0677,118.859033,32.171567,14.0,50,50,2016-06-02 09:40:13:000,,wm2hz814170,1,0
Zhejiang Yuhang goods 01667,120.12385,30.402033,0.0,20,20,2016-06-02 09:40:13:000,,wm2hz801640,1,0
Zhejiang Yuecheng goods 0670,120.736333,32.040183,0.0,210,210,2016-06-02 09:40:14:000,,wm2hz817006,1,0
Zhejiang Anji goods 2389,120.149133,30.636817,0.0,220,220,2016-06-02 09:40:14:000,,wm2hz811204,1,0
Zhejiang Qianjiang Goods 00628,120.053833,30.047217,8.0,210,210,2016-06-02 09:40:14:000,,wm2hz800153,1,0
Zhejiang Shaoxing Yun 6-25,120.473033,30.6393,6.0,230,230,2016-06-02 09:40:14:000,,wm2hz801842,1,0
Zhejiang Shangyu goods 0553,119.705217,29.82325,null,330,330,2016-06-02 09:40:14:000,,wm2hz801018,1,0
Zhejiang Yuhang goods 01782,120.07065,30.399117,0.0,20,20,2016-06-02 09:40:14:000,,wm2hz801764,1,0
Zhejiang Tonglu goods 00483,120.217017,30.269617,0.0,20,20,2016-06-02 09:40:14:000,,wm2hz803645,1,0
Hui Hai ji 026,120.1237,30.401283,0.0,0,0,2016-06-02 09:40:14:000,,wm2hz811220,1,0
Hai Yuan 186,121.394967,31.485683,0.0,0,0,2016-06-02 09:40:14:000,,wm2hz815369,1,0
No. 1th Ren Qiang, 120.085533,30.0672,0.0,30,30,2016-06-02 09:40:14:000,,wm2hz818227,1,0
Jinshun 668,120.166217,30.125183,0.0,0,0,2016-06-02 09:40:14:000,,wm2hz819091,1,0
Zhejiang Fuyang Goods 00636,119.705533,29.823683,null,200,200,2016-06-02 09:40:14:000,,wm2hz804624,1,0
Zhejiang Linan Tour 026,119.769083,30.230167,0.0,0,0,2016-06-02 09:40:14:000,,wm2hz817016,1,0
Zhejiang Xiaoshan Goods 23922,120.29145,30.501783,7.0,80,80,2016-06-02 09:40:14:000,,wm2hz818998,1,0
Zhejiang Xiaoshan Goods 23922,120.29145,30.501783,null,80,80,2016-06-02 09:40:14:000,,wm2hz818998,1,0
Zhejiang Jiashan goods 03216,120.141717,30.355,0.0,280,280,2016-06-02 09:40:14:000,,wm2hz800918,1,0
Zhejiang Xiaoshan Goods 03166,119.835467,29.907917,0.0,310,310,2016-06-02 09:40:14:000,,wm2hz801393,1,0
Zhejiang Xiaoshan Goods 23751,120.156167,30.100917,0.0,100,100,2016-06-02 09:40:14:000,,wm2hz802551,1,0
Hefei Wu Yun 628,120.16455,30.125717,null,0,0,2016-06-02 09:40:14:000,,wm2hz802141,1,0

JMS Consumer Mode

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.