在大多数通讯拓扑结构图中都有分JMS Brokers (服务端) 和JMS客户端。但有时会把消息中间件部署到你的jvm内部也是有道理的。This allows you to optimise away a network hop;让JMS的网络调用象RMI同样高效,但又拥有一般JMS的特性好比位置独立性,可靠性,负载均衡等。html
根据你实际使用Java,Spring,XBean或ActiveMQConnectionFactory 的状况有许多种途径能够达到内嵌消息中间件。java
下面的java代码会建立一个内嵌的消息中间件
web
BrokerService broker = new BrokerService(); // configure the broker broker.addConnector("tcp://localhost:61616"); broker.start();
If you want to lazily bind the transport connector as part of start(), useful when start() will block pending a store lock (as in a slave start),你可使用下面的代码spring
BrokerService broker = new BrokerService(); TransportConnector connector = new TransportConnector(); connector.setUri(new URI("tcp://localhost:61616")); broker.addConnector(connector); broker.start();
在同一个JVM内部的客户端可以使用vm:// transport这样的方式链接到消息中间件-同时外部的客户端可使用tcp:// protocol方式来链接消息中间件。apache
若是你有一个以上的内嵌的消息中间件,要确保你给它们每一个实例起了一个惟一的名字,好比下面这样:api
BrokerService broker = new BrokerService(); // configure the broker broker.setBrokerName("fred"); broker.addConnector("tcp://localhost:61616"); broker.start();
而后若是你想在同一个jvm内部链接到一个名叫'fred'的消息中间件,你可使用url vm://fred网络
也有可能使用应用代码完整的配置一个消息中间件,好比下面这样:负载均衡
BrokerService broker = new BrokerService(); broker.setBrokerName("fred"); broker.setUseShutdownHook(false); //Add plugin broker.setPlugins(new BrokerPlugin[]{new JaasAuthenticationPlugin()}); //Add a network connection NetworkConnector connector = answer.addNetworkConnector("static://"+"tcp://somehost:61616"); connector.setDuplex(true); broker.addConnector("tcp://localhost:61616"); broker.start();
注:请注意你要把设置插件的代码放在链接前面不然它不会被初始化
更多关于可用的属性的细节,请参见BrokerService javadocjvm
有一个叫BrokerFactory的辅助类能够经过URL的配置方式来建立一个消息中间件
maven
BrokerService broker = BrokerFactory.createBroker(new URI(someURI));
URI可用的值以下
URI scheme | 例子 |
描述 |
xbean: | xbean:activemq.xml | 在clpassth(和文件系统)中搜索URI中给出的xml文件(这边是activemq.xml),而后把他看成xml配置 |
broker: | broker:tcp://localhost:61616 | 使用 Broker Configuration URI的方式来配置中间件 |
有一个factory bean可以指向外部的ActiveMQ XML配置文件
<bean id="broker" class="org.apache.activemq.xbean.BrokerFactoryBean"> <property name="config" value="classpath:org/apache/activemq/xbean/activemq.xml" /> <property name="start" value="true" /> </bean>
在这个例子中使用的是普通的Spring'classpath:org/apache/activemq/xbean/activemq.xml'资源文件,因此要确保这个activemq.xml文件在你的工程目录下的clpsspath目录下能找到'org/apache/activemq/xbean/activemq.xml'这个文件。固然你也可使用其它的你喜欢的文件路径。好比使用:classpath:activemq.xml若是你把配置文件直接放在了classpqth的根目录下,象web工程中的WEB-INF/classes。
也行你但愿你可以使用URL的方式来代替file:* or *http: 这样的前缀。更多的细节能够参考 Spring deals with resources
若是你已经在使用XBean你能够混合使用你的 Spring/XBean xml配置和ActiveMQ的配置。
<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"> <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"/> <broker useJmx="true" xmlns="http://activemq.apache.org/schema/core"> <persistenceFactory> <kahaDB directory="${basedir}/target" /> </persistenceFactory> <transportConnectors> <transportConnector uri="tcp://localhost:61636" /> </transportConnectors> </broker> </beans>
若是你正在使用Spring 2.0和ActiveMQ 4.1及之后的版本(和xbean-spring 2.5及之后的版本)你能够直接把ActiveMQ的配置放到任何合法的Spring.xml文件中而不须要上面的factory bean。例以下面就是一个例子:
<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"> <!-- lets create an embedded ActiveMQ Broker --> <amq:broker useJmx="false" persistent="false"> <amq:transportConnectors> <amq:transportConnector uri="tcp://localhost:0" /> </amq:transportConnectors> </amq:broker> <!-- ActiveMQ destinations to use --> <amq:queue id="destination" physicalName="org.apache.activemq.spring.Test.spring.embedded"/> <!-- JMS ConnectionFactory to use, configuring the embedded broker using XML --> <amq:connectionFactory id="jmsFactory" brokerURL="vm://localhost"/> <!-- Spring JMS Template --> <bean id="myJmsTemplate" class="org.springframework.jms.core.JmsTemplate"> <property name="connectionFactory"> <!-- lets wrap in a pool to avoid creating a connection per send --> <bean class="org.springframework.jms.connection.SingleConnectionFactory"> <property name="targetConnectionFactory"> <ref local="jmsFactory" /> </property> </bean> </property> </bean> <bean id="consumerJmsTemplate" class="org.springframework.jms.core.JmsTemplate"> <property name="connectionFactory" ref="jmsFactory"/> </bean> <!-- a sample POJO which uses a Spring JmsTemplate --> <bean id="producer" class="org.apache.activemq.spring.SpringProducer"> <property name="template"> <ref bean="myJmsTemplate"></ref> </property> <property name="destination"> <ref bean="destination" /> </property> <property name="messageCount"> <value>10</value> </property> </bean> <!-- a sample POJO consumer --> <bean id="consumer" class="org.apache.activemq.spring.SpringConsumer"> <property name="template" ref="consumerJmsTemplate"/> <property name="destination" ref="destination"/> </bean> </beans>
也能够经过使用ActiveMQConnectionFactory来建立一个内嵌的中间件而且使用一个vm connector象下面这样:
ActiveMQConnectionFactory cf = new ActiveMQConnectionFactory("vm://localhost?broker.persistent=false");
使用查询参数 "broker.<property>" 来配置消息中间件,这边的<property>属性必须与BrokerService中的相应的属性匹配。
消息中间件将会在第一次链接时立刻建立。
你能够经过设置create属性为假来关闭自动建立的功能:
ActiveMQConnectionFactory cf = new ActiveMQConnectionFactory("vm://localhost?create=false");