-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit e46c664
Showing
5 changed files
with
243 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
<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>com.test.testspringjmsmq</groupId> | ||
<artifactId>testspringjmsmq</artifactId> | ||
<version>1.0-SNAPSHOT</version> | ||
<packaging>jar</packaging> | ||
|
||
<name>testspringjmsmq</name> | ||
<url>http://maven.apache.org</url> | ||
|
||
<properties> | ||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | ||
<javaVersion>1.5</javaVersion> | ||
<webSphereMQVersion>7.0.1</webSphereMQVersion> | ||
<springJMSVersion>3.0.3.RELEASE</springJMSVersion> | ||
<springTestVersion>3.0.5.RELEASE</springTestVersion> | ||
<junitVersion>4.6</junitVersion> | ||
</properties> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>com.ibm</groupId> | ||
<artifactId>com.ibm.mqjms</artifactId> | ||
<version>${webSphereMQVersion}</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>com.ibm</groupId> | ||
<artifactId>com.ibm.mq.jmqi</artifactId> | ||
<version>${webSphereMQVersion}</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>com.ibm</groupId> | ||
<artifactId>com.ibm.disthub2.dhbcore</artifactId> | ||
<version>${webSphereMQVersion}</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.springframework</groupId> | ||
<artifactId>spring-jms</artifactId> | ||
<version>${springJMSVersion}</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.springframework</groupId> | ||
<artifactId>spring-test</artifactId> | ||
<version>${springTestVersion}</version> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>junit</groupId> | ||
<artifactId>junit</artifactId> | ||
<version>${junitVersion}</version> | ||
<scope>test</scope> | ||
</dependency> | ||
</dependencies> | ||
|
||
<build> | ||
<plugins> | ||
<plugin> | ||
<inherited>true</inherited> | ||
<groupId>org.apache.maven.plugins</groupId> | ||
<artifactId>maven-compiler-plugin</artifactId> | ||
<configuration> | ||
<source>${javaVersion}</source> | ||
<target>${javaVersion}</target> | ||
<optimize>true</optimize> | ||
<debug>true</debug> | ||
</configuration> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
</project> |
53 changes: 53 additions & 0 deletions
53
src/main/java/com/test/testspringjmsmq/MessageService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package com.test.testspringjmsmq; | ||
|
||
import javax.jms.JMSException; | ||
import javax.jms.Message; | ||
import javax.jms.Session; | ||
import javax.jms.TextMessage; | ||
|
||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.jms.core.JmsTemplate; | ||
import org.springframework.jms.core.MessageCreator; | ||
|
||
/** | ||
* A service that sends and receives JMS messages. | ||
* | ||
*/ | ||
public class MessageService | ||
{ | ||
@Autowired | ||
private JmsTemplate jmsTemplate; | ||
@Value("${queue}") | ||
private String destination; | ||
|
||
/** | ||
* Sends a message to a queue. | ||
* | ||
* @param text Message text. | ||
*/ | ||
public void sendMessage(final String text) { | ||
jmsTemplate.send(destination, new MessageCreator() { | ||
public Message createMessage(Session session) throws JMSException { | ||
return session.createTextMessage(text); | ||
} | ||
}); | ||
} | ||
|
||
/** | ||
* Receives a message from a queue. | ||
* | ||
* @return Message text. | ||
* @throws JMSException | ||
*/ | ||
public String readMessage() throws JMSException { | ||
String message = null; | ||
|
||
Message msg = jmsTemplate.receive(destination); | ||
if(msg instanceof TextMessage) { | ||
message = ((TextMessage) msg).getText(); | ||
} | ||
|
||
return message; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
queue=TestQueue001 | ||
queue_manager=TEST_QM001 | ||
queue_hostname=localhost | ||
queue_port=1414 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
<?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" | ||
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"> | ||
|
||
<context:property-placeholder location="classpath:main.properties" /> | ||
|
||
<!-- WebSphere MQ Connection Factory --> | ||
<bean id="mqConnectionFactory" class="com.ibm.mq.jms.MQQueueConnectionFactory"> | ||
<property name="hostName"> | ||
<value>${queue_hostname}</value> | ||
</property> | ||
<property name="port"> | ||
<value>${queue_port}</value> | ||
</property> | ||
<property name="queueManager"> | ||
<value>${queue_manager}</value> | ||
</property> | ||
<property name="transportType"> | ||
<value>1</value> | ||
</property> | ||
</bean> | ||
|
||
<!-- JMS Queue Connection Factory --> | ||
<bean id="jmsQueueConnectionFactory" | ||
class="org.springframework.jms.connection.SingleConnectionFactory102"> | ||
<property name="targetConnectionFactory"> | ||
<ref bean="mqConnectionFactory" /> | ||
</property> | ||
<property name="pubSubDomain"> | ||
<value>false</value> | ||
</property> | ||
</bean> | ||
|
||
<!-- JMS Destination Resolver --> | ||
<bean id="jmsDestinationResolver" | ||
class="org.springframework.jms.support.destination.DynamicDestinationResolver"> | ||
</bean> | ||
|
||
<!-- JMS Queue Template --> | ||
<bean id="jmsQueueTemplate" class="org.springframework.jms.core.JmsTemplate102"> | ||
<property name="connectionFactory"> | ||
<ref bean="jmsQueueConnectionFactory" /> | ||
</property> | ||
<property name="destinationResolver"> | ||
<ref bean="jmsDestinationResolver" /> | ||
</property> | ||
<property name="pubSubDomain"> | ||
<value>false</value> | ||
</property> | ||
<property name="receiveTimeout"> | ||
<value>20000</value> | ||
</property> | ||
</bean> | ||
|
||
<bean id="messageService" class="com.test.testspringjmsmq.MessageService" /> | ||
</beans> |
53 changes: 53 additions & 0 deletions
53
src/test/java/com/test/testspringjmsmq/MessageServiceTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package com.test.testspringjmsmq; | ||
|
||
import static org.junit.Assert.*; | ||
|
||
import javax.jms.JMSException; | ||
|
||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.test.context.ContextConfiguration; | ||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; | ||
|
||
/** | ||
* Unit tests for <code>MessageService</code>. | ||
* | ||
*/ | ||
@RunWith(SpringJUnit4ClassRunner.class) | ||
@ContextConfiguration(locations="/spring.xml") | ||
public class MessageServiceTest | ||
{ | ||
@Autowired | ||
private MessageService messageService; | ||
private String message; | ||
|
||
/** | ||
* Test setup. | ||
*/ | ||
@Before | ||
public void setUp() { | ||
message = "TestSpringJMSMQ test message."; | ||
} | ||
|
||
/** | ||
* Test that sends a message to a queue. | ||
*/ | ||
@Test | ||
public void testSendMessage() { | ||
messageService.sendMessage(message); | ||
} | ||
|
||
/** | ||
* Test that receives a message from a queue. | ||
* | ||
* @throws JMSException | ||
*/ | ||
@Test | ||
public void testReadMessage() throws JMSException { | ||
String readMessage = messageService.readMessage(); | ||
|
||
assertEquals(readMessage, message); | ||
} | ||
} |