Dependencies:
Enable pom for maven dependency resolving:
In
BuildConfig.groovy, before repository, set pom to true, as:
pom true
repositories {
// ...
mavenRepo "http://repository.codehaus.org"
//...
make sure maven repository path for spring module is enabled.
create
pom.xml at same directory level as grails-app, at root folder, with contents as follows:
<?xml version="1.0" encoding="UTF-8"?>
<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">
<modelVersion>4.0.0</modelVersion>
<groupId>ANY_GROUP_ID_DOESNT_MATTER</groupId>
<artifactId>ANY_PROJECT_ID_DOESNT_MATTER</artifactId>
<version>1.0</version>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jms</artifactId>
<version>3.2.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-all</artifactId>
<version>5.9.0</version>
</dependency>
</dependencies>
</project>
Make sure module version matches with spring version comes package with grails, you may know by creating war, i.e. grails war, and extracting the war in folder and see file names of spring jars package in war.
Spring Beans:
Create
resources.xml, at grails-app/conf/spring/, and define spring beans, as done in any normal spring application (beans.xml):
<beans xmlns:jms="http://www.springframework.org/schema/jms"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<bean class="org.apache.activemq.command.ActiveMQQueue" id="destinationQueue">
<constructor-arg value="TEST.Q1"></constructor-arg>
</bean>
<bean class="org.apache.activemq.ActiveMQConnectionFactory" id="connectionFactoryQueue">
<property name="brokerURL">
<value>nio://localhost:61616</value>
</property>
</bean>
<bean class="org.springframework.jms.core.JmsTemplate" id="producerTemplate">
<property name="connectionFactory" ref="connectionFactoryQueue"></property>
<property name="defaultDestination" ref="destinationQueue"></property>
</bean>
<bean class="com.test.activemq.MessageSender" id="activeMQMsgSender">
<property name="jmsTemplate" ref="producerTemplate"></property>
</bean>
<bean class="org.springframework.jms.listener.DefaultMessageListenerContainer"
id="jmsContainer">
<property name="connectionFactory" ref="connectionFactoryQueue"></property>
<property name="destination" ref="destinationQueue"></property>
<property name="messageListener" ref="messageReceiverService"></property>
</bean>
</beans>
Above bean configuration connects to activemq at transport "nio://localhost:61616/", and send/receive message on queue "TEST.Q1"
Message Sender:
Create message sender bean,
MessageSender.java, in src/java/com/test/activemq/, with following content:
(Note: its declaration in bean configuration file, resources.xml, above)
package com.test.activemq;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.Session;
import javax.jms.TextMessage;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator;
public class MessageSender {
protected JmsTemplate jmsTemplate;
public JmsTemplate getJmsTemplate() {
return jmsTemplate;
}
public void setJmsTemplate(JmsTemplate jmsTemplate) {
this.jmsTemplate = jmsTemplate;
}
public void sendMessages(final String msg) throws JMSException {
System.out.println("PRODUCER [" + msg + "]");
MessageCreator messageCreator = new MessageCreator() {
public Message createMessage(Session session) throws JMSException {
TextMessage message = session
.createTextMessage(msg);
return message;
}
};
jmsTemplate.send(messageCreator);
}
}
Create controller
ActiveMQController.groovy, as grails controller with action "send" as follows:
(Note: activeMQMsgSender bean usage, with related to MessageSender.java and resources.xml)
def activeMQMsgSender
def send = {
def msg = params.msg
activeMQMsgSender.sendMessages(msg)
respond msg, model:[msg:msg]
}
Create corresponding view send.gsp as grails view with following content:
send [${msg}]
Message Receiver:
Create message receive bean,
MessageReceiverService.goovy, as grails service as follows:
(please note, how it is specified in beans configuration file, resources.xml, above)
package com.test.activemq
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.TextMessage;
import grails.transaction.Transactional
@Transactional
class MessageReceiverService implements MessageListener {
public void onMessage(Message message) {
System.out.print("RECEIVER ");
TextMessage msg = (TextMessage) message;
try {
System.out.print("MESSAGE: [" + msg.getText() + "]");
} catch (JMSException e) {
e.printStackTrace();
}
System.out.println();
}
}
Testing
Run grails app for testing, sending message to queue.
On browser, run: localhost:8080/test/activeMQ/send?msg=abcd
which would place abcd text message on activemq, queue TEST.Q1