在普通Java程序(我指的是非Spring等)中加载ActiveMQ配置有两种方式:使用配置文件和不使用配置文件。
1. 使用配置文件
首先编写配置文件,我通常会把src目录删除,然后另建两个source foulder,一个为src/java,用于放置源代码;一个为src/config,用于放置配置文件,如下图所示:
所以我把ActiveMQ的配置文件activemq.xml放置在src/config下面,而在src/java中添加包和类,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">
<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>
</beans>
和它对应的类如下所示:
/**
*
* @author geloin
* @date 2012-9-12 下午2:19:29
*/
package com.geloin.activemq.test2;
import java.net.URI;
import org.apache.activemq.broker.BrokerFactory;
import org.apache.activemq.broker.BrokerService;
/**
*
* @author geloin
* @date 2012-9-12 下午2:19:29
*/
public class MyConfiguration2 {
/**
*
* @author geloin
* @date 2012-9-12 下午2:19:29
* @param args
*/
public static void main(String[] args) throws Exception {
System.setProperty("activemq.base",System.getProperty("user.dir"));
String configUri = "xbean:/activemq.xml";
URI brokerUri = new URI(configUri);
BrokerService broker = BrokerFactory.createBroker(brokerUri);
broker.start();
System.out.println("\nPress any key to stop the broker\n");
System.in.read();
}
}
你会看到在activemq.xml文件中使用了一个变量${activemq.base},所以在MyConfiguration2中我们首先就给activemq.base赋值,如下所示:
System.setProperty("activemq.base",System.getProperty("user.dir"));
这样以后使用URI方式获取配置文件时,就可以顺便为其变量赋值。
2. 不使用配置文件,也就是硬代码方式,代码如下所示:
/**
*
* @author geloin
* @date 2012-9-12 下午1:38:54
*/
package com.geloin.activemq.test2;
import java.util.ArrayList;
import java.util.List;
import org.apache.activemq.broker.BrokerPlugin;
import org.apache.activemq.broker.BrokerService;
import org.apache.activemq.security.AuthenticationUser;
import org.apache.activemq.security.SimpleAuthenticationPlugin;
/**
*
* @author geloin
* @date 2012-9-12 下午1:38:54
*/
public class MyConfiguration {
/**
*
* @author geloin
* @date 2012-9-12 下午1:38:57
* @param args
*/
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("\nPress any key to stop the broker\n");
System.in.read();
}
}
对照配置文件和此处的硬代码,你会发现它们是一一对应的。
使用硬代码方式时,需要注意两点:
(1) plugins必须在connector前设置,即先broker.setPlugins,再broker.addConnector,位置不得错乱,否则初始化失败;
(2) 在start之前,得先把plugins和connector设置好,否则也会导致初始化失败。