使用ActiveMQ创建Java应用程序

1. 将ActiceMQ嵌入至Java应用程序中

    (1) 嵌入ActiveMQ使用BrokerService

    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>

    Configure ActiveMQ with security plug-ins using Java:

public static void main(String[] args) throws Exception {

    //实例化并且配置BrokerService
    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
    broker.start();
   
    System.out.println();
    System.out.println("Press any key to stop the broker");
    System.out.println();
   
    System.in.read();
}

    说明:在使用BrokerService进行Broker配置时,需要先将plug-ins添加至broker,后添加connector;否则

    plug-ins是不能被初始化的;Broker启动以后所添加的Connector也是不会被启动的。

    (2) 嵌入ActiveMQ使用BrokerFactory

    使用最多的Factory 是XBeanBrokerFactory,XBeanBrokerFactory使用的URI模板为:

    xbean:/path/to/activemq.xml

public static void main(String[] args) throws Exception {
        System.setProperty("activemq.base", System.getProperty("user.dir"));
        BrokerService broker = BrokerFactory.createBroker(
                new URI("xbean: target/classes/org/apache/activemq/book/ch7/activemq-simple.xml"));
        broker.start();

        System.out.println();
        System.out.println("Press any key to stop the broker");
        System.out.println();

        System.in.read();
 }

    说明:配置中的xbean URI命名空间是用来告诉BrokerFactory去ClassPath 或文件系统中查找给定的配置文

    件,activemq-simple.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
    http://activemq.apache.org/camel/schema/spring http://activemq.apache.org/camel/schema/spring/camel-spring.xsd">

	<!-- Allows us to use system properties as variables in this configuration file -->
	<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer" />

	<broker xmlns="http://activemq.apache.org/schema/core"
		brokerName="localhost" dataDirectory="${activemq.base}/data">

		<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>

		<!-- The transport connectors ActiveMQ will listen to -->
		<transportConnectors>
			<transportConnector name="openwire" uri="tcp://localhost:61616" />
		</transportConnectors>
	</broker>

</beans>

2. 将ActiveMQ嵌入至Spring应用中

    (1) 使用纯粹的SpringXML配置

<beans>
	<bean id="admins" class="org.apache.activemq.security.AuthenticationUser">
		<constructor-arg index="0" value="admin" />
		<constructor-arg index="1" value="password" />
		<constructor-arg index="2" value="admins,publisher,consumers" />
	</bean>
	<bean id="publishers" class="org.apache.activemq.security.AuthenticationUser">
		<constructor-arg index="0" value="publisher" />
		<constructor-arg index="1" value="password" />
		<constructor-arg index="2" value="publisher,consumers" />
	</bean>
	<bean id="consumers" class="org.apache.activemq.security.AuthenticationUser">
		<constructor-arg index="0" value="consumer" />
		<constructor-arg index="1" value="password" />
		<constructor-arg index="2" value="consumers" />
	</bean>
	<bean id="guests" class="org.apache.activemq.security.AuthenticationUser">
		<constructor-arg index="0" value="guest" />
		<constructor-arg index="1" value="password" />
		<constructor-arg index="2" value="guests" />
	</bean>
	<bean id="simpleAuthPlugin" class="org.apache.activemq.security.SimpleAuthenticationPlugin">
		<property name="users">
			<util:list>
				<ref bean="admins" />
				<ref bean="publishers" />
				<ref bean="consumers" />
				<ref bean="guests" />
			</util:list>
		</property>
	</bean>
	<bean id="broker" class="org.apache.activemq.broker.BrokerService"
		init-method="start" destroy-method="stop">
		<property name="brokerName" value="myBroker" />
		<property name="persistent" value="false" />
		<property name="transportConnectorURIs">
			<list>
				<value>tcp://localhost:61616</value>
			</list>
		</property>
		<property name="plugins">
			<list>
				<ref bean="simpleAuthPlugin" />
			</list>
		</property>
	</bean>
</beans>

    (2) 使用BrokerFactoryBean配置:

<beans>
	<bean id="broker" class="org.apache.activemq.xbean.BrokerFactoryBean">
		<property name="config"
			value="file:src/main/resources/org/apache/activemq/book/ch6/activemq-simple.xml" />
		<property name="start" value="true" />
	</bean>
</beans>

    (3) Using Apache XBean with Spring:

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"});
    }       
}

    (4) 使用自定义的XML命名空间(amq)with Spring

<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">
		<!-- The transport connectors ActiveMQ will listen to -->
		<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>

你可能感兴趣的:(activemq)