Skip to content

Commit

Permalink
Add jms client example (#53)
Browse files Browse the repository at this point in the history
* Add JMS Client Example for Payara Micro 172

* Added correct license
  • Loading branch information
smillidge authored May 10, 2017
1 parent a6f820b commit 7231be7
Show file tree
Hide file tree
Showing 9 changed files with 293 additions and 2 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,6 @@ derby.log
target/
.idea/
*.iml
/JMSClient/nbproject/private/
/JMSClient/build/
/JMSClient/dist/
23 changes: 23 additions & 0 deletions Payara-Micro/JMS-Client-Example/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# JMS Client Example
(from Payara Micro 172 onwards)

This Payara Micro example shows how to send and receive JMS messages to ActiveMQ from Payara Micro.

The example consists of an MDB that receives messages from a Queue names TESTQ and a Timer Bean that
periodically sends messages to the same Queue.

For this example to run you must first install ActiveMQ and ensure it is running on standard ports.
You must also download the ActiveMQ rar file, in particular version activemq-rar-5.14.5. If you have a different download
version of ActiveMQ, modify the source code that refers to the rar to the same version as you have downloaded.

To run the application on Payara Micro once built ensure that you deploy both the rar file and the ejb jar.

```shell
java -jar payara-micro.jar --autobindhttp --deploy activemq-rar-5.14.5.rar --deploy JMS-Client-Example-1.0-SNAPSHOT.jar
```

When the application is running you should see output like;

```shell
Got message ActiveMQTextMessage {commandId = 13, responseRequired = true, messageId = ID:MintyFresh-36284-1494434785461-3:1:2:1:1, originalDestination = null, originalTransactionId = null, producerId = ID:MintyFresh-36284-1494434785461-3:1:2:1, destination = queue://TESTQ, transactionId = null, expiration = 0, timestamp = 1494434795008, arrival = 0, brokerInTime = 1494434795008, brokerOutTime = 1494434795010, correlationId = null, replyTo = null, persistent = true, type = null, priority = 4, groupID = null, groupSequence = 0, targetConsumerId = null, compressed = false, userID = null, content = org.apache.activemq.util.ByteSequence@2fee6b02, marshalledProperties = null, dataStructure = null, redeliveryCounter = 0, size = 0, properties = null, readOnlyProperties = true, readOnlyBody = true, droppable = false, jmsXGroupFirstForConsumer = false, text = This is a test at Wed May 10 17:46:35 BST 2017}
```
19 changes: 19 additions & 0 deletions Payara-Micro/JMS-Client-Example/nb-configuration.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?xml version="1.0" encoding="UTF-8"?>
<project-shared-configuration>
<!--
This file contains additional configuration written by modules in the NetBeans IDE.
The configuration is intended to be shared among all the users of project and
therefore it is assumed to be part of version control checkout.
Without this configuration present, some functionality in the IDE may be limited or fail altogether.
-->
<properties xmlns="http://www.netbeans.org/ns/maven-properties-data/1">
<!--
Properties that influence various parts of the IDE, especially code formatting and the like.
You can copy and paste the single properties, into the pom.xml file and the IDE will pick them up.
That way multiple projects can share the same settings (useful for formatting rules for example).
Any value defined here will override the pom.xml file value but is only applicable to the current project.
-->
<org-netbeans-modules-maven-j2ee.netbeans_2e_hint_2e_j2eeVersion>1.7</org-netbeans-modules-maven-j2ee.netbeans_2e_hint_2e_j2eeVersion>
<org-netbeans-modules-maven-j2ee.netbeans_2e_hint_2e_deploy_2e_server>gfv3ee6</org-netbeans-modules-maven-j2ee.netbeans_2e_hint_2e_deploy_2e_server>
</properties>
</project-shared-configuration>
81 changes: 81 additions & 0 deletions Payara-Micro/JMS-Client-Example/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
<?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>
<parent>
<artifactId>payaramicro</artifactId>
<groupId>fish.payara.examples</groupId>
<version>1.0-SNAPSHOT</version>
</parent>

<groupId>fish.payara.examples.micro.jms</groupId>
<artifactId>JMS-Client-Example</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>ejb</packaging>

<name>JMS-Client-Example</name>

<properties>
<endorsed.dir>${project.build.directory}/endorsed</endorsed.dir>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<dependencies>
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-api</artifactId>
<version>7.0</version>
<scope>provided</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
<compilerArguments>
<endorseddirs>${endorsed.dir}</endorseddirs>
</compilerArguments>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-ejb-plugin</artifactId>
<version>2.3</version>
<configuration>
<ejbVersion>3.1</ejbVersion>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.6</version>
<executions>
<execution>
<phase>validate</phase>
<goals>
<goal>copy</goal>
</goals>
<configuration>
<outputDirectory>${endorsed.dir}</outputDirectory>
<silent>true</silent>
<artifactItems>
<artifactItem>
<groupId>javax</groupId>
<artifactId>javaee-endorsed-api</artifactId>
<version>7.0</version>
<type>jar</type>
</artifactItem>
</artifactItems>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
*
* Copyright (c) [2016-2017] Payara Foundation and/or its affiliates. All rights reserved.
*
* The contents of this file are subject to the terms of either the GNU
* General Public License Version 2 only ("GPL") or the Common Development
* and Distribution License("CDDL") (collectively, the "License"). You
* may not use this file except in compliance with the License. You can
* obtain a copy of the License at
* https://github.com/payara/Payara/blob/master/LICENSE.txt
* See the License for the specific
* language governing permissions and limitations under the License.
*
* When distributing the software, include this License Header Notice in each
* file and include the License file at glassfish/legal/LICENSE.txt.
*
* GPL Classpath Exception:
* The Payara Foundation designates this particular file as subject to the "Classpath"
* exception as provided by the Payara Foundation in the GPL Version 2 section of the License
* file that accompanied this code.
*
* Modifications:
* If applicable, add the following below the License Header, with the fields
* enclosed by brackets [] replaced by your own identifying information:
* "Portions Copyright [year] [name of copyright owner]"
*
* Contributor(s):
* If you wish your version of this file to be governed by only the CDDL or
* only the GPL Version 2, indicate your decision by adding "[Contributor]
* elects to include this software in this distribution under the [CDDL or GPL
* Version 2] license." If you don't indicate a single choice of license, a
* recipient has the option to distribute your version of this file under
* either the CDDL, the GPL Version 2 or to extend the choice of license to
* its licensees as provided above. However, if you add GPL Version 2 code
* and therefore, elected the GPL Version 2 license, then the option applies
* only if the new code is made subject to such option by the copyright
* holder.
*/
package fish.payara.examples.payaramicro.jms;

import javax.ejb.ActivationConfigProperty;
import javax.ejb.MessageDriven;
import javax.jms.Message;
import javax.jms.MessageListener;



@MessageDriven(name = "testmdb", activationConfig = {
@ActivationConfigProperty(propertyName = "destinationType", propertyValue = "javax.jms.Queue"),
@ActivationConfigProperty(propertyName = "destination", propertyValue = "TESTQ"),
@ActivationConfigProperty(propertyName = "resourceAdapter", propertyValue = "activemq-rar-5.14.5")

})
public class ReceiveMessage implements MessageListener {

public ReceiveMessage() {
}

@Override
public void onMessage(Message message) {
System.out.println("Got message " + message);
}
}


Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
*
* Copyright (c) [2016-2017] Payara Foundation and/or its affiliates. All rights reserved.
*
* The contents of this file are subject to the terms of either the GNU
* General Public License Version 2 only ("GPL") or the Common Development
* and Distribution License("CDDL") (collectively, the "License"). You
* may not use this file except in compliance with the License. You can
* obtain a copy of the License at
* https://github.com/payara/Payara/blob/master/LICENSE.txt
* See the License for the specific
* language governing permissions and limitations under the License.
*
* When distributing the software, include this License Header Notice in each
* file and include the License file at glassfish/legal/LICENSE.txt.
*
* GPL Classpath Exception:
* The Payara Foundation designates this particular file as subject to the "Classpath"
* exception as provided by the Payara Foundation in the GPL Version 2 section of the License
* file that accompanied this code.
*
* Modifications:
* If applicable, add the following below the License Header, with the fields
* enclosed by brackets [] replaced by your own identifying information:
* "Portions Copyright [year] [name of copyright owner]"
*
* Contributor(s):
* If you wish your version of this file to be governed by only the CDDL or
* only the GPL Version 2, indicate your decision by adding "[Contributor]
* elects to include this software in this distribution under the [CDDL or GPL
* Version 2] license." If you don't indicate a single choice of license, a
* recipient has the option to distribute your version of this file under
* either the CDDL, the GPL Version 2 or to extend the choice of license to
* its licensees as provided above. However, if you add GPL Version 2 code
* and therefore, elected the GPL Version 2 license, then the option applies
* only if the new code is made subject to such option by the copyright
* holder.
*/
package fish.payara.examples.payaramicro.jms;

import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.Resource;
import javax.ejb.Schedule;
import javax.ejb.Stateless;
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.JMSException;
import javax.jms.Queue;
import javax.jms.Session;
import javax.resource.AdministeredObjectDefinition;
import javax.resource.ConnectionFactoryDefinition;

/**
* An example Timer Bean to send messages to an ActiveMQ broker
*
* @author Steve Millidge <Payara Services Limited>
*/
@Stateless
@ConnectionFactoryDefinition ( name = "java:global/jms/SendJMS",
interfaceName = "javax.jms.ConnectionFactory",
resourceAdapter = "activemq-rar-5.14.5",
properties = {"UserName=admin","Password=admin","ServerUrl=tcp://127.0.0.1:61616"})

@AdministeredObjectDefinition ( resourceAdapter = "activemq-rar-5.14.5",
interfaceName = "javax.jms.Queue",
className = "org.apache.activemq.command.ActiveMQQueue",
name = "java:global/jms/TestQ",
properties = {"PhysicalName=TESTQ"})
public class SendJMSMessage {

@Resource(lookup = "java:global/jms/TestQ")
Queue queue;

@Resource(lookup = "java:global/jms/SendJMS")
ConnectionFactory factory;

@Schedule(hour = "*", minute = "*", second = "*/5", info = "Every 5 second timer", timezone = "UTC", persistent = false)
public void myTimer() {
try (Connection conn = factory.createConnection()){
Session sess = conn.createSession(true,Session.AUTO_ACKNOWLEDGE);
sess.createProducer(queue).send(sess.createTextMessage("This is a test at " + new Date()));
} catch (JMSException ex) {
Logger.getLogger(SendJMSMessage.class.getName()).log(Level.SEVERE, null, ex);
}
}

// Add business logic below. (Right-click in editor and choose
// "Insert Code > Add Business Method")
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Manifest-Version: 1.0

4 changes: 4 additions & 0 deletions Payara-Micro/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,7 @@ An example Spring Boot application using JSF and JPA that runs on Payara Micro
## Spring Boot REST Example

An example Spring Boot REST service that runs on Payara Micro

## JMS Client Example

An example demonstrating sending and receiving messages on an ActiveMQ using Payara Micro.
5 changes: 3 additions & 2 deletions Payara-Micro/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
<module>simplest-bootstrap</module>
<module>spring-boot-jpa-jsf-example</module>
<module>spring-boot-rest-example</module>
</modules>
<module>JMS-Client-Example</module>
</modules>

</project>
</project>

0 comments on commit 7231be7

Please sign in to comment.