Spring配置ActiveMQ和Tibco EMS

目的

如何使用Spring配置ActiveMQ和Tibco EMS(TIBCO Enterprise For JMS)。

Preparation

  • Maven
  • Spring Framework
  • ActiveMQ or Tibco EMS

ActiveMQ

  1. 添加spring-jms和ActiveMQ的依赖。

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-jms</artifactId>
    <version>4.3.1.RELEASE</version>
    </dependency>
    <dependency>
    <groupId>org.apache.activemq</groupId>
    <artifactId>activemq-core</artifactId>
    <version>5.7.0</version>
    </dependency>
  2. applicationContext.xml配置。
    ActiveMQ地址: tcp://localhost:61616
    监听队列: garfield
    监听类: com.garfieldwiki.jms.QueueConsumer

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    <?xml version="1.0" encoding="UTF-8"?>
    <beans
    xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="targetConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory">
    <property name="brokerURL" value="tcp://localhost:61616"/>
    </bean>

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

    <bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
    <property name="connectionFactory" ref="connectionFactory"/>
    </bean>

    <bean id="queueDestination" class="org.apache.activemq.command.ActiveMQQueue">
    <constructor-arg>
    <value>garfield</value>
    </constructor-arg>
    </bean>

    <bean id="queueConsumer" class="com.garfieldwiki.jms.QueueConsumer"/>

    <bean id="jmsContainer"
    class="org.springframework.jms.listener.DefaultMessageListenerContainer">
    <property name="connectionFactory" ref="connectionFactory" />
    <property name="destination" ref="queueDestination" />
    <property name="messageListener" ref="queueConsumer" />
    </bean>

    </beans>
  3. com.garfieldwiki.jms.QueueConsumer,该类继承了MessageListener,监听队列的消息。

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    package com.garfieldwiki.jms;

    import java.util.logging.Logger;

    import javax.jms.JMSException;
    import javax.jms.Message;
    import javax.jms.MessageListener;
    import javax.jms.TextMessage;

    public class QueueConsumer implements MessageListener {

    private static final Logger LOGGER = Logger.getLogger(QueueConsumer.class.getName());

    public void onMessage(Message message) {
    try {
    TextMessage textMsg = (TextMessage) message;
    LOGGER.info("Receive a message from garfield queue: " + textMsg.getText());
    } catch (JMSException e) {
    LOGGER.warning(e.getMessage());
    }
    }

    }
  4. com.garfieldwiki.jms.QueueProducer,该类可以发送消息到队列,使用JmsTemplate进行消息发送。

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    package com.garfieldwiki.jms;

    import java.util.logging.Logger;

    import javax.jms.JMSException;
    import javax.jms.Message;
    import javax.jms.Queue;
    import javax.jms.Session;

    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.beans.factory.annotation.Qualifier;
    import org.springframework.jms.core.JmsTemplate;
    import org.springframework.jms.core.MessageCreator;
    import org.springframework.stereotype.Component;

    @Component
    public class QueueProducer {

    private static final Logger LOGGER = Logger.getLogger(QueueProducer.class.getName());

    @Autowired
    private JmsTemplate jmsTemplate;

    @Autowired
    @Qualifier("queueDestination")
    private Queue queue;

    public void sendMessage() {
    this.jmsTemplate.send(this.queue, new MessageCreator() {
    public Message createMessage(Session session) throws JMSException {
    String msg = "Hello, I am Garfield.";
    LOGGER.info("Send message to garfield queue: " + msg);
    return session.createTextMessage(msg);
    }
    });
    }

    }

Tibco EMS

  1. 引入必要的jar包jms-2.0.jartibjms.jar,可在Tibco的安装目录寻找C:\tibco\ems\8.3\lib

  2. 项目只需要引入spring-jms即可。

    1
    2
    3
    4
    5
    <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-jms</artifactId>
    <version>4.3.1.RELEASE</version>
    </dependency>
  3. applicationContext.xml配置跟ActiveMQ有不同,主要是相关实现类的命名方式不一样。

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    <?xml version="1.0" encoding="UTF-8"?>
    <beans
    xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context.xsd
    http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="targetConnectionFactory" class="com.tibco.tibjms.TibjmsConnectionFactory">
    <property name="serverUrl" value="tcp://localhost:7222"/>
    <property name="userName" value=""/>
    <property name="userPassword" value=""/>
    </bean>

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

    <bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
    <property name="connectionFactory" ref="connectionFactory"/>
    </bean>

    <bean id="queueDestination" class="com.tibco.tibjms.TibjmsQueue">
    <constructor-arg>
    <value>garfield</value>
    </constructor-arg>
    </bean>

    <bean id="queueConsumer" class="com.garfieldwiki.jms.QueueConsumer"/>

    <bean id="jmsContainer"
    class="org.springframework.jms.listener.DefaultMessageListenerContainer">
    <property name="connectionFactory" ref="connectionFactory" />
    <property name="destination" ref="queueDestination" />
    <property name="messageListener" ref="queueConsumer" />
    </bean>

    </beans>
  4. Tibco EMS消息的生产和消费跟ActiveMQ一样。

参考资料
Spring JMS
ActiveMQ Spring Support
Tibco EMS Document
Maven ActiveMQ