This is my first post, sorry for poor formatting.
My Environment is Fedora 20 64-bit,JBoss EAP 6.2, Active MQ 5.9, Spring 4.0.0.Final,mavne 3.
I googled out for JMS Listener with the above technologies but i could not find any.
It has taken me many hours to work out and find the appropriate solution. So i'm sharing my experiences so that you can work out easily.
First we need to set up Active MQ 5.9 which is latest by writing this post.
First download it from Apache website Active MQ Download page and just unzip it.
you can start it by typing the command bin/activemq start
You can refer all the other things here as well.
Active MQ installation
Next install the Jboss EAP 6.2 and configure the adapter.
In standalone.xml write the following code
You can download that rar file from the following link.ActiveMQ adapter rar file
You can rename it and this name should be as in the above code snippet.
<archive> activemq-rar.rar </archive>
And then you can configure the spring-config.xml file as follows to listen the messages.
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:jms="http://www.springframework.org/schema/jms"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/jms http://www.springframework.org/schema/jms/spring-jms-3.0.xsd">
<!-- A JMS connection factory for ActiveMQ -->
<bean id="connectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory"
p:brokerURL="tcp://localhost:61616" />
<!-- A POJO that implements the JMS message listener -->
<bean id="simpleMessageListener" class="com.test.jms.TestMessageListener"></bean>
<!-- The Spring message listener container configuration -->
<jms:listener-container
container-type="default"
connection-factory="connectionFactory"
acknowledge="auto">
<jms:listener destination="jms/testQueueOne" ref="simpleMessageListener" method="onMessage" />
</jms:listener-container>
</beans>
And the Java class as follows,
My Environment is Fedora 20 64-bit,JBoss EAP 6.2, Active MQ 5.9, Spring 4.0.0.Final,mavne 3.
I googled out for JMS Listener with the above technologies but i could not find any.
It has taken me many hours to work out and find the appropriate solution. So i'm sharing my experiences so that you can work out easily.
First we need to set up Active MQ 5.9 which is latest by writing this post.
First download it from Apache website Active MQ Download page and just unzip it.
you can start it by typing the command bin/activemq start
You can refer all the other things here as well.
Active MQ installation
Next install the Jboss EAP 6.2 and configure the adapter.
In standalone.xml write the following code
<subsystem xmlns="urn:jboss:domain:resource-adapters:1.1">and one more thing we need to do.. that is adding the apache-activemq.rar to the deployment folder in the Jboss Standalone Installation directory.
<resource-adapters>
<resource-adapter id="activemq-rar.rar">
<archive>
activemq-rar.rar
</archive>
<transaction-support>NoTransaction</transaction-support>
<config-property name="ServerUrl">
tcp://localhost:61616
</config-property>
<connection-definitions>
<connection-definition class-name="org.apache.activemq.ra.ActiveMQManagedConnectionFactory" jndi-name="jms/mqConnectionFactory" enabled="true" use-java-context="true" pool-name="mqConnectionFactory"/>
</connection-definitions>
<admin-objects>
<admin-object class-name="org.apache.activemq.command.ActiveMQQueue" jndi-name="jms/testQueueOne" use-java-context="true" pool-name="testQueueOne">
<config-property name="PhysicalName">
TestQueueOne
</config-property>
</admin-object>
</admin-objects>
</resource-adapter>
</resource-adapters>
</subsystem>
You can download that rar file from the following link.ActiveMQ adapter rar file
You can rename it and this name should be as in the above code snippet.
<archive> activemq-rar.rar </archive>
And then you can configure the spring-config.xml file as follows to listen the messages.
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:jms="http://www.springframework.org/schema/jms"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/jms http://www.springframework.org/schema/jms/spring-jms-3.0.xsd">
<!-- A JMS connection factory for ActiveMQ -->
<bean id="connectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory"
p:brokerURL="tcp://localhost:61616" />
<!-- A POJO that implements the JMS message listener -->
<bean id="simpleMessageListener" class="com.test.jms.TestMessageListener"></bean>
<!-- The Spring message listener container configuration -->
<jms:listener-container
container-type="default"
connection-factory="connectionFactory"
acknowledge="auto">
<jms:listener destination="jms/testQueueOne" ref="simpleMessageListener" method="onMessage" />
</jms:listener-container>
</beans>
And the Java class as follows,
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.TextMessage;
import org.apache.log4j.Logger;
public class TestMessageListener implements MessageListener {
private static final Logger LOG = Logger.getLogger(MyMessageListener.class);
public void onMessage(Message message) {
try {
TextMessage msg = (TextMessage) message;
LOG.info("Consumed message: " + msg.getText());
} catch (JMSException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
And pom.xml would be like as follows,
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><artifactId>spring-activemq</artifactId><modelVersion>4.0.0</modelVersion><groupId>com.test</groupId><project.groupid>com.test</project.groupid><version>0.0.1-SNAPSHOT</version><name>Spring JMS and Active MQ</name><properties><packaging>war</packaging><groupId>org.apache.activemq</groupId><spring.version>4.0.0.RELEASE</spring.version><hostname>localhost</hostname> <!-- Where to deploy. --><dependency><port>9999</port></properties></dependency><dependencies> <artifactId>activemq-all</artifactId><version>5.9.0</version><dependency></dependency><artifactId>spring-core</artifactId><groupId>org.springframework</groupId><groupId>org.springframework</groupId><version>${spring.version}</version><dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>${spring.version}</version> </dependency> <dependency><groupId>org.springframework</groupId><artifactId>spring-context-support</artifactId> <version>${spring.version}</version><groupId>org.springframework</groupId></dependency> <dependency><version>${spring.version}</version><artifactId>spring-jms</artifactId> </dependency> <dependency></dependency><artifactId>spring-messaging</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>${spring.version}</version> </dependencies> <profiles><artifactId>jboss-as-maven-plugin</artifactId><profile> <id>deploy-eap</id> <activation> <property> <name>deployToEAP</name> </property> </activation> <build> <finalName>spring-activemq</finalName> <plugins> <plugin> <groupId>org.jboss.as.plugins</groupId><phase>install</phase><version>7.5.Final</version> <configuration> <hostname>${hostname}</hostname> <port>${port}</port> <username>${username}</username> <password>${password}</password> </configuration> <executions> <execution> <goals><artifactId>jboss-as-maven-plugin</artifactId><goal>deploy</goal> </goals> </execution> </executions> </plugin> </plugins> </build> </profile> </profiles> <build> <finalName>spring-activemq</finalName> <plugins> <plugin> <groupId>org.jboss.as.plugins</groupId> <version>7.5.Final</version></execution><configuration> <hostname>${hostname}</hostname> <port>${port}</port> <username>${username}</username> <password>${password}</password> </configuration> <executions> <execution> <phase>install</phase> <goals> <goal>deploy</goal> </goals> </executions></plugin> </plugins> </build> </project>
Once you are done with this, You can build it and then deploy to Jboss.
In the activemq console, you can create queue and then send messages any number.
once it is sent, Listener will catch and executes onMessage().
You are done with the settings.
Hope it helps one in need. :)