JMS与Message-Driven Bean使用总结

1、JMS是一个由AS提供的Message服务。它能接受消息产生者(Message Provider)所发出的消息,并把消息转发给消息消费者(Message  Consumer)。
2、JMS提供2种类型的消息服务:(1)Queue,即点对点,每个消息只转发给一个消息消费者使用。(2)Topic,即发布和订阅,每个消息可以转发
给所有的订阅者(消费者)。
3、WEBLOGIC 8下的JMS配置:
(1)配置JMS Connection Factory
(2)配置JMS File Store(目前所找到的文档都是配置File Store,其实在具体的应用中,可能JMS JDBC Store更广泛,但暂时没有找到资料)
(3)配置JMS Server
(4)在JMS Server的destinations中配置JMS Queue或者JMS Topic
其中提供给消息产生者和消息消费者使用的是JMS Connection Factory的JNDI和JMS Queue或者JMS Topic的JNDI。
4、消息产生者向JMS发送消息的步骤:
(1)使用JNDI查询对象JMS ConnectionFactory和Destination(JMS Queue/Topic)
(2)使用管理对象JMS ConnectionFactory建立连接Connection
(3)使用连接Connection 建立会话Session
(4)使用会话Session和管理对象Destination创建消息生产者MessageSender
(5)使用消息生产者MessageSender发送消息
一个消息发送者的例子:
package myjms;

import java.util.*;
import javax.naming.*;
import javax.jms.*;

public class MessageProducter {
  public static void main(String[] args) {
    String queueConnectionFactoryName = "myjmsconnectionfactory"; //JMS Connection Factory的JNDI
    String queueName = "myjmsqueue"; //JMS Queue或者JMS Topic的JNDI

    boolean transacted = false;//transaction模式
    int acknowledgementMode = Session.AUTO_ACKNOWLEDGE;//acknowledgement模式
    String message="Message need to send";//模拟需要发送的消息

    Properties properties = new Properties();
    properties.put(Context.INITIAL_CONTEXT_FACTORY,"weblogic.jndi.WLInitialContextFactory");
    properties.put(Context.PROVIDER_URL, "t3://localhost:7001");

    try {
      Context context = new InitialContext(properties);
      Object obj = context.lookup(queueConnectionFactoryName);
      QueueConnectionFactory queueConnectionFactory = (QueueConnectionFactory) obj;//JMS Connection Factory的获得
     
      obj = context.lookup(queueName);
      Queue queue = (Queue) obj;//JMS Queue或者JMS Topic的获得

      QueueConnection queueConnection=queueConnectionFactory.createQueueConnection();//产生连接
      queueConnection.start();
      QueueSession queueSession = queueConnection.createQueueSession(transacted, acknowledgementMode);
      TextMessage textMessage = queueSession.createTextMessage();
      textMessage.clearBody();
      textMessage.setText(message);
      QueueSender queueSender = queueSession.createSender(queue);
      queueSender.send(textMessage);
      if (transacted) {
        queueSession.commit();
      }

      if (queueSender != null) {
        queueSender.close();
      }
      if (queueSession != null) {
        queueSession.close();
      }
      if (queueConnection != null) {
        queueConnection.close();
      }

    }
    catch(Exception ex){
      ex.printStackTrace();
    }
  }
}

5、消息消费者从JMS接受消息的步骤:
(1)使用JNDI查询对象JMS ConnectionFactory和Destination(JMS Queue/Topic)
(2)使用管理对象JMS ConnectionFactory建立连接Connection
(3)使用连接Connection 建立会话Session
(4)使用会话Session和管理对象Destination创建消息消费者MessageReceiver
(5)使用消息消费者MessageReceiver接受消息,需要用setMessageListener将MessageListener接口绑定到MessageReceiver
消息消费者必须实现了MessageListener接口,需要定义onMessage事件方法。
一个消息消费者的例子:
package myjms;

import java.util.*;
import javax.naming.*;
import javax.jms.*;

public class MessageReciever
    implements MessageListener {
  public void onMessage(Message message) {
    if (message instanceof TextMessage) {
      TextMessage textMessage = (TextMessage) message;
      try {
        System.out.println("Message content is:" + textMessage.getText());
      }
      catch (JMSException e) {
        e.printStackTrace();
      }
    }
  }

  public static void main(String[] args) {
   
    MessageReciever msgRcvr=new MessageReciever();
    String queueConnectionFactoryName = "myjmsconnectionfactory";
    String queueName = "myjmsqueue";

    boolean transacted = false;
    int acknowledgementMode = Session.AUTO_ACKNOWLEDGE;

    Properties properties = new Properties();
    properties.put(Context.INITIAL_CONTEXT_FACTORY,
                   "weblogic.jndi.WLInitialContextFactory");
    properties.put(Context.PROVIDER_URL, "t3://localhost:7001");

    try {
      Context context = new InitialContext(properties);
      Object obj = context.lookup(queueConnectionFactoryName);
      QueueConnectionFactory queueConnectionFactory = (QueueConnectionFactory)
          obj;

      obj = context.lookup(queueName);
      Queue queue = (Queue) obj;

      QueueConnection queueConnection = queueConnectionFactory.
          createQueueConnection();
      queueConnection.start();
      QueueSession queueSession = queueConnection.createQueueSession(transacted,
          acknowledgementMode);
      QueueReceiver queueReceiver = queueSession.createReceiver(queue);

      queueReceiver.setMessageListener(msgRcvr);

      synchronized(msgRcvr){
        msgRcvr.wait(100000);
      }

      if (queueReceiver != null) {
        queueReceiver.close();
      }
      if (queueSession != null) {
        queueSession.close();
      }
      if (queueConnection != null) {
        queueConnection.close();
      }

    }
    catch (Exception ex) {
      ex.printStackTrace();
    }
  }
}

6、Message-driven Bean
MDB实际上就是一个消息消费者的客户端程序。它由AS EJB Container来管理。在JBUILDER生成一个MDB非常简单。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
一、JMS基本概念   1.1 P2P通信   1.2 Pub/Sub通信 二、JMS消息 三、JMS P2P编程   3.1 使用JMS QueueConnection对象   3.2 处理回退事件   3.3 关闭JMS对象   3.4 接收消息   3.5 消息驱动的Bean   3.6 消息持久化   3.7 消息选择器 四、JMS Pub/Sub编程 五、二阶段提交的事务 ━━━━━━━━━━━━━━━━━━━━━━━━━━   EJB 2.0和J2EE 1.3规范开始提供对Java消息服务(JMS)的支持。在J2EE 1.3加入JMS之前,J2EE环境中的组件通过RMI-IIOP协议通信,J2EE是一个完全同步的平台。由于在J2EE 1.3规范中引入了JMS,J2EE环境开始具备一项极其重要的功能--异步通信。   ● 说明:RMI-IIOP是Java远程方法调用(RMI,Remote Method Invocation)的一个兼容CORBA的版本,CORBA是Common Object Request Broker Architecture的缩写,即公用对象请求代理(调度)体系结构。RMI-IIOP通过CORBA平台和语言中立的通信协议发送RMI消息。   为了支持异步通信,J2EE 1.3规范还引入了一种新的EJB类型:消息驱动的Bean,即Message Driven Bean,简称MDB。如前所述,在JMS之前,J2EE原来是一个建立在Java RMI-IIOP通信协议基础上的同步环境,但MDB却具有接收异步消息的能力。   异步通信使得企业应用能够建立在一种全新的通信机制之上,它具有如下重要优点:   ■ 异步通信程序不直接交换信息。由于这个原因,通信的任意一方能够在对方停止响应时继续自己的操作,例如当接收消息的一方不能响应时,发送方仍能够发出消息。
Event-driven programming is a programming paradigm that allows the software to respond to events or user actions in a asynchronous manner. Here are the general steps to implement an event-driven program: 1. Identify the events: Determine the events that the program needs to respond to. These could be user actions, system events, or external signals. 2. Define event handlers: Create functions or methods to handle each event. These functions will be executed when the event occurs. 3. Register event handlers: Associate each event handler with the corresponding event. This is usually done using a framework or library. 4. Run the program: Once the event handlers are registered, the program is ready to execute. As events occur, the associated event handlers will be executed. 5. Clean up: After the program finishes executing, clean up any resources used or allocated during the program. Here's an example of an event-driven program in Python using the Tkinter GUI library: ```python import tkinter as tk def button_click(event): print("Button clicked") root = tk.Tk() button = tk.Button(root, text="Click me") button.bind("<Button-1>", button_click) button.pack() root.mainloop() ``` In this program, we identify the event as a button click, define an event handler function `button_click` to handle the event, register the event handler with the button using the `bind` method, and run the program using the `mainloop` method. When the button is clicked, the `button_click` function will be executed and print "Button clicked" to the console.

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值