@TransactionAttribute有什么用?

时间:2018-02-23 06:37:27

标签: java-ee transactions ibm-mq message-driven-bean

在下面的MDB代码片段中,它将作为消息提供者部署在JBoss和IBM MQ中,@TransactionAttribute(TransactionAttributeType.NOT_SUPPORTED)的用途是什么 注解?为什么它保持在班级?

import javax.ejb.*;
import javax.jms.Message;
import javax.jms.TextMessage;
import javax.jms.JMSException;
import javax.ejb.MessageDriven;
import javax.jms.MessageListener;
import javax.ejb.ActivationConfigProperty;
import org.jboss.ejb3.annotation.ResourceAdapter;

@MessageDriven( name="MyMDB",
        activationConfig = 
        { 
            @ActivationConfigProperty(propertyName = "destinationType",propertyValue = "javax.jms.Queue"),
            @ActivationConfigProperty(propertyName = "useJNDI", propertyValue = "false"),
            @ActivationConfigProperty(propertyName = "hostName", propertyValue = "MQ.HOST.NAME"),
            @ActivationConfigProperty(propertyName = "port", propertyValue = "MQ.PORT"),
            @ActivationConfigProperty(propertyName = "channel", propertyValue = "MQ.CHANNEL.NAME"),
            @ActivationConfigProperty(propertyName = "queueManager", propertyValue = "MQ.QUEUE.MANAGER"),
            @ActivationConfigProperty(propertyName = "destination", propertyValue = "MQ.QUEUE.NAME"),
            @ActivationConfigProperty(propertyName = "transportType", propertyValue = "MQ.CLIENT")
        }) 
@ResourceAdapter(value = "wmq.jmsra.rar")
@TransactionAttribute(TransactionAttributeType.NOT_SUPPORTED)

public class MyMDB implements MessageListener{

    public void onMessage(Message message) {
        TextMessage textMessage = (TextMessage) message;
        try {
            System.out.println("nnt Message Received by MDB : "+ textMessage.getText());
        } catch (JMSException e) {
            e.printStackTrace();
        }
    }
}

1 个答案:

答案 0 :(得分:0)

您提供的代码段代表MDB的声明式事务配置,它告诉容器应该如何管理此MDB的事务。

我找不到比java事务设计策略书中的引用更好的解释这个属性:

  

NotSupported属性(Spring中的PROPAGATION_NOT_SUPPORTED)告诉容器被调用的方法不使用事务。如果某个事务已经启动,它将暂停,直到该方法完成。如果不存在事务,则容器将在不启动事务的情况下调用该方法