从头到尾讲解EJB MDB(消息驱动bean)三——MDB Class、Client 代码规范

2024-02-15 23:08

本文主要是介绍从头到尾讲解EJB MDB(消息驱动bean)三——MDB Class、Client 代码规范,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

七、TheMessage-Driven Bean Class

A message drivenbean must be annotated with the MessageDriven annotation or denoted in the deploymentdescriptor as a message-driven bean. The bean class need not implement the javax.ejb.MessageDrivenBeaninterface

The class mustbe defined as public.

The class cannotbe defined as abstract or final.

It must containa public constructor with no arguments.

It must notdefine the finalize method.

It isrecommended, but not required, that a message-driven bean class implement themessage listener interface for the message type it supports. A bean thatsupports the JMS API implements the javax.jms.MessageListener interface.

Unlike sessionbeans and entities, message-driven beans do not have the remote or localinterfaces that define client access. Client components do not locatemessage-driven beans and invoke methods on them. Although message-driven beansdo not have business methods, they may contain helper methods that are invokedinternally by the onMessage method.

For theGlassFish Server, the @MessageDriven annotation typically contains a mappedNameelement that specifies the JNDI name of the destination from which the beanwill consume messages. For complex message-driven beans, there can also be anactivationconfig element containing @ActivationConfigProperty annotations usedby the bean.

A message-drivenbean can also inject a MessageDrivenContext resource. Commonly you use thisresource to call the setRollbackOnly method to handle exceptions for a beanthat uses container-managed transactions.

Therefore, thefirst few lines of the SimpleMessageBean class look like this:

 

@MessageDriven(mappedName="jms/Queue",activationConfig =  {

       @ActivationConfigProperty(propertyName = "acknowledgeMode",

                                  propertyValue= "Auto-acknowledge"),

       @ActivationConfigProperty(propertyName = "destinationType",

                                  propertyValue= "javax.jms.Queue")

   })

public class SimpleMessageBean implementsMessageListener {

   @Resource

   private MessageDrivenContext mdc;

...

Property Name

Description

acknowledgeMode

Acknowledgment mode; see Controlling Message Acknowledgment for information

destinationType

Either javax.jms.Queue or javax.jms.Topic

subscriptionDurability

For durable subscribers, set to Durable; see Creating Durable Subscriptionsfor information

clientId

For durable subscribers, the client ID for the connection

subscriptionName

For durable subscribers, the name of the subscription

messageSelector

A string that filters messages; see JMS Message Selectors for information, and see An Application That Uses the JMS API with a Session Bean for an example

addressList

Remote system or systems to communicate with; see An Application Example That Consumes Messages from a Remote Server for an example

 

The onMessage Method

When the queuereceives a message, the EJB container invokes the message listener method ormethods. For a bean that uses JMS, this is the onMessage method of theMessageListener interface.

A messagelistener method must follow these rules:

The method mustbe declared as public.

The method mustnot be declared as final or static.

The onMessagemethod is called by the bean’s container when a message has arrived for thebean to service. This method contains the business logic that handles the processingof the message. It is the message-driven bean’s responsibility to parse themessage and perform the necessary business logic.

The onMessagemethod has a single argument: the incoming message.

The signature ofthe onMessage method must follow these rules:

The return typemust be void.

The method musthave a single argument of type javax.jms.Message.

In theSimpleMessageBean class, the onMessage method casts the incoming message to aTextMessage and displays the text:

 

public void onMessage(Message inMessage) {

   TextMessage msg = null;

 

   try {

       if (inMessage instanceof TextMessage) {

           msg = (TextMessage) inMessage;

           logger.info("MESSAGE BEAN: Message received: " +

                msg.getText());

       } else {

            logger.warning("Message of wrongtype: " +

               inMessage.getClass().getName());

       }

    }catch (JMSException e) {

       e.printStackTrace();

       mdc.setRollbackOnly();

    }catch (Throwable te) {

       te.printStackTrace();

    }

}

八、客户端

@Resource(mappedName="jms/ConnectionFactory")

private static ConnectionFactoryconnectionFactory;

 

@Resource(mappedName="jms/Queue")

private static Queue queue;

 

//Next, the client creates the connection,session, and message producer:

connection =connectionFactory.createConnection();

session = connection.createSession(false,Session.AUTO_ACKNOWLEDGE);

messageProducer =session.createProducer(queue);

 

//Finally, the client sends severalmessages 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);

}

这篇关于从头到尾讲解EJB MDB(消息驱动bean)三——MDB Class、Client 代码规范的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!


原文地址:
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.chinasem.cn/article/712811

相关文章

Java List排序实例代码详解

《JavaList排序实例代码详解》:本文主要介绍JavaList排序的相关资料,Java排序方法包括自然排序、自定义排序、Lambda简化及多条件排序,实现灵活且代码简洁,文中通过代码介绍的... 目录一、自然排序二、自定义排序规则三、使用 Lambda 表达式简化 Comparator四、多条件排序五、

Java 压缩包解压实现代码

《Java压缩包解压实现代码》Java标准库(JavaSE)提供了对ZIP格式的原生支持,通过java.util.zip包中的类来实现压缩和解压功能,本文将重点介绍如何使用Java来解压ZIP或RA... 目录一、解压压缩包1.zip解压代码实现:2.rar解压代码实现:3.调用解压方法:二、注意事项三、总

Linux实现简易版Shell的代码详解

《Linux实现简易版Shell的代码详解》本篇文章,我们将一起踏上一段有趣的旅程,仿照CentOS–Bash的工作流程,实现一个功能虽然简单,但足以让你深刻理解Shell工作原理的迷你Sh... 目录一、程序流程分析二、代码实现1. 打印命令行提示符2. 获取用户输入的命令行3. 命令行解析4. 执行命令

SQL Server身份验证模式步骤和示例代码

《SQLServer身份验证模式步骤和示例代码》SQLServer是一个广泛使用的关系数据库管理系统,通常使用两种身份验证模式:Windows身份验证和SQLServer身份验证,本文将详细介绍身份... 目录身份验证方式的概念更改身份验证方式的步骤方法一:使用SQL Server Management S

uniapp小程序中实现无缝衔接滚动效果代码示例

《uniapp小程序中实现无缝衔接滚动效果代码示例》:本文主要介绍uniapp小程序中实现无缝衔接滚动效果的相关资料,该方法可以实现滚动内容中字的不同的颜色更改,并且可以根据需要进行艺术化更改和自... 组件滚动通知只能实现简单的滚动效果,不能实现滚动内容中的字进行不同颜色的更改,下面实现一个无缝衔接的滚动

利用Python实现可回滚方案的示例代码

《利用Python实现可回滚方案的示例代码》很多项目翻车不是因为不会做,而是走错了方向却没法回头,技术选型失败的风险我们都清楚,但真正能提前规划“回滚方案”的人不多,本文从实际项目出发,教你如何用Py... 目录描述题解答案(核心思路)题解代码分析第一步:抽象缓存接口第二步:实现两个版本第三步:根据 Fea

Java计算经纬度距离的示例代码

《Java计算经纬度距离的示例代码》在Java中计算两个经纬度之间的距离,可以使用多种方法(代码示例均返回米为单位),文中整理了常用的5种方法,感兴趣的小伙伴可以了解一下... 目录1. Haversine公式(中等精度,推荐通用场景)2. 球面余弦定理(简单但精度较低)3. Vincenty公式(高精度,

QT6中绘制UI的两种方法详解与示例代码

《QT6中绘制UI的两种方法详解与示例代码》Qt6提供了两种主要的UI绘制技术:​​QML(QtMeta-ObjectLanguage)​​和​​C++Widgets​​,这两种技术各有优势,适用于不... 目录一、QML 技术详解1.1 QML 简介1.2 QML 的核心概念1.3 QML 示例:简单按钮

CSS引入方式和选择符的讲解和运用小结

《CSS引入方式和选择符的讲解和运用小结》CSS即层叠样式表,是一种用于描述网页文档(如HTML或XML)外观和格式的样式表语言,它主要用于将网页内容的呈现(外观)和结构(内容)分离,从而实现... 目录一、前言二、css 是什么三、CSS 引入方式1、行内样式2、内部样式表3、链入外部样式表四、CSS 选

Java进行日期解析与格式化的实现代码

《Java进行日期解析与格式化的实现代码》使用Java搭配ApacheCommonsLang3和Natty库,可以实现灵活高效的日期解析与格式化,本文将通过相关示例为大家讲讲具体的实践操作,需要的可以... 目录一、背景二、依赖介绍1. Apache Commons Lang32. Natty三、核心实现代