Eric_STF 2015-09-14 01:14 采纳率: 0%
浏览 2133
已采纳

为什么在JMS中使用JTA事务控制全局事务失效

package cn.producer;

import java.util.Properties;

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.ExceptionListener;
import javax.jms.JMSException;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.transaction.HeuristicMixedException;
import javax.transaction.HeuristicRollbackException;
import javax.transaction.NotSupportedException;
import javax.transaction.RollbackException;
import javax.transaction.SystemException;
import javax.transaction.UserTransaction;

public class ExceptionMessageSender {
/**

  • 本地获取Context
  • 需要添加wlclient.jar , webservices.jar , wljmsclient.jar
  • @return */ private Context getInitialContext(){ Context context = null; final String INIT_FACTORY = "weblogic.jndi.WLInitialContextFactory"; final String SERVER_URL = "t3://localhost:7001"; Properties props = new Properties(); props.put(Context.INITIAL_CONTEXT_FACTORY, INIT_FACTORY); props.put(Context.PROVIDER_URL, SERVER_URL); try { context = new InitialContext(props); } catch (NamingException e) { e.printStackTrace(); } return context; }

public void sendMessage() throws NamingException, JMSException, NotSupportedException, SystemException, SecurityException, IllegalStateException, RollbackException, HeuristicMixedException, HeuristicRollbackException{
//定义默认的weblogic的JNDI
final String CONNECTION_FACTORY_JNDI = "weblogic.jms.ConnectionFactory";
//获取Context
Context context = getInitialContext();
//获取连接工厂
ConnectionFactory cf = (ConnectionFactory) context.lookup(CONNECTION_FACTORY_JNDI);
//获取连接
Connection conn = cf.createConnection();
//监听JMS服务器异常情况(只有当JMS服务器运行时发生的异常才会触发异常监听器)
conn.setExceptionListener(new ExceptionListener(){
@Override
public void onException(JMSException e) {
System.out.println("JMS服务器发生异常,异常信息如下:");
e.printStackTrace();
}
});
//获取JTA事务
UserTransaction tx = (UserTransaction) context.lookup("javax.transaction.UserTransaction");
tx.begin();
//获取会话,如果设置为事务性会话,那么JTA不起作用
Session session = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
//获取消息目的
Destination dest = (Destination) context.lookup("MessageQueue");
//通过会话获取消息生产者
MessageProducer sender = session.createProducer(dest);
//通过会话创建一个空格JMS消息
TextMessage msg = session.createTextMessage();
msg.setText("你好,我是路人甲!");
//通过消息生产者发送消息
sender.send(msg);
try{
int num = 4/0;//-----------这里会抛出异常,引发JTA全局事务回滚
msg.setText(num+":number");
sender.send(msg);
tx.commit();
}catch(Exception e){
tx.rollback();
}finally{
//关闭资源
session.close();
conn.close();
}

}
}

package cn.consumer;

import java.util.Properties;

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.ExceptionListener;
import javax.jms.JMSException;
import javax.jms.MessageConsumer;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.transaction.HeuristicMixedException;
import javax.transaction.HeuristicRollbackException;
import javax.transaction.NotSupportedException;
import javax.transaction.RollbackException;
import javax.transaction.SystemException;
import javax.transaction.UserTransaction;

public class ExceptionConsumer {
/**

  • 本地获取Context
  • 需要添加wlclient.jar , webservices.jar , wljmsclient.jar
  • @return */ private Context getInitialContext(){ Context context = null; final String INIT_FACTORY = "weblogic.jndi.WLInitialContextFactory"; final String SERVER_URL = "t3://localhost:7001"; Properties props = new Properties(); props.put(Context.INITIAL_CONTEXT_FACTORY, INIT_FACTORY); props.put(Context.PROVIDER_URL, SERVER_URL); try { context = new InitialContext(props); } catch (NamingException e) { e.printStackTrace(); } return context; }

public void receiveMessage() throws NamingException, JMSException, NotSupportedException, SystemException, SecurityException, IllegalStateException, RollbackException, HeuristicMixedException, HeuristicRollbackException{
//定义默认的weblogic连接工程的JNDI
final String CONNECTION_FACTORY_JNDI = "weblogic.jms.ConnectionFactory";
//获取Context
Context context = getInitialContext();
//通过JNDI获取连接工厂
ConnectionFactory cf = (ConnectionFactory) context.lookup(CONNECTION_FACTORY_JNDI);
//通过连接工厂获取连接
Connection conn = cf.createConnection();
//开始JMS传输
conn.start();
//设置异常监听器,监听JMS服务器上的异常
conn.setExceptionListener(new ExceptionListener(){
public void onException(JMSException e){
System.out.println("JMS服务器发生异常,异常信息如下:");
e.printStackTrace();
}
});
//打开全局事务
UserTransaction tx = (UserTransaction) context.lookup("javax.transaction.UserTransaction");
tx.begin();
//通过连接获取会话,使用全局事务了,就不应该设置为事务性会话,否则全局事务会失效,使用的事务还是事务性会话的事务
Session session = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
//获取消息目的(消息队列)
Destination dest = (Destination) context.lookup("MessageQueue");
//创建消息消费者
MessageConsumer receiver = session.createConsumer(dest);
//消息消费者同步接收消息
TextMessage msg = (TextMessage) receiver.receiveNoWait();
System.out.println(msg);
if(msg != null){
System.out.println("同步接收到信息:"+msg.getText());
}
//确认事务
tx.commit();
//关闭资源
session.close();
conn.close();
}
}

package cn.test;

import javax.jms.JMSException;
import javax.naming.NamingException;
import javax.transaction.HeuristicMixedException;
import javax.transaction.HeuristicRollbackException;
import javax.transaction.NotSupportedException;
import javax.transaction.RollbackException;
import javax.transaction.SystemException;

import cn.producer.ExceptionMessageSender;

public class TestExceptionProducer {
public static void main(String[] args) {
ExceptionMessageSender ems = new ExceptionMessageSender();
try {
ems.sendMessage();
} catch (SecurityException e) {
e.printStackTrace();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (NamingException e) {
e.printStackTrace();
} catch (JMSException e) {
e.printStackTrace();
} catch (NotSupportedException e) {
e.printStackTrace();
} catch (SystemException e) {
e.printStackTrace();
} catch (RollbackException e) {
e.printStackTrace();
} catch (HeuristicMixedException e) {
e.printStackTrace();
} catch (HeuristicRollbackException e) {
e.printStackTrace();
}
}
}


代码贴在上面,我的意思就是不用JMS自带的事务,而用JTA来控制全局事务。但是没找到失效的原因。

  • 写回答

2条回答 默认 最新

  • Eric_STF 2015-09-18 13:22
    关注

    终于被我搞清楚了,原来是Connection和ConnectionFactory的问题。Connection是不支持JTA全局事务的,要用weblogic.jms.XAConnetion,
    而XAConnection通过weblogic.jms.XAConnetionFactory获取。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 R语言卸载之后无法重装,显示电脑存在下载某些较大二进制文件行为,怎么办
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?
  • ¥15 关于#vue.js#的问题:修改用户信息功能图片无法回显,数据库中只存了一张图片(相关搜索:字符串)
  • ¥15 texstudio的问题,