从头到尾讲解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

相关文章

活用c4d官方开发文档查询代码

当你问AI助手比如豆包,如何用python禁止掉xpresso标签时候,它会提示到 这时候要用到两个东西。https://developers.maxon.net/论坛搜索和开发文档 比如这里我就在官方找到正确的id描述 然后我就把参数标签换过来

poj 1258 Agri-Net(最小生成树模板代码)

感觉用这题来当模板更适合。 题意就是给你邻接矩阵求最小生成树啦。~ prim代码:效率很高。172k...0ms。 #include<stdio.h>#include<algorithm>using namespace std;const int MaxN = 101;const int INF = 0x3f3f3f3f;int g[MaxN][MaxN];int n

MySQL高性能优化规范

前言:      笔者最近上班途中突然想丰富下自己的数据库优化技能。于是在查阅了多篇文章后,总结出了这篇! 数据库命令规范 所有数据库对象名称必须使用小写字母并用下划线分割 所有数据库对象名称禁止使用mysql保留关键字(如果表名中包含关键字查询时,需要将其用单引号括起来) 数据库对象的命名要能做到见名识意,并且最后不要超过32个字符 临时库表必须以tmp_为前缀并以日期为后缀,备份

Linux_kernel驱动开发11

一、改回nfs方式挂载根文件系统         在产品将要上线之前,需要制作不同类型格式的根文件系统         在产品研发阶段,我们还是需要使用nfs的方式挂载根文件系统         优点:可以直接在上位机中修改文件系统内容,延长EMMC的寿命         【1】重启上位机nfs服务         sudo service nfs-kernel-server resta

计算机毕业设计 大学志愿填报系统 Java+SpringBoot+Vue 前后端分离 文档报告 代码讲解 安装调试

🍊作者:计算机编程-吉哥 🍊简介:专业从事JavaWeb程序开发,微信小程序开发,定制化项目、 源码、代码讲解、文档撰写、ppt制作。做自己喜欢的事,生活就是快乐的。 🍊心愿:点赞 👍 收藏 ⭐评论 📝 🍅 文末获取源码联系 👇🏻 精彩专栏推荐订阅 👇🏻 不然下次找不到哟~Java毕业设计项目~热门选题推荐《1000套》 目录 1.技术选型 2.开发工具 3.功能

代码随想录冲冲冲 Day39 动态规划Part7

198. 打家劫舍 dp数组的意义是在第i位的时候偷的最大钱数是多少 如果nums的size为0 总价值当然就是0 如果nums的size为1 总价值是nums[0] 遍历顺序就是从小到大遍历 之后是递推公式 对于dp[i]的最大价值来说有两种可能 1.偷第i个 那么最大价值就是dp[i-2]+nums[i] 2.不偷第i个 那么价值就是dp[i-1] 之后取这两个的最大值就是d

pip-tools:打造可重复、可控的 Python 开发环境,解决依赖关系,让代码更稳定

在 Python 开发中,管理依赖关系是一项繁琐且容易出错的任务。手动更新依赖版本、处理冲突、确保一致性等等,都可能让开发者感到头疼。而 pip-tools 为开发者提供了一套稳定可靠的解决方案。 什么是 pip-tools? pip-tools 是一组命令行工具,旨在简化 Python 依赖关系的管理,确保项目环境的稳定性和可重复性。它主要包含两个核心工具:pip-compile 和 pip

D4代码AC集

贪心问题解决的步骤: (局部贪心能导致全局贪心)    1.确定贪心策略    2.验证贪心策略是否正确 排队接水 #include<bits/stdc++.h>using namespace std;int main(){int w,n,a[32000];cin>>w>>n;for(int i=1;i<=n;i++){cin>>a[i];}sort(a+1,a+n+1);int i=1

Spring 源码解读:自定义实现Bean定义的注册与解析

引言 在Spring框架中,Bean的注册与解析是整个依赖注入流程的核心步骤。通过Bean定义,Spring容器知道如何创建、配置和管理每个Bean实例。本篇文章将通过实现一个简化版的Bean定义注册与解析机制,帮助你理解Spring框架背后的设计逻辑。我们还将对比Spring中的BeanDefinition和BeanDefinitionRegistry,以全面掌握Bean注册和解析的核心原理。

ActiveMQ—消息特性(延迟和定时消息投递)

ActiveMQ消息特性:延迟和定时消息投递(Delay and Schedule Message Delivery) 转自:http://blog.csdn.net/kimmking/article/details/8443872 有时候我们不希望消息马上被broker投递出去,而是想要消息60秒以后发给消费者,或者我们想让消息没隔一定时间投递一次,一共投递指定的次数。。。 类似