从头到尾讲解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中Springboot集成Kafka实现消息发送和接收功能

《Java中Springboot集成Kafka实现消息发送和接收功能》Kafka是一个高吞吐量的分布式发布-订阅消息系统,主要用于处理大规模数据流,它由生产者、消费者、主题、分区和代理等组件构成,Ka... 目录一、Kafka 简介二、Kafka 功能三、POM依赖四、配置文件五、生产者六、消费者一、Kaf

python实现pdf转word和excel的示例代码

《python实现pdf转word和excel的示例代码》本文主要介绍了python实现pdf转word和excel的示例代码,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价... 目录一、引言二、python编程1,PDF转Word2,PDF转Excel三、前端页面效果展示总结一

在MyBatis的XML映射文件中<trim>元素所有场景下的完整使用示例代码

《在MyBatis的XML映射文件中<trim>元素所有场景下的完整使用示例代码》在MyBatis的XML映射文件中,trim元素用于动态添加SQL语句的一部分,处理前缀、后缀及多余的逗号或连接符,示... 在MyBATis的XML映射文件中,<trim>元素用于动态地添加SQL语句的一部分,例如SET或W

使用C#代码计算数学表达式实例

《使用C#代码计算数学表达式实例》这段文字主要讲述了如何使用C#语言来计算数学表达式,该程序通过使用Dictionary保存变量,定义了运算符优先级,并实现了EvaluateExpression方法来... 目录C#代码计算数学表达式该方法很长,因此我将分段描述下面的代码片段显示了下一步以下代码显示该方法如

VUE动态绑定class类的三种常用方式及适用场景详解

《VUE动态绑定class类的三种常用方式及适用场景详解》文章介绍了在实际开发中动态绑定class的三种常见情况及其解决方案,包括根据不同的返回值渲染不同的class样式、给模块添加基础样式以及根据设... 目录前言1.动态选择class样式(对象添加:情景一)2.动态添加一个class样式(字符串添加:情

python多进程实现数据共享的示例代码

《python多进程实现数据共享的示例代码》本文介绍了Python中多进程实现数据共享的方法,包括使用multiprocessing模块和manager模块这两种方法,具有一定的参考价值,感兴趣的可以... 目录背景进程、进程创建进程间通信 进程间共享数据共享list实践背景 安卓ui自动化框架,使用的是

SpringBoot项目删除Bean或者不加载Bean的问题解决

《SpringBoot项目删除Bean或者不加载Bean的问题解决》文章介绍了在SpringBoot项目中如何使用@ComponentScan注解和自定义过滤器实现不加载某些Bean的方法,本文通过实... 使用@ComponentScan注解中的@ComponentScan.Filter标记不加载。@C

SpringBoot生成和操作PDF的代码详解

《SpringBoot生成和操作PDF的代码详解》本文主要介绍了在SpringBoot项目下,通过代码和操作步骤,详细的介绍了如何操作PDF,希望可以帮助到准备通过JAVA操作PDF的你,项目框架用的... 目录本文简介PDF文件简介代码实现PDF操作基于PDF模板生成,并下载完全基于代码生成,并保存合并P

SpringBoot基于MyBatis-Plus实现Lambda Query查询的示例代码

《SpringBoot基于MyBatis-Plus实现LambdaQuery查询的示例代码》MyBatis-Plus是MyBatis的增强工具,简化了数据库操作,并提高了开发效率,它提供了多种查询方... 目录引言基础环境配置依赖配置(Maven)application.yml 配置表结构设计demo_st

SpringCloud集成AlloyDB的示例代码

《SpringCloud集成AlloyDB的示例代码》AlloyDB是GoogleCloud提供的一种高度可扩展、强性能的关系型数据库服务,它兼容PostgreSQL,并提供了更快的查询性能... 目录1.AlloyDBjavascript是什么?AlloyDB 的工作原理2.搭建测试环境3.代码工程1.