Activemq and Lingo implement JMS and asynchronous calls

Source: Internet
Author: User

Jms is a frequently used technology in projects, and activemq is an excellent open-source jms product. When using JMS to process related services, all users construct messages and send them to the queue. Finally, they use the message listener to listen to the messages and analyze and process the messages. In this process, two steps are troublesome and repetitive, that is, constructing messages and splitting messages. In addition, this is contrary to the object-oriented thinking. If this is done, it is clearly message-oriented, rather than object-oriented. For the above reasons, I would like to introduce you to a practical open-source framework in the project: lingo.

  

The main function of lingo in JMS is to encapsulate messages, so that you don't have to worry about message construction and splitting, but you only need to care about your own business logic. I will give you an example of using activemq to directly implement and using lingo. In this example, I used spring framework.

  

==================================== Hello. java =

  

Import java. io. Serializable;

  

/** Hello. java is used to pass JAVA objects

* Author: cjp

* Date: 2005-11-8

* Time: 22:24:02

*/

Public class Hello implements Serializable {

Private String id;

Private Hello;

Private PointList pointList;

Public String getId (){

Return id;

}

  

Public void setId (String id ){

This. id = id;

}

  

Public Hello getHello (){

Return hello;

}

  

Public void setHello (Hello hello ){

This. hello = hello;

}

}

  

======================================= SpringTest. java ====================================

  

Import org. springframework. jms. core. JmsTemplate;

Import org. springframework. jms. core. MessageCreator;

Import org. springframework. test. AbstractDependencyInjectionSpringContextTests;

  

Import javax. jms .*;

  

/**

  

* Send a JMS message

  

*/

Public class SpringTest extends actdependencyinjectionspringcontexttests

{

Protected String [] getConfigLocations ()

{

Return new String [] {"file: D: \ wosame \ test \ com \ wosame \ room \ jms. xml "};

}

  

Public void testSendMessage () throws Exception

{

JmsTemplate jmsTemplate = (JmsTemplate) applicationContext. getBean ("jmsTemplate ");

JmsTemplate. send (new MessageCreator ()

{

Public Message createMessage (Session session) throws JMSException

{

ObjectMessage message = session. createObjectMessage ();

Hello hello = new Hello ();

Hello. setId ("test ");

Message. setObject (hello );

Return message;

}

});

}

}

  

================================ HelloMDP. java ========================================

  

/**

  

Process JMS messages

  

*/

  

Import org. apache. commons. logging. Log;

Import org. apache. commons. logging. LogFactory;

Import javax. jms .*;

  

Public class HelloMDP implements MessageListener

{

Protected Log log = LogFactory. getLog (HelloMDP. class );

  

Public void onMessage (Message message)

{

Try

{

ObjectMessage objMessage = (ObjectMessage) message;

Hello hello = (Hello) objMessage. getObject ();

System. out. println ("hello. getId () =" + hello. getId ());

} Catch (JMSException e)

{

Log. error ("Parse failed", e );

}

}

}

  

====================================== Jms. xml =

  

  

Http://www.springframework.org/dtd/spring-beans.dtd>

  

  

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.