Artemis - 如何避免非事务会话的 TransactionRolledBackException
创始人
2024-11-10 20:30:15
0

解决非事务会话导致TransactionRolledBackException的方法可以是配置Artemis以确保会话在非事务模式下运行。以下是一个示例代码,其中演示了如何使用Artemis配置和处理非事务会话。

首先,您需要创建一个Artemis服务器实例,并使用以下配置启动它:

import org.apache.activemq.artemis.core.config.impl.ConfigurationImpl;
import org.apache.activemq.artemis.core.server.ActiveMQServer;
import org.apache.activemq.artemis.core.server.ActiveMQServers;

public class ArtemisServerExample {

    public static void main(String[] args) throws Exception {
        // 创建并配置服务器实例
        ConfigurationImpl configuration = new ConfigurationImpl();
        configuration.addAcceptorConfiguration("tcp", "tcp://localhost:61616");
        configuration.setPersistenceEnabled(false); // 禁用持久化
        configuration.setSecurityEnabled(false); // 禁用安全性

        // 启动服务器
        ActiveMQServer server = ActiveMQServers.newActiveMQServer(configuration);
        server.start();

        // 在这里执行您的应用程序逻辑

        // 关闭服务器
        server.stop();
    }
}

接下来,您可以使用以下代码示例在非事务模式下发送和接收消息:

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageConsumer;
import javax.jms.MessageProducer;
import javax.jms.Queue;
import javax.jms.Session;
import org.apache.activemq.artemis.jms.client.ActiveMQConnectionFactory;

public class ArtemisClientExample {

    public static void main(String[] args) throws JMSException {
        // 创建连接工厂
        ConnectionFactory connectionFactory = new ActiveMQConnectionFactory("tcp://localhost:61616");

        // 创建连接
        Connection connection = connectionFactory.createConnection();

        // 创建会话
        Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); // 非事务模式

        // 创建队列
        Queue queue = session.createQueue("exampleQueue");

        // 创建消息生产者
        MessageProducer producer = session.createProducer(queue);

        // 创建消息
        Message message = session.createTextMessage("Hello, Artemis!");

        // 发送消息
        producer.send(message);

        // 创建消息消费者
        MessageConsumer consumer = session.createConsumer(queue);

        // 接收消息
        Message receivedMessage = consumer.receive();

        // 处理接收到的消息
        System.out.println("Received message: " + receivedMessage.getBody(String.class));

        // 关闭连接
        connection.close();
    }
}

在上述代码示例中,我们通过将会话的事务标志设置为false来确保会话在非事务模式下运行。这样,如果在发送或接收消息时发生错误,会话将不会回滚事务,并且不会引发TransactionRolledBackException。

相关内容

热门资讯

总结透视!wepoker辅助器... 总结透视!wepoker辅助器软件下载,hhpoker的辅助是真的吗,模块教程(有挂分享)-哔哩哔哩...
科普透视!epoker透视!真... 科普透视!epoker透视!真是真的是有辅助插件(有挂解惑)-哔哩哔哩1、游戏颠覆性的策略玩法,独创...
必备透视!德州透视是真的吗,w... 必备透视!德州透视是真的吗,wpk免费辅助,项目教程(证实有挂)-哔哩哔哩1、用户打开应用后不用登录...
教你透视!werplan怎么作... 教你透视!werplan怎么作必弊!竟然是有辅助教程(有挂秘诀)-哔哩哔哩教你透视!werplan怎...
详情透视!智星德州插件怎么下载... 详情透视!智星德州插件怎么下载,德普之星透视软件免费入口官网,要领教程(有挂技巧)-哔哩哔哩1、下载...
专业透视!pokermaste... 专业透视!pokermaster修改器!原来一直都是有辅助攻略(有挂分享)-哔哩哔哩1、pokerm...
总结透视!wepoker透视最... 总结透视!wepoker透视最简单三个步骤,wepoker私局辅助,窍门教程(有挂辅助)-哔哩哔哩;...
了解透视!红龙poker辅助器... 了解透视!红龙poker辅助器免费观看!竟然真的是有辅助脚本(证实有挂)-哔哩哔哩1)红龙poker...
揭幕透视!hhpoker有作必... 揭幕透视!hhpoker有作必弊的吗,wejoker手机版透视脚本,总结教程(了解有挂)-哔哩哔哩1...
总结透视!hardrock透视... 总结透视!hardrock透视工具!本来存在有辅助神器(有挂助手)-哔哩哔哩1、下载好hardroc...