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>