Spring Tutorials

Spring Tutorial

Introduction of Spring Framework

Advantages and Disadvantages of Spring Framework

Features of Spring Framework

Basic concept of Spring Framework

Architecture of Spring Framework

Modules of Spring Framework

Goal of Spring Framework

Create Application of Spring without any IDE

Create Application of Spring in Eclipse IDE

Create Application of Spring in MYEclipse IDE

Important JAR of Spring Framework

IOC container in Spring Framework

Bean and Applicationcontext container in Spring Framework

Example of Spring BeanFactory Container in Spring Framework

Example of Spring ApplicationContext Container in Spring Framework

Bean in Spring Framework

Lifecycle of Bean in Spring Framework

Scope of Bean in Spring Framework

Autowiring in Spring Framework

Dependency Injection in Spring Framework

Constructor-based Dependency Injection Example in Spring Framework

Setter-based Dependency Injection Example in Spring Framework

Introduction of AOP in Spring Framework

Core concept and Goal of AOP in Spring Framework

AOP Proxies in Spring framework

XML Schema Based AOP in Spring Framework

AOP Xml based Configuration Example in Spring Framework

Example of Declaring AOP Advices in Spring Framework

AOP AspectJ Annotation with Example in Spring Framework

Declaring an aspect and pointcut using Annotation in Spring Framework

Declaring AOP Advices using Annotation Example in Spring Framework

DAO support in Spring Framework

Introduction of Spring JDBC Framework in Spring Framework

Introduction of Spring JdbcTemplate in Spring Framework

Example of Spring JdbcTemplate class in Spring framework

Example of Executing SQL statements in Spring Framework

Example of Executing DDL statements in Spring Framework

SQL Stored Procedure in Spring Framework

Example of NamedParameterJdbcTemplate in Spring Framework

Example of RowMapperJdbcTemplate in Spring Framework

Introduction of ORM Framework in Spring Framework

Integration of Spring with Hibernate in Spring Framework

Integration of Spring with JPA in Spring Framework

Introduction of Spring Expression Language (SpEL) in Spring Framework

Example of Spring Expression Language (SpEL) in Spring framework

Example of Spring EL in XML and Annotation in Spring Framework

Language Reference with SpEL in Spring Framework

Operators in Spring Expression Language(SpEL) in Spring Framework

Variable in Spring Expression Language(SpEL) in Spring Framework

Introduction of Spring Framework Transaction Management in Spring Framework

Spring Framework Transaction Abstraction

Spring Declarative Transaction Management in Spring Framework

Spring Programmatic Transaction Management in Spring Framework

Introduction of Spring OXM (Object XML Mapping) in Spring Framework

Integration of Spring with Jaxb Example in Spring framework

Example of Spring with Xstream in Spring Framework

Example of Spring with Castor in Spring Framework

Remote Method Invocation(RMI) in Spring Framework

Spring and RMI Integration with Example in Spring Framework

Example of Spring and Httpinvoker in Spring Framework

Example of Spring and Hessian in Spring Framework

Integration of Spring with JMS in Spring Framework

Introduction of Webservice in Spring Framework

Spring Web Services in Spring Framework

Web Services with Jax-WS in Spring framework

Exposing and Exporting servlet-based Web Services using JAX-WS in Spring Framework

Accessing Web Services using JAX-WS in Spring Framework

Introduction of JMS in Spring Framework

JMS Messaging Models in Spring Framework

Using Spring JMS in Spring Framework

Sending and Receiving a Message Using JMS API in Spring Framework

Introduction of JMX (Java Management Extension) in Spring Framework

Integrating Beans with JMX in Spring Framework

Creating a MBeanServer in Spring Framework

Introduction Java Mail with Spring in Spring Framework

Example of Java Mail with spring in Spring Framework

Introduction of EJB(Enterprise JavaBeans) in Spring Framework

Introduction of EJB(Enterprise JavaBeans) Integration in Spring Framework

Integration of Spring With Struts 2 Example in Spring Framework

Spring MVC

Spring MVC

adplus-dvertising
Spring and JMS Integration with Example
Previous Home Next

Spring provides a JMS integration framework that simplifies the use of the JMS API much like Spring’s integration does for the JDBC API.

JMS can be roughly divided into two areas of functionality, namely the production and consumption of messages. The JmsTemplate class is used for message production and synchronous message reception. For asynchronous reception similar to Java EE’s message-driven bean style, Spring provides a number of message listener containers that are used to create Message-Driven POJOs (MDPs).

Spring also provides a declarative way of creating message listeners. The package org.springframework.jms.core provides the core functionality for using JMS.

JMS:- JMS is a set of APIs defined in JSR. In practical you can think it’s a set of Java Interfaces. JMS is part of the J2EE standard coming with JDK in package javax.jms.* There are basically 2 versions of JMS, JMS 1.x and JMS 2.0. These 2 sets of APIs are different.

ActiveMQ:- ActiveMQ is one of the implementation of JMS APIs. ActiveMQ also provide the broker which can be treated somehow like a server, that you can send message to or receive message from.

Spring JMS:- Spring JMS is part of the whole spring framework. It wrap the real JMS service provider such as ActiveMQ or OpenJMS, provides consistent APIs to upper logic. Spring JMS’s APIs are quite similar to JMS2.0 in JEE7. Spring JMS can decouple you business logic from the real JMS service provider, which here in our example is apache ActiveMQ.

Technologies used to run this Application.
  1. Spring 3.0
  2. Eclipse IDE
  3. JDK 1.6
  4. ActiveMQ 5.9.1
Download the Active MQ Server

After Downloading the Active MQ Server, put this is in C-drive. now add the activemqall5.9.jar File into the Appache Active MQ Server, then Double Click on the activemq.bat file located inside apache-activemq-5.9.1-bin\apache-activemq-5.9.1\bin\win32 directory. Now you see the below image as your screen.

Now activemq server console will open. Access the console of activemq server by http://localhost:8161

Now click on the link as shown above image, then you find the admin page as given below. If console ask password then admin is bydefault.

Now, click on the Queues link, as shown in the image menu and write the any name like yourqueue in the textfield and click on the create button.

Now Let us have working Eclipse IDE in place and follow the following steps to create a Spring application:

Step1. Now you create a simple Java Project using Eclipse IDE. Follow the option File -> New -> Project and finally select Java Project wizard from the wizard list. Now name your project as JmsReceiverApplication using the wizard window as given below.

Add spring jar files

Step2. Now add spring jar in your project, To do this, right click on your project name JmsReceiverApplication and then follow the following option available in context menu: Build Path -> Configure Build Path to display the Java Build Path window as given below. There are some jar files required to run this application, To load the jar files in eclipse IDE, Right click on your project - Build Path - Add external archives - select all the required jar files - finish.

  1. spring core Jars
  2. spring aop Jars
  3. spring j2ee Jars
  4. spring persistence core jar
  5. And add activemqall5.9.jar file located inside the activemq directory
Create Java class file

Step3. Now you create simply java file with any name. First we need to create a package with any name like com.r4r. To do this, Right click on src in package explorer section and follow the option : New -> Package. and then Right click on package(com.r4r) - New - class - Write the class name e.g. MessageReceiver - finish. Write the following code:

package com.r4r;
import javax.jms.Message;  
import javax.jms.MessageListener;  
import javax.jms.TextMessage;  
public class MessageReceiver implements MessageListener{  
    @Override  
    public void onMessage(Message m) {  
        TextMessage message=(TextMessage)m;  
        try{  
            System.out.println(message.getText());  
        }catch (Exception e) 
			{
			e.printStackTrace(); 
			}  
    }  
} 

Step4. Now create the xml file click on src- new -file -give the file name like applicationContext.xml then finish.

<?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">  
  
<bean id="connectionFactory" 
class="org.apache.activemq.ActiveMQConnectionFactory"  
 p:brokerURL="tcp://localhost:61616" />  
  
<bean id="listener" class="com.r4r.MessageReceiver"></bean>  
  
<jms:listener-container container-type="default"
connection-factory="connectionFactory"  
 acknowledge="auto">  
<jms:listener destination="yourqueue" ref="listener" 
method="onMessage"></jms:listener>  
</jms:listener-container>  
  
</beans>     

Step5. Now Create the java Main class (Test.java).

package com.r4r;
import org.springframework.context.support.GenericXmlApplicationContext;
public class Test {  
public static void main(String[] args) {  
    GenericXmlApplicationContext ctx=new GenericXmlApplicationContext();  
    ctx.load("classpath:applicationContext.xml");  
    ctx.refresh();  
    while(true){}  
}  
}  

Step6. Now you create another simple Java Project using Eclipse IDE. Follow the option File -> New -> Project and finally select Java Project wizard from the wizard list. Now name your project as JmsSenderApplication using the wizard window as given below. and add the same Jars as given above.

Create a Java class file.

Step7. Now you create simply java file with any name. First we need to create a package with any name like com.r4r. To do this, Right click on src in package explorer section and follow the option : New -> Package. and then Right click on package(com.r4r) - New - class - Write the class name e.g. MessageSender - finish. Write the following code:

package com.r4r;
import javax.jms.*;  
import org.springframework.beans.factory.annotation.Autowired;  
import org.springframework.jms.core.JmsTemplate;  
import org.springframework.jms.core.MessageCreator;  
import org.springframework.stereotype.Component;  
  
@Component("messageSender")  
public class MessageSender {  
@Autowired  
private JmsTemplate jmsTemplate;  
public void sendMessage(final String message){  
    jmsTemplate.send(new MessageCreator(){  
  
        @Override  
        public Message createMessage(Session session) throws JMSException {  
            return session.createTextMessage(message);  
        }  
    });  
}  
}
 

Step8. Now create the xml file click on src- new -file -give the file name like applicationContext.xml then finish.

<?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:context="http://www.springframework.org/schema/context"  
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/context  
http://www.springframework.org/schema/context/spring-context-3.0.xsd  
http://www.springframework.org/schema/jms  
http://www.springframework.org/schema/jms/spring-jms-3.0.xsd">  
  
<bean id="connectionFactory" 
class="org.apache.activemq.ActiveMQConnectionFactory"  
 p:brokerURL="tcp://localhost:61616" />  
  
<bean id="jmsTemplate" 
class="org.springframework.jms.core.JmsTemplate">  
<constructor-arg name="connectionFactory" 
ref="connectionFactory"></constructor-arg>  
<property name="defaultDestinationName"
value="yourqueue"></property>  
</bean>  
  
<context:component-scan base-package="com.r4r">
</context:component-scan>  
  
</beans>

Step9. Now Create the java Main class (Test.java).

package com.r4r;
import org.springframework.context.support.GenericXmlApplicationContext;  
public class Test {  
public static void main(String[] args) {  
    GenericXmlApplicationContext ctx=new GenericXmlApplicationContext();  
    ctx.load("classpath:applicationContext.xml");  
    ctx.refresh();  
    MessageSender sender=ctx.getBean("messageSender",MessageSender.class);  
    sender.sendMessage("Spring With JMS Integration...........");  
}  
}  

Now first Run the JmsSenderApplication and reload the Active MQ server page, you see the Active MQ server show one message as given below image.

And Console Output is shown below:

Now Run JmsReceiverApplication, Finally reload again Active Mq server, you see the following output.

Previous Home Next