субота, 3 січня 2015 р.

JMS (from my old report)

You can see original slides in http://www.slideshare.net/mybbslides/jms-14767911

General concepts



Message types

JMS specification provides standard implementations of javax.jms.Message:
1. javax.jms.BytesMessage - array of bytes
2. javax.jms.MapMessage - key-value pairs
3. javax.jms.ObjectMessage - serialized Java object
4. javax.jms.StreamMessage - stream of Java primitive values
5. javax.jms.TextMessage - Java String object (used for XML)

JMS working algorithm

1. Obtain a javax.jms.ConnectionFactory using JNDI lookup. (ConnectionFactory
names are different depending on JMS provider.)
2. Obtain a javax.jms.Connection from the ConnectionFactory.
3. Obtain a javax.jms.Session from the Connection.
4. Create a javax.jms.MessageProducer or javax.jms.MessageConsumer from the Session.
5. Send a message for the MessageProducer, or receive a message for the MessageConsumer
(synchronous) or set a message listener (asynchronous).
6. Start the Connection to start message delivery.
7. Finally close the Connection.

Synchronous messaging

while(true) {
    Message message = consumer.receive();

Asynchronous messaging (listener class)

class AsyncConsumerMessageListener implements MessageListener {
    public void onMessage(Message message) {
        TextMessage msg = (TextMessage) message;

Asynchronous messaging (listener class usage)

AsyncConsumerMessageListener asyncConsumer = new AsyncConsumerMessageListener();
MessageConsumer consumer = session.createConsumer(destination);

Spring amqConnectionFactory

<bean class="org.apache.activemq.ActiveMQConnectionFactory" id="amqConnectionFactory">
    <property name="brokerURL" value="${jms.url}"/>

1) jms.url=tcp://localhost:61616
2) jms.url=vm://localhost

Spring jmsQueueConnectionFactory

<bean id="jmsQueueConnectionFactory" class="org.springframework.jms.connection.SingleConnectionFactory">
    <property name="targetConnectionFactory" ref="amqConnectionFactory"/>

Spring listener-container

<jms:listener-container connection-factory="jmsQueueConnectionFactory" destination-type="queue" container-type="default">
    <jms:listener destination="TESTQUEUE" method="onMessage" ref="webConsumer"/>

Spring jmsTemplate

<bean class="org.springframework.jms.core.JmsTemplate" id="jmsTemplate">
    <constructor-arg ref="jmsQueueConnectionFactory"/>

Spring message listener

public class WebConsumer implements MessageListener
    public void onMessage( final Message message ) {...}

Spring producer

public class WebProducer
    private JmsTemplate jmsTemplate;

    public void send(String message) throws JMSException
        jmsTemplate.convertAndSend("TESTQUEUE", message);

Sources and useful links

1. Official JMS tutorial book
2. http://www.thirdeyeconsulting.com/indyjug/jms/jms.html
3. http://www2.sys-con.com/itsg/virtualcd/Java/archives/0604/chappell/index.html

Немає коментарів:

Дописати коментар