开发学院软件开发Java EnterpriseJavaBeansDistilled... 阅读

EnterpriseJavaBeansDistilled...

 2007-12-23 12:27:42 来源:WEB开发网   
核心提示:消息驱动bean(续)jms 应用客户程序to get a better idea of how JMS is used, we can create a java application whose sole purpose is receiving and PRocessing reservation messag
消息驱动bean(续)

jms 应用客户程序

to get a better idea of how JMS is used, we can create a java application whose sole purpose is receiving and PRocessing reservation messages. We will develop a very simple JMS client that simply prints a description of each ticket as it receives the messages. We'll assume that the TravelAgent EJB is using the TextMessage to send a description of the ticket to the JMS clients. The following code shows how the JMS application client might look:

为更好的理解如何使用JMS,可以开发一个Java应用,其唯一的用途在于接收和处理预定消息。在这里,将开发一个非常简单的JMS客户。当该客户接收到消息时,会打印出所每张票的描述信息。假定TravelAgent EJB使用TextMessage以发送票的描述给JMS客户。下列代码显示了JMS应用客户可能的样子:

import javax.jms.Message;

import javax.jms.TextMessage;

import javax.jms.TopicConnectionFactory;

import javax.jms.TopicConnection;

import javax.jms.Topicsession;

import javax.jms.Topic;

import javax.jms.Session;

import javax.jms.TopicSubscriber;

import javax.jms.JMSException;

import javax.naming.InitialContext;





public class JmsClient_1 implements javax.jms.MessageListener {



  public static void main(String[]args) throws Exception {

    

    if(args.length != 2)

      throw new Exception("Wrong number of arguments");

    

    new JmsClient_1(args[0], args[1]);

    

    while(true){Thread.sleep(10000);}

    

  }

    

  public JmsClient_1(String factoryName, String topicName) throws Exception {

      

    InitialContext jndiContext = getInitialContext();

    

    TopicConnectionFactory factory = (TopicConnectionFactory)

      jndiContext.lookup("TopicFactoryNameGoesHere");

    

    Topic topic = (Topic)jndiContext.lookup("TopicNameGoesHere");



    TopicConnection connect = factory.createTopicConnection();



    TopicSession session =

      connect.createTopicSession(false,Session.AUTO_ACKNOWLEDGE);



    TopicSubscriber subscriber = session.createSubscriber(topic);



    subscriber.setMessageListener(this);

    

    connect.start();

  }

  

  public void onMessage(Message message) {

    try {

    

      TextMessage textMsg = (TextMessage)message;

      String text = textMsg.getText();

      System.out.println("\n RESERVATION RECIEVED:\n"+text);

    

    } catch(JMSException jmsE) {

      jmsE.printStackTrace();

    }

  }

  

  public static InitialContext getInitialContext() {

    // 创建具体产品厂商的JNDI上下文

  }

}

  JmsClient_1的构建器含有来自JNDI InitialContext的TopicConnectionFactory和Topic。这些对象是使用具体厂商产品的properties创建的,从而使得客户能够连接到TravelAgent EJB使用的同一JMS供应者。比如,WebLogic应用服务器中getInitialContext()方法的代码如下:(JNDI也允许将properties放置在jndi.properties文件中,该文件包含有用于InitialContext的property值,并能够在运行时被动态找到。本书中,显式的给出了properties值。)

public static InitialContext getInitialContext() {

  Properties env = new Properties();

  env.put(Context.SECURITY_PRINCIPAL, "guest");

  env.put(Context.SECURITY_CREDENTIALS, "guest");

  env.put(Context.INITIAL_CONTEXT_FACTORY,

    "weblogic.jndi.WLInitialContextFactory");

  env.put(Context.PROVIDER_URL, "t3://localhost:7001");

  return new InitialContext(env);

}

一旦客户获得了TopicConnectionFactory和Topic,就可以采取和TravelAgent EJB相同的方式创建TopicConnection和TopicSession。它们的主要区别在于这里的TopicSession对象创建了TopicSubscriber对象,而不是TopicPublisher。其中,TopicSubscriber被明确设计成处理来自特定Topic的消息:

TopicSession session = 

  connect.createTopicSession(false,Session.AUTO_ACKNOWLEDGE);



TopicSubscriber subscriber = session.createSubscriber(topic);



subscriber.setMessageListener(this);

    

connect.start();

TopicSubscriber能直接接收消息,或者能够将消息的处理委派给接口javax.jms.MessageLister。在例子中,JmsClient_1实现了MessageListener接口,使得它可以处理消息。MessageListener对象实现了单一的方法,onMessage(),每当新消息发送到订阅者的topic中时,该方法都会被调用。在这里,每次TravelAgent EJB发送预定消息到topic中时,JMS客户中的onMessage()方法都会被调用,使得它能够接收到消息的拷贝并处理它:

public void onMessage(Message message) {

  try {

    TextMessage textMsg = (TextMessage)message;

    String text = textMsg.getText();

    System.out.println("\n RESERVATION RECIEVED:\n"+text);

    

  } catch(JMSException jmsE) {

    jmsE.printStackTrace();

  }

}


待续。。。。。。。。

bill-转自:csdn进入讨论组讨论。

(出处:http://www.cncms.com)


Tags:EnterpriseJavaBeansDistilled

编辑录入:爽爽 [复制链接] [打 印]
[]
  • 好
  • 好的评价 如果觉得好,就请您
      0%(0)
  • 差
  • 差的评价 如果觉得差,就请您
      0%(0)
赞助商链接