Putting the buzz in messaging
Connecting to the Embedded HornetQ
If using the core API, just create the ClientSessionFactory and use the regular core API.
ClientSessionFactory nettyFactory = HornetQClient.createClientSessionFactory(
new TransportConfiguration(
InVMConnectorFactory.class.getName()));
ClientSession session = factory.createSession();
session.createQueue("example", "example", true);
ClientProducer producer = session.createProducer("example");
ClientMessage message = session.createMessage(true);
message.getBody().writeString("Hello");
producer.send(message);
session.start();
ClientConsumer consumer = session.createConsumer("example");
ClientMessage msgReceived = consumer.receive();
System.out.println("message = " + msgReceived.getBody().readString());
session.close();
Connection on an Embedded HornetQ through JMS is also simple. Just instantiate ConnectionFactory directly. The following example illustrates that.
ConnectionFactory cf =
HornetQJMSClient.createConnectionFactory(
new TransportConfiguration(InVMConnectorFactory.class.getName()));
Connection conn = cf.createConnection();
conn.start();
Session sess = conn.createSession(true, Session.SESSION_TRANSACTED);
MessageProducer prod = sess.createProducer(queue);
TextMessage msg = sess.createTextMessage("Hello!");
prod.send(msg);
sess.commit();
MessageConsumer consumer = sess.createConsumer(queue);
TextMessage txtmsg = (TextMessage)consumer.receive();
System.out.println("Msg = " + txtmsg.getText());
sess.commit();
conn.close();
Please see Section 11.2.1, “Embedded” for an example which shows how to setup and run HornetQ embedded with JMS.
No comments:
Post a Comment