最近项目中需要集成消息中间件来处理消息,选择了ActiveMQ这款中间件的开源产品。由于项目用的应用服务器为JBOSS,所以自然就想到如何把两者结合使用。
在ActiveMQ官方网站中给出了答案,网站中用到的是
ActiveMQ4.0.1+JBOSS4.0.4+JDK1.5+ANT1.6.2,我们项目中用的是
jboss-5.1.0.GA+JDK1.7+ActiveMQ5.7.0另外用ANT1.8.4做的集成测试。
ActiveMQ和JBOSS集成地址:http://activemq.apache.org/integrating-apache-activemq-with-jboss.html
具体做法:
1. 安装JDK(不说了,too easy)
2. 安装JBOSS(解压就好)
3. 下载ActiveMQ的rar资源组件包【老版本的ActiveMQ在ActiveMQ压缩包中就有,新版本的压缩包中没有需要单独下载】(如何对J2EE的容器组件开发模型了解的都清楚,j2ee中有五个组件,其中有一个就是资源组件,我们要下载的ActiveMQ的rar文件就是一个资源组件):
https://repository.cloudera.com/content/groups/cdh-build/org/apache/activemq/activemq-rar/5.7.0/
4. 解压RAR拷贝到JBOSS的default/deploy中
5. 配置 activemq(1):修改 META-INF/ra.xml
- <?xmlversionxmlversion="1.0" encoding="UTF-8"?>
- <!--
- Licensed to the Apache Software Foundation(ASF) under one or more
- contributor license agreements. See the NOTICE file distributed with
- this work for additional informationregarding copyright ownership.
- The ASF licenses this file to You under theApache License, Version 2.0
- (the "License"); you may not usethis file except in compliance with
- the License. You may obtain a copy of the License at
- http://www.apache.org/licenses/LICENSE-2.0
- Unless required by applicable law or agreedto in writing, software
- distributed under the License isdistributed on an "AS IS" BASIS,
- WITHOUT WARRANTIES OR CONDITIONS OF ANYKIND, either express or implied.
- See the License for the specific languagegoverning permissions and
- limitations under the License.
- -->
- <connectorxmlnsconnectorxmlns="http://java.sun.com/xml/ns/j2ee"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
- http://java.sun.com/xml/ns/j2ee/connector_1_5.xsd"
- version="1.5">
- <description>ActiveMQ inbound and outbound JMSResourceAdapter</description>
- <display-name>ActiveMQ JMS ResourceAdapter</display-name>
- <vendor-name>activemq.org</vendor-name>
- <eis-type>JMS 1.1</eis-type>
- <resourceadapter-version>1.0</resourceadapter-version>
- <license>
- <description>
- Licensed to the Apache Software Foundation(ASF) under one or more
- contributor license agreements. See the NOTICE file distributed with
- this work for additional informationregarding copyright ownership.
- The ASF licenses this file to You under theApache License, Version 2.0
- (the "License"); you may not usethis file except in compliance with
- the License. You may obtain a copy of the License at
- http://www.apache.org/licenses/LICENSE-2.0
- Unless required by applicable law or agreedto in writing, software
- distributed under the License isdistributed on an "AS IS" BASIS,
- WITHOUT WARRANTIES OR CONDITIONS OF ANYKIND, either express or implied.
- See the License for the specific languagegoverning permissions and
- limitations under the License.
- </description>
- <license-required>true</license-required>
- </license>
- <resourceadapter>
- <resourceadapter-class>org.apache.activemq.ra.ActiveMQResourceAdapter</resourceadapter-class>
- <config-property>
- <description>
- The URL to the ActiveMQ serverthat you want this connection to connect to. If using
- an embedded broker, this valueshould be 'vm://localhost'.
- </description>
- <config-property-name>ServerUrl</config-property-name>
- <config-property-type>java.lang.String</config-property-type>
- <!--<config-property-value>tcp://localhost:61616</config-property-value>-->
- <config-property-value>vm://localhost</config-property-value>
- </config-property>
- <config-property>
- <description>The default username that will be used to establish connections to the ActiveMQserver.</description>
- <config-property-name>UserName</config-property-name>
- <config-property-type>java.lang.String</config-property-type>
- <config-property-value>defaultUser</config-property-value>
- </config-property>
- <config-property>
- <description>The defaultpassword that will be used to log the default user into the ActiveMQserver.</description>
- <config-property-name>Password</config-property-name>
- <config-property-type>java.lang.String</config-property-type>
- <config-property-value>defaultPassword</config-property-value>
- </config-property>
- <config-property>
- <description>The client idthat will be set on the connection that is established to the ActiveMQserver.</description>
- <config-property-name>Clientid</config-property-name>
- <config-property-type>java.lang.String</config-property-type>
- </config-property>
- <config-property>
- <description>Boolean to configureif outbound connections should reuse the inbound connection's session forsending messages.</description>
- <config-property-name>UseInboundSession</config-property-name>
- <config-property-type>java.lang.Boolean</config-property-type>
- <config-property-value>false</config-property-value>
- </config-property>
- <!-- NOTE disable the following propertyif you do not wish to deploy an embedded broker -->
- <config-property>
- <description>
- Sets the XML configuration file used toconfigure the embedded ActiveMQ broker via
- Spring if using embedded mode.
- BrokerXmlConfig is the filenamewhich is assumed to be on the classpath unless
- a URL is specified. So a value offoo/bar.xml would be assumed to be on the
- classpath whereasfile:dir/file.xml would use the file system.
- Any valid URL string issupported.
- </description>
- <config-property-name>BrokerXmlConfig</config-property-name>
- <config-property-type>java.lang.String</config-property-type>
- <!--<config-property-value></config-property-value>-->
- <config-property-value>xbean:broker-config.xml</config-property-value>
- <!--
- To use the broker-config.xml fromthe root for the RAR
- To use an external file or urllocation
- <config-property-value>xbean:file:///amq/config/jee/broker-config.xml</config-property-value>
- -->
- </config-property>
- <outbound-resourceadapter>
- <connection-definition>
- <managedconnectionfactory-class>org.apache.activemq.ra.ActiveMQManagedConnectionFactory</managedconnectionfactory-class>
- <connectionfactory-interface>javax.jms.ConnectionFactory</connectionfactory-interface>
- <connectionfactory-impl-class>org.apache.activemq.ra.ActiveMQConnectionFactory</connectionfactory-impl-class>
- <connection-interface>javax.jms.Connection</connection-interface>
- <connection-impl-class>org.apache.activemq.ra.ManagedConnectionProxy</connection-impl-class>
- </connection-definition>
- <connection-definition>
- <managedconnectionfactory-class>org.apache.activemq.ra.ActiveMQManagedConnectionFactory</managedconnectionfactory-class>
- <connectionfactory-interface>javax.jms.QueueConnectionFactory</connectionfactory-interface>
- <connectionfactory-impl-class>org.apache.activemq.ra.ActiveMQConnectionFactory</connectionfactory-impl-class>
- <connection-interface>javax.jms.QueueConnection</connection-interface>
- <connection-impl-class>org.apache.activemq.ra.ManagedConnectionProxy</connection-impl-class>
- </connection-definition>
- <connection-definition>
- <managedconnectionfactory-class>org.apache.activemq.ra.ActiveMQManagedConnectionFactory</managedconnectionfactory-class>
- <connectionfactory-interface>javax.jms.TopicConnectionFactory</connectionfactory-interface>
- <connectionfactory-impl-class>org.apache.activemq.ra.ActiveMQConnectionFactory</connectionfactory-impl-class>
- <connection-interface>javax.jms.TopicConnection</connection-interface>
- <connection-impl-class>org.apache.activemq.ra.ManagedConnectionProxy</connection-impl-class>
- </connection-definition>
- <transaction-support>XATransaction</transaction-support>
- <authentication-mechanism>
- <authentication-mechanism-type>BasicPassword</authentication-mechanism-type>
- <credential-interface>javax.resource.spi.security.PasswordCredential</credential-interface>
- </authentication-mechanism>
- <reauthentication-support>false</reauthentication-support>
- </outbound-resourceadapter>
- <inbound-resourceadapter>
- <messageadapter>
- <messagelistener>
- <messagelistener-type>javax.jms.MessageListener</messagelistener-type>
- <activationspec>
- <activationspec-class>org.apache.activemq.ra.ActiveMQActivationSpec</activationspec-class>
- <required-config-property>
- <config-property-name>destination</config-property-name>
- </required-config-property>
- <required-config-property>
- <config-property-name>destinationType</config-property-name>
- </required-config-property>
- </activationspec>
- </messagelistener>
- </messageadapter>
- </inbound-resourceadapter>
- <adminobject>
- <adminobject-interface>javax.jms.Queue</adminobject-interface>
- <adminobject-class>org.apache.activemq.command.ActiveMQQueue</adminobject-class>
- <config-property>
- <config-property-name>PhysicalName</config-property-name>
- <config-property-type>java.lang.String</config-property-type>
- </config-property>
- </adminobject>
- <adminobject>
- <adminobject-interface>javax.jms.Topic</adminobject-interface>
- <adminobject-class>org.apache.activemq.command.ActiveMQTopic</adminobject-class>
- <config-property>
- <config-property-name>PhysicalName</config-property-name>
- <config-property-type>java.lang.String</config-property-type>
- </config-property>
- </adminobject>
- </resourceadapter>
- </connector>
6. 配置 activemq (2):修改 broker-config.xml 文件
- <?xmlversionxmlversion="1.0" encoding="UTF-8"?>
- <!--
- Licensed to the Apache Software Foundation(ASF) under one or more
- contributor license agreements. See the NOTICE file distributed with
- this work for additional informationregarding copyright ownership.
- The ASF licenses this file to You under theApache License, Version 2.0
- (the "License"); you may not usethis file except in compliance with
- the License. You may obtain a copy of the License at
- http://www.apache.org/licenses/LICENSE-2.0
- Unless required by applicable law or agreedto in writing, software
- distributed under the License isdistributed on an "AS IS" BASIS,
- WITHOUT WARRANTIES OR CONDITIONS OF ANYKIND, either express or implied.
- See the License for the specific languagegoverning permissions and
- limitations under the License.
- -->
- <!--START SNIPPET: xbean -->
- <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/beanshttp://www.springframework.org/schema/beans/spring-beans-3.0.xsd
- http://activemq.apache.org/schema/corehttp://activemq.apache.org/schema/core/activemq-core.xsd">
- <!-- shutdown hook is disabled as RAR classloader may be gone at shutdown-->
- <brokerxmlnsbrokerxmlns="http://activemq.apache.org/schema/core"useJmx="true" useShutdownHook="false"brokerName="bruce.broker1">
- <managementContext>
- <!-- use appserver provided contextinstead of creating one,
- for jboss use:-Djboss.platform.mbeanserver -->
- <managementContextcreateConnectormanagementContextcreateConnector="false"/>
- </managementContext>
- <persistenceAdapter>
- <kahaDBdirectorykahaDBdirectory="activemq-data/kahadb"/>
- <!--<jdbcPersistenceAdapterdataSource="#oracle-ds"/>-->
- </persistenceAdapter>
- <transportConnectors>
- <transportConnectornametransportConnectorname="bruce.broker1" uri="tcp://localhost:61616"/>
- </transportConnectors>
- </broker>
- </beans>
7. 配置 jboss5,使得jboss5启动的时候能够启动activemq
新建 /default/deploy/activemq-ds.xml 文件,内容如下:
- <?xmlversionxmlversion="1.0" encoding="UTF-8"?>
- <!DOCTYPEconnection-factories
- PUBLIC "-//JBoss//DTD JBOSS JCA Config5.0//EN"
- "http://www.jboss.org/j2ee/dtd/jboss-ds_5_0.dtd">
- <connection-factories>
- <tx-connection-factory>
- <jndi-name>activemq/QueueConnectionFactory</jndi-name>
- <xa-transaction/>
- <track-connection-by-tx/>
- <rar-name>activemq-ra-5.7.0.rar</rar-name>
- <connection-definition>javax.jms.QueueConnectionFactory</connection-definition>
- <ServerUrl>vm://localhost</ServerUrl>
- <!--
- <UserName>sa</UserName>
- <Password></Password>
- -->
- <min-pool-size>1</min-pool-size>
- <max-pool-size>200</max-pool-size>
- <blocking-timeout-millis>30000</blocking-timeout-millis>
- <idle-timeout-minutes>3</idle-timeout-minutes>
- </tx-connection-factory>
- <tx-connection-factory>
- <jndi-name>activemq/TopicConnectionFactory</jndi-name>
- <xa-transaction/>
- <track-connection-by-tx/>
- <rar-name>activemq-ra-5.7.0.rar</rar-name>
- <connection-definition>javax.jms.TopicConnectionFactory</connection-definition>
- <ServerUrl>vm://localhost</ServerUrl>
- <!--
- <UserName>sa</UserName>
- <Password></Password>
- -->
- <min-pool-size>1</min-pool-size>
- <max-pool-size>200</max-pool-size>
- <blocking-timeout-millis>30000</blocking-timeout-millis>
- <idle-timeout-minutes>3</idle-timeout-minutes>
- </tx-connection-factory>
- <mbeancodembeancode="org.jboss.resource.deployment.AdminObject"name="activemq.queue:name=outboundQueue">
- <attributenameattributename="JNDIName">activemq/queue/outbound</attribute>
- <dependsoptional-attribute-namedependsoptional-attribute-name="RARName">jboss.jca:service=RARDeployment,name='activemq-ra-5.7.0.rar'</depends>
- <attributenameattributename="Type">javax.jms.Queue</attribute>
- <attributenameattributename="Properties">PhysicalName=queue.outbound</attribute>
- </mbean>
- <mbeancodembeancode="org.jboss.resource.deployment.AdminObject" name="activemq.topic:name=inboundTopic">
- <attributenameattributename="JNDIName">activemq/topic/inbound</attribute>
- <dependsoptional-attribute-namedependsoptional-attribute-name="RARName">jboss.jca:service=RARDeployment,name='activemq-ra-5.7.0.rar'</depends>
- <attributenameattributename="Type">javax.jms.Topic</attribute>
- <attributenameattributename="Properties">PhysicalName=topic.inbound</attribute>
- </mbean>
- </connection-factories>
8. 启动 jboss5
9. 验证集成成功与否
a) 安装ANT
b) 配置ANT环境变量
c) 启动JBOSS
d) 打开一个命令窗口,输入
cd /apache-activemq-5.3.1/example
ant consumer
e) 打开另外一个命令窗口,输入:
cd /apache-activemq-5.3.1/example
ant consumer
10. 运行成功