# Embedded Example

This example shows how to setup and run HornetQ embedded.

HornetQ was designed to use POJOs (Plain Old Java Objects), what makes embedding HornetQ as simple as instantiating a few objects.

In this example, we are using two jars:

- hornetq-core.jar
- netty.jar

HornetQ Embedded could be used from very simple use cases with only InVM support to very complex cases with clustering, persistence and fail over.

## Example step-by-step

To run the example, simply type `./build.sh` (or `build.bat` on windows) from this directory

In this we don't use any configuration files. (Everything is embedded). We simply instantiate ConfigurationImpl, HornetQServer, start it and operate on JMS regularly

1. Create the Configuration, and set the properties accordingly

               Configuration configuration = new ConfigurationImpl();
               configuration.setEnablePersistence(false);
               configuration.setSecurityEnabled(false);
               configuration.getAcceptorConfigurations().add(new TransportConfiguration(InVMAcceptorFactory.class.getName()));

2. Create and start the server

               HornetQServer server = HornetQ.newHornetQServer(configuration);
               server.start();

3. As we are not using a JNDI environment we instantiate the objects directly

               ServerLocator serverLocator = HornetQClient.createServerLocatorWithoutHA(new TransportConfiguration(InVMConnectorFactory.class.getName()));
               ClientSessionFactory sf = serverLocator.createSessionFactory();

4. Create a Core Queue

               ClientSession coreSession = sf.createSession(false, false, false);
               final String queueName = "queue.exampleQueue";
               coreSession.createQueue(queueName, queueName, true);
               coreSession.close();

5. Create the session and producer

                session = sf.createSession();

                ClientProducer producer = session.createProducer(queueName);

6. Create and send a Message

               ClientMessage message = session.createMessage(false);
               message.putStringProperty(propName, "Hello sent at " + new Date());
               System.out.println("Sending the message.");
               producer.send(message);

7. Create the message consumer and start the connection

               ClientConsumer messageConsumer = session.createConsumer(queueName);
               session.start();

8. Receive the message

               ClientMessage messageReceived = messageConsumer.receive(1000);
               System.out.println("Received TextMessage:" + messageReceived.getProperty(propName));

9. Be sure to close our resources!

               if (sf != null)
               {
                  sf.close();
               }

10. Stop the server

               server.stop();