ActiveMQ in Action-7.2 Embedding ActiveMQ using Spring- 高飞网

7.2 Embedding ActiveMQ using Spring

——用Spring嵌入ActiveMQ

2016-06-07 15:19:24.0

7.2 用Spring嵌入ActiveMQ

    ActiveMQ研发是就考虑了使用Spring。事实上,ActiveMQ默认情况下使用了SpringXml配置文件。这使得将ActiveMQ嵌入到使用Spring的应用中更为容易。本节将介绍多种利用Spring使用ActiveMQ的方式。即使每种方式都有其优缺点,有的方式比另外的方式更受青睐,也没有一种方式是最好的。决定使用哪种方式需要考虑到你的应用本身和系统架构,你的开发团队的能力,以及部署到生产环境后系统的维护难度。

7.2.1 纯Spring Xml

    默认情况下,ActiveMQ使用Spring和Apache XBean(http://geronimo.apache.org/xbean/)来做初始化工作。因此前面几章中使用的多种ActiveMQ特性的配置,都是基于Spring配置文件,同时又受XBean自定义XML模式所驱动。XBean提供了定义和使用定制化XML语法的能力,相比于Spring的XML语法更为简洁。即使现在Spring也提供了这项能力,在ActiveMQ创建时这些特性也并不可用(当时Spring只支持DTD)。

    下面的清单展示了一个简单的Java应用,使用Spring和XBean。

清单7.10 XBeanBroker类
package org.apache.activemq.book.ch7.xbean;

import org.apache.activemq.book.ch6.Publisher;
import org.apache.xbean.spring.context.FileSystemXmlApplicationContext;

public class XBeanBroker {

    public static void main(String[] args) throws Exception {
        if (args.length == 0) {
            System.err.println("Please define a configuration file!");
            return;
        }
        String config = args[0];
        System.out.println("Starting broker with the following configuration: " + config);
        System.setProperty("activemq.base", System.getProperty("user.dir"));

        FileSystemXmlApplicationContext context = new FileSystemXmlApplicationContext(config);

        Publisher publisher = new Publisher();
        for (int i = 0; i < 100; i++) {
            publisher.sendMessage(new String[] { "JAVA", "IONA" });
        }

    }

}

    清单7.10接受一个输入参数,作为XML配置文件的路径,而后设置activemq.base系统变量,再用配置文件初始化Spring应用上下文。然后把Publisher实例化,并用它发送100次简单消息。这就是整个应用。请注意类中使用的应用上下文,是XBean中的,而不是Spring框架里的。下面是运行该示例的命令:

清单7.11 使用XBean和Spring启动ActiveMQ
$ mvn exec:java \
-Dexec.mainClass=org.apache.activemq.book.ch7.xbean.XBeanBroker \
-Dlog4j.configuration=file:src/main/java/log4j.properties \
-Dexec.args= \
"src/main/resources/org/apache/activemq/book/ch6/activemq-simple.xml"
...
[INFO] [exec:java {execution: default-cli}]
Starting broker with the following configuration:
src/main/resources/org/apache/activemq/book/ch6/activemq-simple.xml
INFO | Using Persistence Adapter:
AMQPersistenceAdapter(data/localhost)
INFO | AMQStore starting using directory: data/localhost
INFO | Kaha Store using data directory data/localhost/kr-store/state
INFO | Active data files: []
INFO | ActiveMQ 5.4.1 JMS Message Broker (localhost) is starting
INFO | For help or more information please see:
http://activemq.apache.org/
INFO | Kaha Store using data directory data/localhost/kr-store/data
INFO | JMX consoles can connect to
service:jmx:rmi:///jndi/rmi://localhost:1099/jmxrmi
INFO | Listening for connections at: tcp://localhost:61616
INFO | Connector openwire Started
INFO | ActiveMQ JMS Message Broker
(localhost, ID:dejanb-65363-1269596340878-0:0) started
Sending: {price=53.794098159875, stock=IONA, offer=53.847892258035,
up=false}
on destination: topic://STOCKS.IONA
Sending: {price=53.489740886575, stock=IONA, offer=53.543230627461,
up=false}
on destination: topic://STOCKS.IONA
Sending: {price=53.5342708859, stock=IONA, offer=53.58780515680,
up=true}
on destination: topic://STOCKS.IONA
Sending: {price=53.86122035252, stock=IONA, offer=53.91508157288,
up=true}
on destination: topic://STOCKS.IONA
Sending: {price=54.15343454330, stock=IONA, offer=54.207587977851,
up=true}
on destination: topic://STOCKS.IONA
Sending: {price=49.27384513708, stock=JAVA, offer=49.323118982218,
up=false}
on destination: topic://STOCKS.JAVA
Sending: {price=53.83373859262, stock=IONA, offer=53.8875723312,
up=false}
on destination: topic://STOCKS.IONA
Sending: {price=53.933391780045, stock=IONA, offer=53.98732517182,
up=true}
on destination: topic://STOCKS.IONA
...

    如上,服务端启动的状况与之前的一样。

    另外,XBean提供的简洁的XML语法,Spring也支持XML命名空间。

7.2.4 使用Spring自定义命名空间

    Spring框架近来所有的版本,都允许开发着使用自定义XML模式。ActiveMQ使用Spring配置文件,提供了自定义命名模式来配置ActiveMQ。下面的清单中演示了如何利用XML命名空间,使用定制的Spring模式配置ActiveMQ:

清单7.12 使用Spring2.x(或更新)的ActiveMQ XML配置
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:amq="http://activemq.apache.org/schema/core" 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-2.0.xsd
http://activemq.apache.org/schema/core
http://activemq.apache.org/schema/core/activemq-core.xsd">
    <amq:broker brokerName="localhost" dataDirectory="${activemq.base}/data">
        <amq:transportConnectors>
            <amq:transportConnector name="openwire" uri="tcp://localhost:61616" />
        </amq:transportConnectors>
        <amq:plugins>
            <amq:simpleAuthenticationPlugin>
                <amq:users>
                    <amq:authenticationUser username="admin" password="password"
                        groups="admins,publishers,consumers" />
                    <amq:authenticationUser username="publisher" password="password"
                        groups="publishers,consumers" />
                    <amq:authenticationUser username="consumer" password="password" groups="consumers" />
                    <amq:authenticationUser username="guest" password="password" groups="guests" />
                </amq:users>
            </amq:simpleAuthenticationPlugin>
        </amq:plugins>
    </amq:broker>
</beans>

    正如你在清单7.12中看到的,首先贯穿整个XML文件使用的一个声明了的前缀,用以引用自定义模式中的元素类型。这个ActiveMQ常用的前缀就是amq。其次是前缀右边的url,本例中是http://activemq.apache.org/schema/core。前缀通常引用XML文档中的URI。第三,URI作为一个标识符,通过schemaLocation属性,指向真实的XML模式文档(XSD)。例如,当使用<amq:broker>元素时,amq前缀作为一个URI的别名,而URI指向了发现服务端元素的XSD。

    一旦XML命名空间声明以后,我们自由地利用自定义XML语法定义服务端关联的Bean。在这个特定的例子中,我们已经像第6章那样地配置了服务端,使之使用上了简单的验证插件。现在Spring服务端可以像下面一样启动了。

清单7.13 启动使用自定义命名空间配置的ActiveMQ
$ mvn -e exec:java \
-Dexec.mainClass=org.apache.activemq.book.ch7.spring.SpringBroker \
-Dlog4j.configuration=file:src/main/java/log4j.properties \
-Dexec.args=\
src/main/resources/org/apache/activemq/book/ch7/spring-2.0.xml
...
[INFO] [exec:java {execution: default-cli}]
Starting broker with the following configuration:
src/main/resources/org/apache/activemq/book/ch7/spring-2.0.xml
INFO | Using Persistence Adapter:
AMQPersistenceAdapter(${activemq.base}/data/localhost)
INFO | AMQStore starting using directory: data/localhost
INFO | Kaha Store using data directory data/localhost/kr-store/state
INFO | Active data files: []
INFO | ActiveMQ 5.4.1 JMS Message Broker (localhost) is starting
INFO | For help or more information please see: http://activemq.apache.org/
INFO | Kaha Store using data directory data/localhost/kr-store/data
INFO | JMX consoles can connect
to service:jmx:rmi:///jndi/rmi://localhost:1099/jmxrmi
INFO | Listening for connections at: tcp://localhost:61616
INFO | Connector openwire Started
INFO | ActiveMQ JMS Message Broker
(localhost, ID:dejanb-65324-1269595874364-0:0) started
Sending: {price=83.53568740848, stock=IONA, offer=83.61922309589, up=true}
on destination: topic://STOCKS.IONA
Sending: {price=84.15670625187, stock=IONA, offer=84.24086295812, up=true}
on destination: topic://STOCKS.IONA
Sending: {price=83.64752134809, stock=IONA, offer=83.7311688694, up=false}
on destination: topic://STOCKS.IONA
Sending: {price=83.33023218494, stock=IONA, offer=83.41356241712, up=false}
on destination: topic://STOCKS.IONA
Sending: {price=84.05476877613, stock=IONA, offer=84.13882354490, up=true}
on destination: topic://STOCKS.IONA
Sending: {price=57.75764610250, stock=JAVA, offer=57.815403748606, up=true}
on destination: topic://STOCKS.JAVA
Sending: {price=84.3813034823, stock=IONA, offer=84.46568478585, up=true}
on destination: topic://STOCKS.IONA
Sending: {price=84.77874758495, stock=IONA, offer=84.86352633253, up=true}
on destination: topic://STOCKS.IONA

   清单7.13中的示例,使用了最通用的XML配置文件类型,即使它没有什么新特点,对于大多数开发者来讲,仍然不容易完全理解。

    现在我们已经讲解和演示了多种配置ActiveMQ例子。是时候加快步伐看一下ActiveMQ的通常的使用了。创建一个应用,使用通常的JMS范式:request/repply。