Showing posts with label ActiveMQ. Show all posts
Showing posts with label ActiveMQ. Show all posts

Wednesday, April 1, 2009

Another Way of Starting ActiveMQ Broker in Spring

I had posted a blog about running an ActiveMQ broker in Spring. Recently, I found an alternative way to start an ActiveMQ message broker in spring environment by injecting a bean based on org.apache.activemq.xbean.XBeanBrokerService:


<bean id="broker" class="org.apache.activemq.xbean.XBeanBrokerService">
<property name="useJmx" value="true"/>
<!-- this tells it to create a filesystem store -->
<property name="persistent" value="true"/>
<property name="brokerName" value="mySpringBroker"/>
<!--the queues/topics that this broker starts with-->
<property name="destinations">
<list>
<ref bean="destination1"/>
<ref bean="destination2"/>
</list>
</property>
<property name="transportConnectors">
<list>
<!-- an in vm connector which is nice and fast -->
<bean class="org.apache.activemq.broker.TransportConnector">
<property name="uri" value="vm://localhost"/>
</bean>
<!-- a connection for external clients specifying the IP of machine deploying the broker, something like tcp://10.10.10.10:61636. Here read from a properties file but can be hard-coded as well-->
<bean class="org.apache.activemq.broker.TransportConnector">
<property name="uri" value="${broker.transportExternalConnectorUri}"/>
</bean>
</list>
</property>
</bean>

<bean id="destination1" class="org.apache.activemq.command.ActiveMQQueue">
<constructor-arg value="com.saveenkumar.myQueue1"/>
</bean>

<bean id="destination2" class="org.apache.activemq.command.ActiveMQQueue">
<constructor-arg value="com.saveenkumar.myQueue2"/>
</bean>

In fact,the package org.apache.activemq.xbean provides various options (beans) to start the broker in a spring environment.

Wednesday, October 8, 2008

ActiveMQ Message Consumer in Spring

In the last blog we saw how to configure an ActiveMQ broker to run in a Spring environment. In this blog we will see how to configure a message consumer. Again, I am not trying to explain how consumers work but just sharing code and config to make it work in Spring :p

Since we have the luxury of Spring , we will use a message listener container provided by the framework: SimpleMessageListenerContainer (org.springframework.jms.listener.SimpleMessageListenerContainer ). My needs were simple and non-transactional so this container suffices

The basic reason is the desire to be message driven i.e. I want my app to take some actions as and when it receives messages. The container bean definition should look like:

    <bean id="jmsContainer" class="org.springframework.jms.listener.SimpleMessageListenerContainer">
        <property name="autoStartup" value="true"/>
        <property name="connectionFactory" ref="myConnectionFactory"/>
        <property name="destination" ref="myDestination"/>
        <property name="messageListener" ref="myMessageListener"/>
        <property name="acceptMessagesWhileStopping" value="true"/>
    </bean>

This config makes the container start when the bean gets loaded and makes sure no messages are lost even if it is down. Further we need to specify the connection factory to use to connect to broker, the destination at broker from which we wish to hear and the message listener that ultimately handles the message. The config for these three will look like:

 <bean id="myDestination" class="org.apache.activemq.command.ActiveMQQueue">
        <constructor-arg value="myPackage.myQueue"/>
    </bean>

 <bean id="myConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory">
        <property name="brokerURL" value="tcp://localhost:61636"/>
   </bean>

<bean id="myMessageListener" class="myPackage.MyMessageListener">       
    <property name="appName" value="My Application"/>
</bean>


Destination takes the name for destination in order to identify it at broker. the producer would refer it by the same name. JNDI can be used for more sophisticated look-ups which we may see some other time. Connection factory just needs url for a running broker. Message Listener is just our custom bean to handle messages. MyMessageListener would look like:

package myPackage;

import javax.jms.*;

public class MyMessageListener implements MessageListener {
    private String appName;

    public void setAppName(String appName){
        this.appName = appName;    
    }

    public void onMessage(Message message) {
        //do your stuff
    }
}

Finally, don't forget the POM dependencies included in last blog if you are using Maven. You should be good to go.

Tuesday, October 7, 2008

Running ActiveMQ Broker in Spring

ActiveMQ can run as an independent JMS server. This may be desirable if we want to run it independently of our spring application. However it is also possible that we may want to deploy a message broker within a spring environment. This may be simply because we need to do some unit testing or because our webapp is too small to break across several machines. Whatever be the reason. Let us make a note of basic steps to do it:

1. Put in your activemq.xml under the WEB-INF folder. Your ActiveMQ distribution(5.1.0 at time of this article) has this config file under the "conf" folder. We will not discuss configuring it here. Go here to read about configuring

2. The lib folder of the distribution has all possible jars that JMS may ever need in its lifetime. If you are not using Maven, you may need to copy the jars to your classpath. If you are using Maven, these dependencies should generally suffice for basic use.

        <dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jms</artifactId>
<version>2.5.5</version>
</dependency>

<dependency>
<groupId>org.apache.derby</groupId>
<artifactId>derby</artifactId>
<version>10.3.1.4</version>
</dependency>

<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-all</artifactId>
<version>5.1.0</version>
</dependency>

<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-web</artifactId>
<version>5.1.0</version>
</dependency>

Go here if you wish to simply run ActiveMQ using Maven
3. Put this bean definition in your spring conf:
<bean id="broker" class="org.apache.activemq.xbean.BrokerFactoryBean">
<property name="config" value="/WEB-INF/activemq.xml"/>
<property name="start" value="true"/>
</bean>
You should be good to go. Bringing up your Spring application will start the broker at tcp://localhost:61636