Brief introduction of Spring JMS and ACTIVEMQ 1.1 spring JMS and ACTIVEMQ
The full name of JMS is the Java message Service, whose main purpose is to deliver the message between the producer and the consumer; in the actual business scenario, when a system completes a business operation, it needs to notify the state of the operation of system B or any other system a system. and the relevant information involved in the operation, such as when the membership card system to complete the user binding a membership card operation, you can send a message, the message content is the UID or phone for XXX users, binding a XX type (ordinary card, VIP card, etc.) card, this time other systems, For example, the system to add points to the user, you can receive this message, according to the card type of the binding to the user to add the corresponding points; but the increase of the operation and the operation of the card to the user should not be redundant, and in the future, after the successful issuance, each line of business may have their own different follow-up operations, In order to isolate the loyalty card issue from the business logic operation after the method membership card is successful, you can use the message delivery method.
There are two types of message passing, one is point-to-point, that is, one producer corresponds to a message, and the other is the publish-and-subscribe pattern, where a producer generates a message to the message queue and can be read by any consumer.
2.1 Main method for sending and reading messages
Importjavax.jms.Connection;Importjavax.jms.ConnectionFactory;Importjavax.jms.Destination;Importjavax.jms.JMSException;ImportJavax.jms.MessageProducer;Importjavax.jms.Session;ImportJavax.jms.TextMessage;Importorg.apache.activemq.ActiveMQConnection;Importorg.apache.activemq.ActiveMQConnectionFactory;/*** Message producer *@authorAdministrator **/ Public classJMSProducer {Private Static FinalString Username=activemqconnection.default_user;//The default connection user name Private Static FinalString Password=activemqconnection.default_password;//The default connection password Private Static FinalString Brokeurl=activemqconnection.default_broker_url;//The default connection address Private Static Final intsendnum=10;//number of messages sent Public Static voidMain (string[] args) {connectionfactory connectionfactory;//Connection FactoryConnection Connection =NULL;//ConnectionSession session;//threads that the session accepts or sends messagesDestination Destination;//Destination of messageMessageProducer MessageProducer;//message producer//instantiating a connection factoryConnectionFactory =Newactivemqconnectionfactory (Jmsproducer.username, Jmsproducer.password, Jmsproducer.brokeurl); Try{Connection= Connectionfactory.createconnection ();//getting connections from a connection factoryConnection.start ();//Start ConnectionSession = Connection.createsession (Boolean.true, Session.auto_acknowledge);//Create sessionDestination = Session.createqueue ("FirstQueue1");//Create a message queue FirstQueue1 the name of the message queueMessageProducer = session.createproducer (destination);//Create a message producerSendMessage (session, MessageProducer);//Send MessageSession.commit (); } Catch(Exception e) {//TODO auto-generated Catch blockE.printstacktrace (); } finally{ if(connection!=NULL){ Try{connection.close (); } Catch(jmsexception e) {//TODO auto-generated Catch blockE.printstacktrace (); } } } } /*** Send Message *@paramSession *@paramMessageProducer *@throwsException*/ Public Static voidSendMessage (Session session,messageproducer messageproducer)throwsexception{ for(inti=0;i<jmsproducer.sendnum;i++) {TextMessage message=session.createtextmessage ("Message sent by ActiveMQ" +i); System.out.println ("Send message:" + "ActiveMQ messages sent" +i); Messageproducer.send (message); } }}
Spring JMS and Activemq initial knowledge