Spring Web Service, Spring JMS, Eclipse & Maven Tutorials
Spring Web Service, Spring JMS, Eclipse & Maven Tutorials
The author has made every effort in the preparation of this book to ensure the accuracy of the information.
However, information in this book is sold without warranty either expressed or implied. The author will not be
held liable for any damages caused or alleged to be caused either directly or indirectly by this book.
By
K. Arulkumaran
&
A. Sivayini
Website: http://www.lulu.com/java-success
Table Of Contents
Notations ..................................................................................................................... 3
Tutorial 11 – Spring Web Service ......................................................................... 4
Tutorial 12 – Spring Web Service with Logging ............................................. 19
Tutorial 13 – Spring JMS....................................................................................... 22
Tutorial 14 – Spring JMS Asynchronous.......................................................... 38
3
Notations
Command prompt:
Eclipse:
Internet Explorer:
4
Tutorial 11 – Spring Web Service
This tutorial will guide you through building a simple web service using spring
framework. This tutorial assumes that you have gone through Tutorials 1-10
& the source code for tutorials 1-10. Also refer to spring-ws-reference.pdf
(http://static.springframework.org/spring-ws/site/reference/pdf/spring-ws-
reference.pdf ) for further information.
Step 1: Create a project named simpleWeb2. Run the following command in a command
window:
C:\tutorials\simple-tutorial>mvn archetype:create \
-DarchetypeGroupId=org.springframework.ws -DarchetypeArtifactId=spring-ws-archetype \
-DarchetypeVesrion=1.0.0 -DgroupId=com.mytutorial -DartifactId=simpleWeb2
Step 2: Now you should have the simpleWeb2 project with some basic files like pom.xml,
web.xml, spring-ws-servlet.xml etc.
<dependency>
<groupId>jdom</groupId>
<artifactId>jdom</artifactId>
<version>1.0</version>
</dependency>
<dependency>
<groupId>jaxen</groupId>
<artifactId>jaxen</artifactId>
<version>1.1</version>
</dependency>
<dependency>
<groupId>javax.xml.soap</groupId>
<artifactId>saaj-api</artifactId>
<version>1.3</version>
7
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>com.sun.xml.messaging.saaj</groupId>
<artifactId>saaj-impl</artifactId>
<version>1.3</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring</artifactId>
<version>2.0.6</version>
</dependency>
</dependencies>
</project>
8
Note: The <exclusion> tags will remove any transitive dependencies you would like to
exclude. Look at the .pom file as shown above for the dependencies defined and you can
exclude them if you wish.
Step 3: Next step is to generate eclipse metadata using the mvn command as shown below:
C:\tutorials\simple-tutorial\simpleWeb2>mvn eclipse:eclipse
Step 4: We can now import this project into eclipse. File Æ Import and then
9
Step 5: Next step is to create a folder “java” for the java files under “src/main”. Right click on
simpleWeb2 and select “properties”
10
11
Step 6: Now define the input XML file and its contract. Create a new folder “etc” under
“simpleWeb2” for any miscellaneous files like “simple.xml” that are not packaged.
<simpleRequest>
<firstname>John</firstname>
<surname>Smith</surname>
</simpleRequest>
The next step is to define the schema definition for the above xml file. The “simple.xsd”
should be created under “C:\tutorials\simple-tutorial\simpleWeb2\src\main\webapp\WEB-
INF” as shown below:
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://mytutorial.com/schemas"
xmlns="http://mytutorial.com/schemas" >
<xs:element name="firstname">
<xs:complexType mixed="true" />
</xs:element>
<xs:element name="surname">
<xs:complexType mixed="true" />
</xs:element>
<xs:element name="simpleRequest">
<xs:complexType>
<xs:sequence>
<xs:element ref="firstname" />
<xs:element ref="surname" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
12
Step 8: With spring-ws, you do not have to define the .wsdl file. It will be automatically
generated based on your simple.xsd file you just created and the simple-ws-servlet.xml
(the file convention is <servlet-name-defined-in-web.xml>-servlet.xml) file you are about to
define.
<bean
class="org.springframework.ws.server.endpoint.mapping.PayloadRootQNameEndpointMapping">
<property name="mappings">
<props>
<prop key="simpleRequest">simpleEndPoint</prop>
</props>
</property>
<property name="interceptors">
<bean
class="org.springframework.ws.server.endpoint.interceptor.PayloadLoggingInterceptor" />
</property>
</bean> Generates simple.wsdl
<bean id="simple"
class="org.springframework.ws.wsdl.wsdl11.DynamicWsdl11Definition">
<property name="builder">
<bean
class="org.springframework.ws.wsdl.wsdl11.builder.XsdBasedSoap11Wsdl4jDefinitionBuilder">
<property name="schema" value="/WEB-INF/simple.xsd" />
<property name="portTypeName" value="simple" />
<property name="locationUri"
value="http://localhost:8080/simpleWeb2/simpleService/" />
<property name="targetNamespace"
value="http://mytutorial.com/definitions" />
</bean>
</property>
</bean>
</beans>
Step 9: The next step is to define the end point class as shown below to read the incoming
XML request and construct an XML response back to the caller. Create a new package
com.mytutorial under “java” and then create the java file “SimpleEndPoint.java”.
package com.mytutorial;
14
import org.springframework.ws.server.endpoint.AbstractDomPayloadEndpoint;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
public SimpleEndPoint() {
System.out.println("Instantiated .............. ");
}
String fn =simpleRequest.getElementsByTagName("firstname")
.item(0).getTextContent();
String sn =simpleRequest.getElementsByTagName("surname")
.item(0).getTextContent();
return responseElement;
}
Step 10: Finally we need to create a Web Services client named “Client.java” to invoke our
web service.
package com.mytutorial;
import java.io.StringReader;
import javax.xml.transform.stream.StreamResult;
15
import javax.xml.transform.stream.StreamSource;
import org.springframework.ws.client.core.WebServiceTemplate;
Step 11: Deploy the “simpleWeb2” war file into Tomcat under eclipse and start the Tomcat
Server.
16
Step 11: Run the Client.java (Right click and Run As Æ Java Application) file and check
the output printed on the console.
Interview Questions with Answers on Web Services & SOA are discussed under “How would
you go about section” in Java/J2EE Job Interview Companion at
http://www.lulu.com/content/192463
Please feel free to email any errors to java-interview@hotmail.com. Also stay tuned at
http://www.lulu.com/java-success for more tutorials and Java/J2EE interview
resources.
19
Tutorial 12 – Spring Web Service with Logging
This tutorial is a continuation of Tutorial 11. In this tutorial I will be turning on the logging
feature of spring-ws framework via log4j & interceptors provided by spring-ws so that soap
messages can be printed.
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
<appender name="ConsoleAppender"
class="org.apache.log4j.ConsoleAppender">
<layout class="org.apache.log4j.SimpleLayout" />
</appender>
<logger name="org.springframework.ws">
<level value="DEBUG" />
<appender-ref ref="ConsoleAppender"/>
</logger>
<root>
<level value="debug" />
<appender-ref ref="ConsoleAppender"/>
</root>
</log4j:configuration>
<bean
class="org.springframework.ws.server.endpoint.mapping.PayloadRootQNameEndpointMapping">
<property name="mappings">
<props>
<prop key="simpleRequest">simpleEndPoint</prop>
</props>
</property>
<property name="interceptors"> Interceptors
<list>
<bean class="org.springframework.ws.server.endpoint.interceptor.PayloadLoggingInterceptor"/>
<bean
class="org.springframework.ws.soap.server.endpoint.interceptor.SoapEnvelopeLoggingInterceptor"/>
</list>
</property>
</bean>
<bean id="simple"
class="org.springframework.ws.wsdl.wsdl11.DynamicWsdl11Definition">
<property name="builder">
<bean
class="org.springframework.ws.wsdl.wsdl11.builder.XsdBasedSoap11Wsdl4jDefinitionBuilder">
<property name="schema" value="/WEB-INF/simple.xsd" />
<property name="portTypeName" value="simple" />
<property name="locationUri"
value="http://localhost:8080/simpleWeb2/simpleService/" />
<property name="targetNamespace"
value="http://mytutorial.com/definitions" />
</bean>
</property>
</bean>
</beans>
21
Step 3: Deploy the “simpleWeb2” to Tomcat and run the client again and you should now be
able to see a number of debug messages. Take note of the SOAP messages.
22
Tutorial 13 – Spring JMS
This tutorial will guide you through building a simple messaging (i.e. JMS
based) application using spring framework. You need a message broker or a
JMS provider for your JMS applications. There are number of brokers (aka
providers) both commercial like Websphere MQ (used to be called MQSeries),
Web Methods etc and open source providers like Active MQ, OpenJMS etc.
To keep things simple, I will be using OpnenJMS
(http://openjms.sourceforge.net/ ) in this tutorial as our JMS provider. This
tutorial is based on http://www.devx.com/Java/Article/20903.
You can start & stop the server in a command prompt using batch scripts under “bin”
directory. Also have a look at the opnejms.xml file under “config” folder where connection
factories (e.g. Topic Connection Factories, Queue Connection Factories) & the destinations
like queues and topics are configured. These are JNDI administered objects and loaded into a
JNDI tree (i.e. integral part of OpenJMS) when the server starts up.
23
You can start the server by running the “startup.bat” under C:\java\openjms-0.7.6.1\bin.
You can shutdown the server by running the “shutdown.bat” under C:\java\openjms-
0.7.6.1\bin.
Also have a look at the openjms.xml file under C:\java\openjms-0.7.6.1\config where some
default JNDI administered Connection Factories and destinations are defined.
24
Step 2: Create a new simple2 project by running the following command in a command
prompt.
Step 3: Generate eclipse metadata files like .project & .classpath by running the following
command.
C:\tutorials\simple-tutorial\simple2>mvn eclipse:eclipse
25
Step 4: Import this project into eclipse workspace as did in previous tutorials.
Run the following command to install the jms-1.0.2a.jar into our local repository at
“C:\java\.m2\repository”.
26
<?xml version="1.0"?>
<project>
<parent>
<artifactId>simple-tutorial</artifactId>
<groupId>com.mytutorial</groupId>
<version>1.0</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<groupId>com.mytutorial</groupId>
<artifactId>simple2</artifactId>
<name>simple2</name>
<version>1.0-SNAPSHOT</version>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jms</artifactId>
<version>2.0.6</version>
</dependency>
<dependency>
<groupId>javax.jms</groupId>
<artifactId>jms</artifactId>
<version>1.0.2a</version>
</dependency>
<dependency>
<groupId>openjms</groupId>
<artifactId>openjms-client</artifactId>
<version>0.7.6</version>
</dependency>
<dependency>
<groupId>exolabcore</groupId>
<artifactId>exolabcore</artifactId>
<version>0.3.7</version>
</dependency>
</dependencies>
27
</project>
Step 7: Run the following command in a command prompt to update the project build path
dependencies.
Now inside eclipse right click on “simple2” and press “F5” to refresh. Check your build path
for dependency files as shown below:
28
MyJmsService.java
package com.mytutorial;
MyJmsServiceImpl.java
package com.mytutorial;
}
receiver.processMessage();
}
JmsSender.java
package com.mytutorial;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.Session;
import org.springframework.jms.core.JmsTemplate102;
import org.springframework.jms.core.MessageCreator;
}
JmsReceiver.java
package com.mytutorial;
import javax.jms.Message;
import javax.jms.TextMessage;
import org.springframework.jms.core.JmsTemplate102;
} catch (Exception e) {
e.printStackTrace();
}
}
}
Step 9: Next, we need to modify the “App.java”, which was created before when we ran the
maven archetype command.
App.java
package com.mytutorial;
import org.springframework.context.ApplicationContext;
import
org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* OpenJMS / Spring example
*
*/
public class App {
public static void main(String[] args) throws Exception {
service.process(); Defined in
“applicationContext-
} sample2.xml”
}
Step 10: Finally we need to wire all these using spring. We will declare this via
applicationContext-simple2.xml. Firstly need to create the “resources” folder under
“simple2/src/main”. You can do this via right clicking on “simple2” and then selecting
“properties”
32
applicationContext-simple2.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-
2.0.xsd">
</beans>
34
Step 11: The final step is to run this application. Firstly make sure that the “OpenJMS” server
has started. If not start it by:
Finally run the App.java from the eclipse and also refresh the OpenJMS administrator screen
to check the message count in the destination(s).
36
You can notice the presence of a sent message. This is the reason why a
Thread.sleep(10000) was added in “MyJmsServiceImpl.java” file in the
process(…) method.
Once the message is consumed the queue1 count will be back to “0”.
Please feel free to email any errors to java-interview@hotmail.com. Also stay tuned at
http://www.lulu.com/java-success for more tutorials and Java/J2EE interview
resources.
38
Tutorial 14 – Spring JMS Asynchronous
MyListener.java
package com.mytutorial;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.TextMessage;
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans
40
http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-2.0.xsd">
<bean id="jndiTemplate"
class="org.springframework.jndi.JndiTemplate">
<property name="environment">
<props>
<prop key="java.naming.factory.initial">
org.exolab.jms.jndi.InitialContextFactory
</prop>
<prop key="java.naming.provider.url">rmi://localhost:1099</prop>
</props>
41
</property>
</bean>
</beans>
sender.sendMesage();
/*try {
Thread.sleep(10000); commented
}
}
receiver.processMessage();*/
}
Step 4: Make sure that the “OpenJMS Server” is running and then run the App.java and see
asynchronous message receipts in action.
That’s all to it. Experiment with publish/subscribe, multiple messages etc. Also refer to spring
reference documentation at http://www.springframework.org/docs/reference/index.html.
Interview Questions with Answers on JMS are discussed under “J2EE” section (subsection
JMS) in Java/J2EE Job Interview Companion at http://www.lulu.com/content/192463
Please feel free to email any errors to java-interview@hotmail.com. Also stay tuned at
http://www.lulu.com/java-success for more tutorials and Java/J2EE interview
resources.