# Java EE SSL Servlet Example

This example shows you how to configure and use servlet transport over SSL with HornetQ.

## JBoss AS configuration

Please refer to HornetQ Quickstart guide to install it in JBoss AS 5

## Example Configuration

In the configuration, the hornetq.example.keystore is the key store file holding client certificate. The hornetq.example.truststore is the file for server to hold trusted client certificates. They are pre-generated for illustration purpose1.

## Example step-by-step

To deploy and start the server, type simply type `./build.sh deploy` (or `build.bat deploy` on windows)from the example directory Once the server has started type simply type `./build.sh run` (or `build.bat run` on windows) to run the example.
To remove the new profile type simply type `./build.sh undeploy` (or `build.bat undeploy` on windows).

1. First we need to get an initial context so we can look-up the JMS connection factory and destination objects from JNDI. This initial context will get it's properties from the `jndi.properties` file in the directory `config`

               initialContext = new InitialContext();

2. We look up the JMS queue object from JNDI

               Queue queue = (Queue) initialContext.lookup("/queue/testQueue");

3. We look up the JMS connection factory object from JNDI

               ConnectionFactory cf = (ConnectionFactory) initialContext.lookup("/TestServletConnectionFactory");

4. We create a JMS connection

               connection = cf.createConnection();

5. We create a JMS session. The session is created as non transacted and will auto acknowledge messages.

               Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);

6. We create a JMS message producer on the session. This will be used to send the messages.

              MessageProducer messageProducer = session.createProducer(queue);

7. We create a JMS text messages that we are going to send.

                TextMessage message = session.createTextMessage("This is a text message");

8. We send messages to the queue

               messageProducer.send(message);

9. We create a JMS Message Consumer

               MessageConsumer messageConsumer = session.createConsumer(queue);

10. We start the connection

                connection.start();

11. We receive the message

                TextMessage messageReceived = (TextMessage) messageConsumer.receive(5000);

12. And finally, always remember to close your JMS connections and resources after use, in a `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();
                  }
               }

1. The stores were generating using the following commands ↩:
   - create the keystore: `keytool -genkey -keystore hornetq.example.keystore -storepass hornetqexample`
   - export the certificate: `keytool -export -keystore hornetq.example.keystore -file hornetq.cer`
   - create the truststore: `keytool -import -file hornetq.cer -keystore hornetq.example.truststore -storepass hornetqexample`