This example shows how to run an A-MQ Broker, Producer and Consumer with SSL within JBoss Developer Studio.
Broker
1. Create a Maven project using the maven-archetype-quickstart with artifactId and groupId
<groupId>summit.example.activemq</groupId>
<artifactId>broker</artifactId>
2. Add the broker.ks and broker.ts files for the truststore and keystore from the conf directory from apache-activemq-5.8.0
3. Update the pom.xml with the following which has the maven-activemq-plugin
<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>summit.example.activemq</groupId>
<artifactId>broker</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>broker</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.activemq.tooling</groupId>
<artifactId>maven-activemq-plugin</artifactId>
<version>5.1</version>
<configuration>
<configUri>xbean:file:activemq.xml</configUri>
<fork>false</fork>
<systemProperties>
<property>
<name>org.apache.activemq.default.directory.prefix</name>
<value>./target/</value>
</property>
</systemProperties>
</configuration>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring</artifactId>
<version>2.5.5</version>
</dependency>
<dependency>
<groupId>org.mortbay.jetty</groupId>
<artifactId>jetty-xbean</artifactId>
<version>6.1.11</version>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-activemq</artifactId>
<version>1.1.0</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
</project>
4. Create the A-MQ activemq.xml file. Notice the sslContext and the uri in the transportConnector.
<?xml version="1.0"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:amq="http://activemq.apache.org/schema/core"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://activemq.apache.org/schema/core
http://activemq.apache.org/schema/core/activemq-core.xsd">
<broker xmlns="http://activemq.apache.org/schema/core" brokerName="localhost" dataDirectory="./data">
<sslContext>
<sslContext keyStore="file:broker.ks"
keyStorePassword="password"
trustStore="file:broker.ts"
trustStorePassword="password"/>
</sslContext>
<!-- The transport connectors ActiveMQ will listen to -->
<transportConnectors>
<transportConnector name="ssl" uri="ssl://localhost:61001"/>
</transportConnectors>
</broker>
</beans>
5. Next add the ActiveMQ Broker run configuration with the correct goal org.apache.activemq.tooling:maven-activemq-plugin:5.2.0:run
6. Run the Broker Configuration and view the output for the running broker listening on the port defined in activemq.xml
Consumer
7. Create a Maven project using the maven-archetype-quickstart
<groupId>summit.example.activemq</groupId>
<artifactId>consumer</artifactId>
8. Update the pom.xml
<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>summit.example.activemq</groupId>
<artifactId>consumer</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>consumer</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>javax.jms</groupId>
<artifactId>jms</artifactId>
<version>1.1</version>
<type>pom</type>
</dependency>
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-core</artifactId>
<version>5.2.0</version>
</dependency>
</dependencies>
</project>
9. Create the consumer.java class
package summit.example.activemq.consumer;
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageConsumer;
import javax.jms.MessageListener;
import javax.jms.Session;
import javax.jms.TextMessage;
import org.apache.activemq.ActiveMQConnectionFactory;
public class Consumer implements MessageListener
{
public static String brokerURL = "ssl://localhost:61001";
private Connection connection;
private Session session;
private MessageConsumer consumer;
public static void main( String[] args )
{
Consumer app = new Consumer();
app.run();
}
public void run()
{
try
{
ConnectionFactory factory = new ActiveMQConnectionFactory(brokerURL);
connection = factory.createConnection();
connection.start();
session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
Destination destination = session.createQueue("test");
consumer = session.createConsumer(destination);
consumer.setMessageListener(this);
}
catch (Exception e)
{
System.out.println("Caught:" + e);
e.printStackTrace();
}
}
public void onMessage(Message message)
{
try
{
if (message instanceof TextMessage)
{
TextMessage txtMessage = (TextMessage)message;
System.out.println("Message received: " + txtMessage.getText());
}
else
{
System.out.println("Invalid message received.");
}
}
catch (JMSException e)
{
System.out.println("Caught:" + e);
e.printStackTrace();
}
}
}
10. Create the run configuration for the consumer with the goal
clean compile exec:java -Dexec.mainClass=summit.example.activemq.consumer.Consumer
11. Add the ssl parameters to the JVM parameters in the consumer run configuration
-Djavax.net.ssl.keyStore=client.ks -Djavax.net.ssl.keyStorePassword=password -Djavax.net.ssl.trustStore=client.ts -Djavax.net.ssl.trustStorePassword=password
12. Run the consumer run configuration. The consumer should start and wait on messages.
Producer
13.
Create the producer similar to the consumer.
pom.xml
<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>summit.example.activemq</groupId>
<artifactId>producer</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>producer</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>javax.jms</groupId>
<artifactId>jms</artifactId>
<version>1.1</version>
<type>pom</type>
</dependency>
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-core</artifactId>
<version>5.2.0</version>
</dependency>
</dependencies>
</project>
producer.java
package summit.example.activemq.producer;
import javax.jms.ConnectionFactory;
import org.apache.activemq.ActiveMQConnectionFactory;
import javax.jms.Connection;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageProducer;
import javax.jms.Session;
public class Producer {
public static String brokerURL = "ssl://localhost:61001";
private ConnectionFactory factory;
private Connection connection;
private Session session;
private MessageProducer producer;
public static void main( String[] args ) throws Exception
{
// setup the connection to ActiveMQ
ConnectionFactory factory = new ActiveMQConnectionFactory(brokerURL);
Producer producer = new Producer(factory, "test");
producer.run();
producer.close();
}
public Producer(ConnectionFactory factory, String queueName) throws JMSException
{
this.factory = factory;
connection = factory.createConnection();
connection.start();
session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
Destination destination = session.createQueue(queueName);
producer = session.createProducer(destination);
}
public void run() throws JMSException
{
for (int i = 0; i < 100; i++)
{
System.out.println("Creating Message " + i);
Message message = session.createTextMessage("Hello World!");
producer.send(message);
}
}
public void close() throws JMSException
{
if (connection != null)
{
connection.close();
}
}
}
run configuration
goal: clean compile exec:java -Dexec.mainClass=summit.example.activemq.producer.Producer
JVM params: -Djavax.net.ssl.keyStore=client.ks -Djavax.net.ssl.keyStorePassword=password -Djavax.net.ssl.trustStore=client.ts -Djavax.net.ssl.trustStorePassword=password
14. Run the producer to see messages sent and messages received
producer output
consumer output