站内搜索: 请输入搜索关键词
当前页面: 在线文档首页 > The J2EE 1.4 Tutorial

The Application Client - The J2EE 1.4 Tutorial

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);
}