Step 1: You need Jar files:
A. Spring core, spring-context
B. Spring JMS
C. activemq-all
D. slf4j , log4j etc.
Step 2: Create a Java Project in Eclipse and add "applicationContext.xml" File directly under "src" folder:
applicationContext.xml :
<?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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jms="http://www.springframework.org/schema/jms"
xmlns:amq="http://activemq.apache.org/schema/core"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/jms
http://www.springframework.org/schema/jms/spring-jms.xsd
http://activemq.apache.org/schema/core
http://activemq.apache.org/schema/core/activemq-core.xsd">
<context:component-scan base-package="com.itech" />
<context:annotation-config/>
<!-- JMS ConnectionFactory to use, configuring the embedded broker using XML -->
<bean id="activemqConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory">
<!-- brokerURL, You may have different IP or port -->
<constructor-arg name="brokerURL" value="vm://localhost:61616" />
</bean>
<!-- Pooled Spring connection factory -->
<bean id="connectionFactory" class="org.springframework.jms.connection.CachingConnectionFactory">
<constructor-arg ref="activemqConnectionFactory" />
</bean>
<!-- Queue Destination -->
<bean id="queueDestination" class="org.apache.activemq.command.ActiveMQQueue">
<!-- name of the queue -->
<constructor-arg index="0" value="THIS.IS.TEST.QUEUE" />
</bean>
<!-- JmsTemplate Definition -->
<bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
<property name="connectionFactory" ref="connectionFactory" />
<property name="defaultDestination" ref="queueDestination" />
</bean>
</beans>
Have a look at XML, it has almost everything you need to start with: - ConnectionFactory, Destination and Spring Provides JmsTemplate which we will be using to send the message.
Step 3: Create Test Class:
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.Session;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator;
public class ProducerTest {
public static void main(String[] args) {
// Load Application Context
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
// Get JmsTemplate Bean from context
JmsTemplate template = (JmsTemplate) ctx.getBean("jmsTemplate");
// Create Message
MessageCreator message = new MessageCreator() {
@Override
public Message createMessage(Session session) throws JMSException {
return session.createTextMessage("This is test message");
}
};
// Send the message
template.send(message);
// Close the context
((ClassPathXmlApplicationContext)ctx).close();
// You are Done!!
}
}
A. Spring core, spring-context
B. Spring JMS
C. activemq-all
D. slf4j , log4j etc.
Step 2: Create a Java Project in Eclipse and add "applicationContext.xml" File directly under "src" folder:
applicationContext.xml :
<?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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jms="http://www.springframework.org/schema/jms"
xmlns:amq="http://activemq.apache.org/schema/core"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/jms
http://www.springframework.org/schema/jms/spring-jms.xsd
http://activemq.apache.org/schema/core
http://activemq.apache.org/schema/core/activemq-core.xsd">
<context:component-scan base-package="com.itech" />
<context:annotation-config/>
<!-- JMS ConnectionFactory to use, configuring the embedded broker using XML -->
<bean id="activemqConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory">
<!-- brokerURL, You may have different IP or port -->
<constructor-arg name="brokerURL" value="vm://localhost:61616" />
</bean>
<!-- Pooled Spring connection factory -->
<bean id="connectionFactory" class="org.springframework.jms.connection.CachingConnectionFactory">
<constructor-arg ref="activemqConnectionFactory" />
</bean>
<!-- Queue Destination -->
<bean id="queueDestination" class="org.apache.activemq.command.ActiveMQQueue">
<!-- name of the queue -->
<constructor-arg index="0" value="THIS.IS.TEST.QUEUE" />
</bean>
<!-- JmsTemplate Definition -->
<bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
<property name="connectionFactory" ref="connectionFactory" />
<property name="defaultDestination" ref="queueDestination" />
</bean>
</beans>
Have a look at XML, it has almost everything you need to start with: - ConnectionFactory, Destination and Spring Provides JmsTemplate which we will be using to send the message.
Step 3: Create Test Class:
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.Session;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator;
public class ProducerTest {
public static void main(String[] args) {
// Load Application Context
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
// Get JmsTemplate Bean from context
JmsTemplate template = (JmsTemplate) ctx.getBean("jmsTemplate");
// Create Message
MessageCreator message = new MessageCreator() {
@Override
public Message createMessage(Session session) throws JMSException {
return session.createTextMessage("This is test message");
}
};
// Send the message
template.send(message);
// Close the context
((ClassPathXmlApplicationContext)ctx).close();
// You are Done!!
}
}
That's it. You are Done! You have written a complete code in SpringJMS for sending a message.
Thanks!
No comments:
Post a Comment