7.1使用Java嵌入ActiveMQ

虽然今天的大多数开发者使用一个框架编写他们的应用,但是用最简单的Java总是好的。在这一节我们使用ActiveMQ的Java APIs来initialize和configureActiveMQ。你将看到如何使用BrokerService类来配置代理,什么都不用,仅仅是纯Java。
然后我们将讨论你如何能使用定制的配置XML文件配置你的代理。我们将使用BrokerFactory类来实现它并且你将能使用regular配置文件将ActiveMQ嵌入到你的Java应用程序中。这节之后你将能在你的Java应用中通过任何配置将ActiveMQ嵌入其中。
7.1.1使用BrokerService嵌入ActiveMQ
当使用纯Java建立你的代理,org.apache.activemq.broker.BrokerService 类是一个起点。这个类被用来配置代理和管理它的整个生命周期。该类的使用方法演示最要用一个示例。让我们以第6章中使用的代理配置类配置一个简单的authentication插件并来看看我们如何使用纯Java来实现相同的功能。对于初学者,让我们看看熟知的XML配置示例吧:
Listing 7.1 Configure ActiveMQ with security plug-ins using XML
<broker xmlns="http://activemq.apache.org/schema/core" brokerName="myBroker" dataDirectory="${activemq.base}/data">
<transportConnectors>
<transportConnector name="openwire" uri="tcp://localhost:61616" />
</transportConnectors>
<plugins>
<simpleAuthenticationPlugin>
<users>
<authenticationUser username="admin" password="password" groups="admins,publishers,consumers"/>
<authenticationUser username="publisher" password="password" groups="publishers,consumers"/>
<authenticationUser username="consumer" password="password" groups="consumers"/>
<authenticationUser username="guest" password="password" groups="guests"/>
</users>
</simpleAuthenticationPlugin>
</plugins>
</broker>

表7.1使用了基本的ActiveMQ的XML来定义一个代理实例,定义了一个名字和数据目录还有transport connector和一个插件。现在看看下面使用纯Java和BrokerService的配置,如下所示:

Listing 7.2 Configure ActiveMQ with security plug-ins using Java

public static void main(String[] args) throws Exception {
  BrokerService broker = new BrokerService();
  broker.setBrokerName("myBroker");
  broker.setDataDirectory("data/");
  SimpleAuthenticationPlugin authentication = new SimpleAuthenticationPlugin();
  List<AuthenticationUser> users = new ArrayList<AuthenticationUser>();
  users.add(new AuthenticationUser("admin","password","admins,publishers,consumers"));
  users.add(new AuthenticationUser("publisher","password","publishers,consumers"));
  users.add(new AuthenticationUser("consumer","password","consumers"));
  users.add(new AuthenticationUser("guest","password","guests"));
  authentication.setUsers(users);
  broker.setPlugins(new BrokerPlugin[]{authentication});
  broker.addConnector("tcp://localhost:61616");
  broker.start();
  System.out.println();
  System.out.println("Press any key to stop the broker");
  System.out.println();
  System.in.read();
}

如你所见,表7.2实例化了BrokerService并配置了brokerName和dataDirectory属性。下面SimpleAuthenticationPlugin通过setPlugins()方法被添加到BrokerService。然后一个transport connector通过addConnector()方法被添加到BrokerService。最后start()的方法被用来启动BrokerService实例。现在你完全使用纯Java初始化了代理;没有XML配置文件被用到。要看看该类的活动,执行如下的命令:

Listing 7.3 Run the pure Java example of the BrokerService
$ mvn exec:java \
-Dexec.mainClass=org.apache.activemq.book.ch7.broker.Broker \
-Dlog4j.configuration=file:src/main/java/log4j.properties
...
[INFO] [exec:java {execution: default-cli}]
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 | AMQPersistenceAdapter - 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 | Listening for connections at: tcp://localhost:61616
INFO | Connector tcp://localhost:61616 Started
INFO | JMX consoles can connect to
service:jmx:rmi:///jndi/rmi://localhost:1099/jmxrmi
INFO | ActiveMQ JMS Message Broker
(localhost, ID:dejanb-63935-1269536159457-0:0) started
Press any key to stop the broker
...

在表7.3中需要注意的重要的是你应该总在connectors之前添加你的插件;否则他们将不能被初始化。同样地,在代理启动后添加的任何connector将不会被适当地启动。
当你想使用纯Java类配置代理的时候BrokerService类很有用。如果你不需要一个极度定制化的配置,这个方法对许多情况都是有用的。在许多应用中,你会想要使用和配置单独ActiveMQ代理实例相同的配置文件来初始化代理。为了这个目的ActiveMQ提供了org.apache.activemq.broker.BrokerFactory类。
7.1.2使用BrokerFactory嵌入ActiveMQ
BrokerFactory类这个功能能使使用一个ActiveMQ URI建立代理实例变得很简单。基于代理URI scheme,BrokerFactory定位合适的工厂并使用它建立BrokerService类的实例。最被广泛使用的工厂类是XBeanBrokerFactory类并且通过简单地通过XBean-style的URI来配置。XBean broker URI的示例显示如下:
xbean:/path/to/activemq.xml
该示例URI告诉BrokerFactory去使用XBeanBrokerFactory和冒号后面的路径创建代理实例。
现在,让我们看看下面的列表。BrokerFactory能使用基本的ActiveMQ XML配置文件实例化BrokerService,如下所示:
public class Factory {
	public static void main(String[] args) throws Exception {
		System.setProperty("activemq.base", System.getProperty("user.dir"));
		String configUri = "xbean:target/classes/org/apache/activemq/book/ch6/activemq-simple.xml";
		URI brokerUri = new URI(configUri);
		BrokerService broker = BrokerFactory.createBroker(brokerUri);
		broker.start();
		System.out.println();
		System.out.println("Press any key to stop the broker");
		System.out.println();
		System.in.read();
	}
}

如你在表7.4所见,BrokerFactory.createBroker()方法使用了一个配置URI来建立BrokerService实例。注意在表7.4中用到的配置URI是xbean:URI scheme。这个告诉了代理工厂去在classpath或者文件系统中的其他什么地方搜索给定的XML配置文件。为了illustrate这个示例,下面是执行它的命令:

Listing 7.5 Run the example of the BrokerFactory
$ mvn exec:java \
-Dexec.mainClass=org.apache.activemq.book.ch7.broker.Factory \
-Dlog4j.configuration=file:src/main/java/log4j.properties
...
[INFO] [exec:java {execution: default-cli}]
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 | Listening for connections at: tcp://localhost:61616
INFO | Connector openwire Started
INFO | ActiveMQ JMS Message Broker
(localhost, ID:dejanb-65001-1269594442403-0:0) started
Press any key to stop the broker
...

你也能为完全通过配置URI执行的简单代理配置使用broker:URI scheme。看下面的URI示例:
broker:(tcp://localhost:61616,network:static:tcp://remotehost:61616)?persistent=false&useJmx=true
这个简单的URI包含了启动代理的足够的配置,包括network和transport connectors;持久化被禁用并且JMX被明确启用。更多的信息,请看ActiveMQ网站( http://mng.bz/FNos)上详细的URI说明信息。
如前面所提到的,大多数的Java开发者使用一些框架来构成他们的应用。因为Spring 框架( http://www.springframework.org/)是如今最流行的框架,让我们调查一下若要在Spring框架中像组件一样使用ActiveMQ该如何配置。

猜你喜欢

转载自flxchy4.iteye.com/blog/1741727
今日推荐