jms with appfuse1.x

8

Posted on : 20-May-2009 | By : Bharathi Raja | In : appfuse, Java, Programming

Here are the steps which i have followed for using jms with appfuse 1.9.

Before starting i assume you have read the theory portion of jms , Ok let me start with the steps now :-

Jars required :-

activeio-core-3.1.0.jar

activemq-all-5.1.0.jar

derby-10.1.3.1.jar

jms.jar

xbean-spring-2.7.jar

Xml files :-

Create an xml file named :-applicationContext-jms.xml place this in web/WEB-INF

<?xml version=”1.0″ encoding=”UTF-8″?>
<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 id=”defaultDestination” class=”org.apache.activemq.command.ActiveMQQueue”>
<constructor-arg value=”foo” />
</bean>

<!– JMS –>
<bean id=”broker” class=”org.apache.activemq.xbean.BrokerFactoryBean”>
<!–<property name=”config” value=”classpath:org/apache/activemq/xbean/activemq.xml” />–>
<property name=”config” value=”classpath:activemq.xml” />
<property name=”start” value=”true” />
</bean>

<!– a pooling based JMS provider –>
<bean id=”jmsFactory” class=”org.apache.activemq.pool.PooledConnectionFactory” destroy-method=”stop”>
<property name=”connectionFactory”>
<bean class=”org.apache.activemq.ActiveMQConnectionFactory”>
<property name=”brokerURL”><value>tcp://localhost:61636</value></property>
</bean>
</property>
</bean>

<!– Spring JMS Template –>
<bean id=”proucerJmsTemplate” class=”org.springframework.jms.core.JmsTemplate”>
<property name=”connectionFactory”><ref local=”jmsFactory”/></property>
<property name=”defaultDestination”><ref local=”defaultDestination”/></property>
</bean>

<bean id=”consumerJmsTemplate” class=”org.springframework.jms.core.JmsTemplate”>
<property name=”connectionFactory”><ref local=”jmsFactory”/></property>
<property name=”defaultDestination”><ref local=”defaultDestination”/></property>
</bean>

<!– a sample POJO which uses a Spring JmsTemplate –>
<bean id=”messageProducer” class=”com.foo.service.impl.MessageProducer”>
<property name=”jmsTemplate”><ref bean=”proucerJmsTemplate”></ref></property>
</bean>

<!– a sample POJO consumer –>

<bean id=”messageListener” class=”com.foo.service.impl.MessageConsumer”>
<!–
<property name=”homePageService”><ref bean=”homePageService”></ref></property>
–>
</bean>

<bean id=”listenerContainer”
class=”org.springframework.jms.listener.DefaultMessageListenerContainer”>
<property name=”connectionFactory” ref=”jmsFactory” />
<property name=”destination” ref=”defaultDestination” />
<property name=”messageListener” ref=”messageListener” />
</bean>
</beans>

The above class refernce to three classes which needs to be added :-

1.activemq.xml place this inside web/WEB_INF/classes

<?xml version=”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 information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the “License”); you may not use this 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 agreed to in writing, software
distributed under the License is distributed on an “AS IS” BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
–>

<!– this file can only be parsed using the xbean-spring library –>
<!– START SNIPPET: example –>
<beans>

<bean class=”org.springframework.beans.factory.config.PropertyPlaceholderConfigurer”/>

<broker useJmx=”true” xmlns=”http://activemq.apache.org/schema/core”>

<persistenceAdapter>
<journaledJDBC journalLogFiles=”5″ dataDirectory=”../activemq-data” />

<!– To use a different dataSource, use the following syntax : –>
<!–
<journaledJDBC journalLogFiles=”5″ dataDirectory=”${basedir}/activemq-data” dataSource=”#mysql-ds”/>
–>
</persistenceAdapter>

<transportConnectors>
<transportConnector uri=”tcp://localhost:61636″ />
</transportConnectors>

<networkConnectors>
<!–
<networkConnector uri=”multicast://default?initialReconnectDelay=100″ />
<networkConnector uri=”static://(tcp://localhost:61616)” />
–>
</networkConnectors>
</broker>

<!– MySql DataSource Sample Setup –>
<!–
<bean id=”mysql-ds” class=”org.apache.commons.dbcp.BasicDataSource” destroy-method=”close”>
<property name=”driverClassName” value=”com.mysql.jdbc.Driver”/>
<property name=”url” value=”jdbc:mysql://localhost/activemq?relaxAutoCommit=true”/>
<property name=”username” value=”activemq”/>
<property name=”password” value=”activemq”/>
<property name=”poolPreparedStatements” value=”true”/>
</bean>
–>
</beans>
<!– END SNIPPET: example –>

2.MessageProducer.java

public class MessageProducer extends JmsGatewaySupport {

protected final static String CMD_INDEX_ITEM = “INDEX_ITEM”;
protected final static String CMD_TRIGGER_BIDDING_AGENT = “CMD_TRIGGER_BIDDING_AGENT”;
protected final static String CMD_INCREASE_ITEM_COUNT = “CMD_INCREASE_ITEM_COUNT”;
protected final static String COMMAND = “COMMAND”;
protected final static String CMD_REGISTRATION_MAIL = “CMD_REGISTRATION_MAIL”;

//    private static Logger log = Logger.getLogger(MessageProducer.class.getName());

/**
* Send registration mail
*/
public void sendRegistrationMail(){
getJmsTemplate().send(new MessageCreator() {
public Message createMessage(Session session) throws JMSException {
MapMessage msg = session.createMapMessage();
//msg.setString(MEMBERID, memberId);
//msg.setString(REG_URL, regUrl);
msg.setString(COMMAND, CMD_REGISTRATION_MAIL);
return msg;
}
});
}

}

3.MessageConsumer.java

import static com.thriveresearch.service.impl.MessageProducer.COMMAND;
import static com.thriveresearch.service.impl.MessageProducer.CMD_REGISTRATION_MAIL;
public class MessageConsumer implements MessageListener {

//private static Logger log = Logger.getLogger(MessageConsumer.class.getName());

public void onMessage(Message message) {
if (message instanceof MapMessage) {
try {
MapMessage mapMessage = (MapMessage) message;
String command = mapMessage.getString(COMMAND);
if(CMD_REGISTRATION_MAIL.equals(command)){
System.out.println(“>>>>>>>>>>>>>>>>>>>>>>>>>bharathi here “);
}

}
catch (Exception e) {
//log.error(“error on message”, e);
}
}
}
}
Now in your action class call the message producer this way :-

MessageProducer messageProducer = (MessageProducer) getBean(“messageProducer”);//= new MessageProducer();
messageProducer.sendRegistrationMail();

Eureka build and run your application you can see jms running :)

Post in your comments if you have any doubts :-)

Comments (8)

The article is ver good. Write please more

Thanks janer :)

I really like your post. Does it copyright protected?

No Katty you can use it :)

I think I will try to recommend this post to my friends and family, cuz it’s really helpful.

It’s a masterpiece. I have never thought people can have such ideas and thoughts. You are great.

It’s a pity that people don’t realize the importance of this information. Thanks for posing it.

Awesome information you have here. Thanks for sharing.

Write a comment