`
VerRan
  • 浏览: 456999 次
  • 性别: Icon_minigender_1
  • 来自: 陕西.西安
社区版块
存档分类
最新评论

Message-Driven Bean

阅读更多

What Is a Message-Driven Bean?

A message-driven bean is an enterprise bean that allows J2EE applications to process messages asynchronously. It normally acts as a JMS message listener, which is similar to an event listener except that it receives JMS messages instead of events. The messages can be sent by any J2EE component--an application client, another enterprise bean, or a web component--or by a JMS application or system that does not use J2EE technology. Message-driven beans can process either JMS messages or other kinds of messages.

For a simple code sample, see Chapter 28. For more information about using message-driven beans, see Using the JMS API in a J2EE Application and Chapter 34.

What Makes Message-Driven Beans Different from Session and Entity Beans?

The most visible difference between message-driven beans and session and entity beans is that clients do not access message-driven beans through interfaces. Interfaces are described in the section Defining Client Access with Interfaces. Unlike a session or entity bean, a message-driven bean has only a bean class.

In several respects, a message-driven bean resembles a stateless session bean.

The instance variables of the message-driven bean instance can contain some state across the handling of client messages--for example, a JMS API connection, an open database connection, or an object reference to an enterprise bean object.

Client components do not locate message-driven beans and invoke methods directly on them. Instead, a client accesses a message-driven bean through JMS by sending messages to the message destination for which the message-driven bean class is the MessageListener. You assign a message-driven bean's destination during deployment by using Application Server resources.

Message-driven beans have the following characteristics:

When a message arrives, the container calls the message-driven bean's onMessage method to process the message. The onMessage method normally casts the message to one of the five JMS message types and handles it in accordance with the application's business logic. The onMessage method can call helper methods, or it can invoke a session or entity bean to process the information in the message or to store it in a database.

A message can be delivered to a message-driven bean within a transaction context, so all operations within the onMessage method are part of a single transaction. If message processing is rolled back, the message will be redelivered. For more information, see Chapter 28.

When to Use Message-Driven Beans

Session beans and entity beans allow you to send JMS messages and to receive them synchronously, but not asynchronously. To avoid tying up server resources, you may prefer not to use blocking synchronous receives in a server-side component. To receive messages asynchronously, use a message-driven bean.

评论
1 楼 VerRan 2007-09-05  
One great benefit of EJB is that you don’t need to write thread-safe code. You
design your enterprise beans as single-threaded components and never need to
worry about thread synchronization when concurrent clients access your
component. In order to service concurrent client requests, your EJB container
automatically instantiates multiple instances of your component.
The container’s thread services can be both a benefit and a restriction. The
benefit is that you don’t need to worry about race conditions or deadlock in
your application code. The restriction is that some problems lend themselves
well to multithreaded programming, and that class of problems cannot be
easily solved in an EJB environment.
So why doesn’t the EJB specification allow for multithreaded beans? EJB is
intended to relieve the component developers’ worry about threads or thread
synchronization. The EJB container handles those issues for you by load
balancing client requests to multiple instances of a single-threaded component.
An EJB server provides a highly scalable environment for single-threaded
components.
If the EJB specification allowed for beans to control threads, a Pandora’s box
of problems would result. For example, an EJB container would have a very
hard time controlling transactions if beans were randomly starting and
stopping threads, especially because transaction information is often
associated with a thread.
The bottom line is that EJB was not meant to be a Swiss army knife, solving
every problem in existence. It was designed to assist with server-side business
problems, which are largely single-threaded. For applications that absolutely
must be multithreaded, EJB may not be the correct choice of distributed object
architectures.

相关推荐

    基于java的开发源码-Message-Driven Bean EJB实例源代码.zip

    基于java的开发源码-Message-Driven Bean EJB实例源代码.zip 基于java的开发源码-Message-Driven Bean EJB实例源代码.zip 基于java的开发源码-Message-Driven Bean EJB实例源代码.zip 基于java的开发源码-Message-...

    Message-Driven Bean EJB实例源代码

    - 使用`@MessageDriven`注解标记该类,指定JMS目的地类型(队列或主题),以及其他配置属性。 - 在ejb-jar.xml或元数据注解中配置MDB,如JNDI名称、消息驱动适配器等。 6. **源代码解析**: 压缩包中的源代码...

    基于Java的实例源码-Message-Driven Bean EJB实例源代码.zip

    2. **编写MDB类**:MDB类需要实现`javax.ejb.MessageDrivenBean`接口或者继承`javax.jms.MessageListener`接口,并标注`@MessageDriven`注解。这个注解包含了JMS目的地的配置信息,如目的地类型、激活配置等。 3. *...

    基于Java的实例开发源码-Message-Driven Bean EJB实例源代码.zip

    "基于Java的实例开发源码-Message-Driven Bean EJB实例源代码.zip" 这个标题揭示了本次讨论的核心内容,即一个关于Java编程的实例项目,特别是涉及了Java企业版(Java EE)中的Message-Driven Bean(MDB)组件。...

    基于Java的Message-Driven Bean EJB实例源代码.zip

    Java Message-Driven Bean(MDB)是企业级Java(EJB)技术的一部分,它主要用于处理Java消息服务(JMS)中的消息。在Java EE环境中,MDBs是无状态的bean,它们作为后台服务运行,监听消息队列或主题,并对到来的消息...

    基于java的Message-Driven Bean EJB实例源代码.zip

    - 使用`@MessageDriven`注解来声明bean是一个MDB,并指定JMS配置,如目的地类型(队列或主题)、消息驱动适配器等。 - 可以通过`@ActivationConfigProperty`注解进一步配置MDB的行为,例如设置消息选择器、最大...

    基于Java的源码-Message-Driven Bean EJB实例源代码.zip

    Java中的Message-Driven Bean (MDB) 是Enterprise JavaBeans (EJB) 规范的一部分,用于处理JMS(Java Message Service)消息。这个压缩包“基于Java的源码-Message-Driven Bean EJB实例源代码.zip”显然包含了实现...

    java源码:Message-Driven Bean EJB实例源代码.zip

    在EJB项目中,我们需要在ejb-jar.xml或@MessageDriven注解中配置MDB的相关属性,如消息监听接口、JMS目的地类型(队列或主题)、JNDI名称等。 **4. MDB的源码分析** 在提供的源码中,我们可能会看到以下关键组件: ...

    Message-Driven Bean EJB实例源代码.7z

    7. **部署**:在EJB 3.1及更高版本中,MDB的部署变得更加简单,可以使用注解(如`@MessageDriven`)来代替XML配置,进一步简化了开发过程。 在这个源代码实例中,你可能会看到以下几个关键部分: 1. **MDB类**:...

    Message-Driven Bean 培训PPT

    ### Message-Driven Bean (MDB) 培训知识点解析 #### 一、Java消息服务(JMS) **1.1 Java消息服务概念** Java消息服务(Java Message Service,简称JMS)是一种面向消息中间件的标准API,它使得Java应用程序能够...

    Message-Driven Bean EJB实例源代码.rar

    1. **MDB类定义**:MDB类需要实现 javax.jms.MessageListener 接口,并且通常会带有 @MessageDriven 注解。例如: ```java import javax.ejb.MessageDriven; import javax.jms.MessageListener; import javax.jms....

    小程序 Message-Driven Bean EJB实例源代码(源码).rar

    免责声明:资料部分来源于合法的互联网渠道收集和整理,部分自己学习积累成果,供大家学习参考与交流。收取的费用仅用于收集和整理资料耗费时间的酬劳。 本人尊重原创作者或出版方,资料版权归原作者或出版方所有,...

    SpringMVC源码总结(三)mvc:annotation-driven和mvc:message-converters简单介绍

    在Spring MVC框架中,`mvc:annotation-driven`和`mvc:message-converters`是两个非常重要的元素,它们在处理基于注解的控制器和数据转换方面起着关键作用。本篇文章将深入探讨这两个组件的工作原理以及如何在实际...

    EJB3.0开发Message Driven Bean

    @MessageDriven(activationConfig={ @ActivationConfigProperty(propertyName="destinationType", propertyValue="javax.jms.Queue"), @ActivationConfigProperty(propertyName="destination", propertyValue=...

    J2EE程序设计复习题.doc

    - Session Bean和Message-Driven Bean的事务处理通常采用CMT(Container-Managed Transactions),而Entity Bean可以采用BMT或CMP。 7. 事务处理属性: - EJB的事务属性包括Supports、Should、NotSupported、...

    EJB3图文教程之开发Message Driven Bean

    EJB3图文教程之开发Message Driven Bean

Global site tag (gtag.js) - Google Analytics