The Application Client

The SimpleMessageClient sends messages to the queue that the SimpleMessageBean listens to. The client starts by locating the connection factory and queue:

connectionFactory = 
  (ConnectionFactory) jndiContext.lookup(
    "java:comp/env/jms/MyConnectionFactory");
destination = 
  (Queue) jndiContext.lookup("java:comp/env/jms/QueueName"); 

Next, the client creates the queue connection, session, and sender:

connection = connectionFactory.createConnection();
session = connection.createSession(false,
  Session.AUTO_ACKNOWLEDGE);
messageProducer = session.createProducer(destination); 

Finally, the client sends several messages to the queue:

message = session.createTextMessage();

for (int i = 0; i < NUM_MSGS; i++) {
  message.setText("This is message " + (i + 1));
  System.out.println("Sending message: " +
    message.getText());
  messageProducer.send(message);
}