This example shows you how to configure SSL with ActiveMQ to send and receive message.
Using SSL can make your messaging applications interact with ActiveMQ securely. An application can be secured transparently without extra coding effort. To secure your messaging application with SSL, you need to configure connector and acceptor as follows:
<!-- Connector -->
<connector name="netty-ssl-connector">
<factory-class>org.apache.activemq.core.remoting.impl.netty.NettyConnectorFactory</factory-class>
<param key="hornetq.remoting.netty.host" value="localhost" type="String"/>
<param key="hornetq.remoting.netty.port" value="5500" type="Integer"/>
<param key="hornetq.remoting.netty.ssl-enabled" value="true" type="Boolean"/>
<param key="hornetq.remoting.netty.trust-store-path" value="server0/hornetq.example.truststore" type="String"/>
<param key="hornetq.remoting.netty.trust-store-password" value="hornetqexample" type="String"/>
</connector>
<!-- Acceptor -->
<acceptor name="netty-ssl-acceptor">
<factory-class>org.apache.activemq.core.remoting.impl.netty.NettyAcceptorFactory</factory-class>
<param key="hornetq.remoting.netty.host" value="localhost" type="String"/>
<param key="hornetq.remoting.netty.port" value="5500" type="Integer"/>
<param key="hornetq.remoting.netty.ssl-enabled" value="true" type="Boolean"/>
<param key="hornetq.remoting.netty.key-store-path" value="hornetq.example.keystore" type="String"/>
<param key="hornetq.remoting.netty.key-store-password" value="hornetqexample" type="String"/>
</acceptor>
In the configuration, the hornetq.example.keystore is the key store file holding the server's certificate. The hornetq.example.truststore is the file holding the certificates which the client trusts (i.e. the server's certificate exported from hornetq.example.keystore). They are pre-generated for illustration purpose1.
To run the example, simply type mvn verify
from this directory
client-jndi.properties
file in the directory ../common/config
InitialContext initialContext = getContext();
Queue queue = (Queue) initialContext.lookup("/queue/exampleQueue");
ConnectionFactory cf = (ConnectionFactory) initialContext.lookup("/ConnectionFactory");
connection = cf.createConnection();
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
MessageProducer messageProducer = session.createProducer(topic);
TextMessage message = session.createTextMessage("This is a text message");
messageProducer.send(message);
MessageConsumer messageConsumer = session.createConsumer(queue);
connection.start();
TextMessage messageReceived = (TextMessage) messageConsumer.receive(5000);
finally
block. Closing a JMS connection will automatically close all of its sessions, consumers, producer and browser objects
finally
{
if (initialContext != null)
{
initialContext.close();
}
if (connection != null)
{
connection.close();
}
}
keytool -genkey -keystore hornetq.example.keystore -storepass hornetqexample
keytool -export -keystore hornetq.example.keystore -file hornetq.cer
keytool -import -file hornetq.cer -keystore hornetq.example.truststore -storepass hornetqexample