从头到尾讲解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中Date、LocalDate、LocalDateTime、LocalTime、时间戳之间的相互转换代码

《Java中Date、LocalDate、LocalDateTime、LocalTime、时间戳之间的相互转换代码》:本文主要介绍Java中日期时间转换的多种方法,包括将Date转换为LocalD... 目录一、Date转LocalDateTime二、Date转LocalDate三、LocalDateTim

C++ vector的常见用法超详细讲解

《C++vector的常见用法超详细讲解》:本文主要介绍C++vector的常见用法,包括C++中vector容器的定义、初始化方法、访问元素、常用函数及其时间复杂度,通过代码介绍的非常详细,... 目录1、vector的定义2、vector常用初始化方法1、使编程用花括号直接赋值2、使用圆括号赋值3、ve

jupyter代码块没有运行图标的解决方案

《jupyter代码块没有运行图标的解决方案》:本文主要介绍jupyter代码块没有运行图标的解决方案,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录jupyter代码块没有运行图标的解决1.找到Jupyter notebook的系统配置文件2.这时候一般会搜索到

在Android平台上实现消息推送功能

《在Android平台上实现消息推送功能》随着移动互联网应用的飞速发展,消息推送已成为移动应用中不可或缺的功能,在Android平台上,实现消息推送涉及到服务端的消息发送、客户端的消息接收、通知渠道(... 目录一、项目概述二、相关知识介绍2.1 消息推送的基本原理2.2 Firebase Cloud Me

Python通过模块化开发优化代码的技巧分享

《Python通过模块化开发优化代码的技巧分享》模块化开发就是把代码拆成一个个“零件”,该封装封装,该拆分拆分,下面小编就来和大家简单聊聊python如何用模块化开发进行代码优化吧... 目录什么是模块化开发如何拆分代码改进版:拆分成模块让模块更强大:使用 __init__.py你一定会遇到的问题模www.

Java调用C++动态库超详细步骤讲解(附源码)

《Java调用C++动态库超详细步骤讲解(附源码)》C语言因其高效和接近硬件的特性,时常会被用在性能要求较高或者需要直接操作硬件的场合,:本文主要介绍Java调用C++动态库的相关资料,文中通过代... 目录一、直接调用C++库第一步:动态库生成(vs2017+qt5.12.10)第二步:Java调用C++

Feign Client超时时间设置不生效的解决方法

《FeignClient超时时间设置不生效的解决方法》这篇文章主要为大家详细介绍了FeignClient超时时间设置不生效的原因与解决方法,具有一定的的参考价值,希望对大家有一定的帮助... 在使用Feign Client时,可以通过两种方式来设置超时时间:1.针对整个Feign Client设置超时时间

Python基础文件操作方法超详细讲解(详解版)

《Python基础文件操作方法超详细讲解(详解版)》文件就是操作系统为用户或应用程序提供的一个读写硬盘的虚拟单位,文件的核心操作就是读和写,:本文主要介绍Python基础文件操作方法超详细讲解的相... 目录一、文件操作1. 文件打开与关闭1.1 打开文件1.2 关闭文件2. 访问模式及说明二、文件读写1.

Java编译生成多个.class文件的原理和作用

《Java编译生成多个.class文件的原理和作用》作为一名经验丰富的开发者,在Java项目中执行编译后,可能会发现一个.java源文件有时会产生多个.class文件,从技术实现层面详细剖析这一现象... 目录一、内部类机制与.class文件生成成员内部类(常规内部类)局部内部类(方法内部类)匿名内部类二、

springboot循环依赖问题案例代码及解决办法

《springboot循环依赖问题案例代码及解决办法》在SpringBoot中,如果两个或多个Bean之间存在循环依赖(即BeanA依赖BeanB,而BeanB又依赖BeanA),会导致Spring的... 目录1. 什么是循环依赖?2. 循环依赖的场景案例3. 解决循环依赖的常见方法方法 1:使用 @La