欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Mule ESB 学习笔记(10)mule事务配置

程序员文章站 2022-07-12 18:57:56
...

       在mule的事务可能为jdbc事务,jms事务,xa事务等,多种事务.这里讲解事务的几个动作:

相关的文档:

https://www.mulesoft.org/documentation-3.2/display/MULE2USER/Transaction+Management

transaction可以使用的动作为:

NONE - Never participate in a transaction.(不采用事务)
ALWAYS_BEGIN - Always start a new transaction when receiving a message. If a previous transaction exists, it commits that transaction.(接收消息时候必须有一个新的事务,如果事务已经存在,则先commit,在创建新事务.)
BEGIN_OR_JOIN - If a transaction is already in progress when an event is received, join the transaction, otherwise start a new transaction.(采用事务,如果事务存在,直接使用,如果不存在,创建新事务)
ALWAYS_JOIN - Always expects a transaction to be in progress when an event is received. If there is no transaction, an exception is thrown.(在接收消息时候总是期望有一个事务,如果事务不存在,抛出异常信息)
JOIN_IF_POSSIBLE - Will join the current transaction if one is available. Otherwise, no transaction is created (如果事务存在则使用事务,如果事务不存在,则不创建事务)

 

 

 

举例:

jms-queue-with-transaction.xml

 

 

 

<?xml version="1.0" encoding="UTF-8"?>
<mule xmlns="http://www.mulesoft.org/schema/mule/core"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:jms="http://www.mulesoft.org/schema/mule/jms"
    xmlns:vm="http://www.mulesoft.org/schema/mule/vm"
    xmlns:spring="http://www.springframework.org/schema/beans" 
    xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd
        http://www.mulesoft.org/schema/mule/jms http://www.mulesoft.org/schema/mule/jms/current/mule-jms.xsd
        http://www.mulesoft.org/schema/mule/vm http://www.mulesoft.org/schema/mule/vm/current/mule-vm.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-current.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd">

    <vm:connector name="VMQueue"/>
     
     <!-- 创建链接 -->
    <jms:activemq-connector name="jmsConnector" maxRedelivery="1">
        <dispatcher-threading-profile doThreading="false"/>
    </jms:activemq-connector>
    
    <!-- 定义压缩转换器 -->
    <gzip-compress-transformer name="Compress"/>
    <gzip-uncompress-transformer name="Uncompress"/>
    
  
    <!-- 定义输入队列 -->
    <endpoint name="in" address="jms://queue/in" exchange-pattern="one-way"/>
    
    <!-- see MULE-3342 for what this test is supposed to check -->
    <model name="model">
        <service name="vm-to-jms-queue">
            <inbound>
                <inbound-endpoint address="vm://in" exchange-pattern="one-way"/>
            </inbound>
            <outbound>
                <pass-through-router>
                    <outbound-endpoint ref="in" >
                        <jms:transaction action="ALWAYS_BEGIN" timeout="3000"/>
                    </outbound-endpoint>
                </pass-through-router>
            </outbound>
        </service>
        
        <service name="jms-to-vm">
            <inbound>
                <inbound-endpoint ref="in"/>
            </inbound>
            <outbound>
                <pass-through-router>
                    <outbound-endpoint address="vm://out" exchange-pattern="one-way"/>
                </pass-through-router>
            </outbound>
        </service>
    </model>
</mule>

 

测试代码:

 

 

 

public class MuleJMSMain {
    public static void main(String[] args)  {
        try {
            String configFile = "jms-queue-with-transaction.xml";
            String[] configFileArr = new String[] {configFile };
            MuleContextFactory muleContextFactory = new DefaultMuleContextFactory();
            MuleContext context = muleContextFactory
                    .createMuleContext(new SpringXmlConfigurationBuilder(configFileArr));
            context.start();
            LocalMuleClient client = context.getClient();
            client.send("vm://in", new DefaultMuleMessage("i love china ", context));

            MuleMessage response = client.request("vm://out", 100000);
            System.out.println("payload as string :"+response.getPayloadAsString());
            
            
        } catch (Exception e) {
            e.printStackTrace();
        }
        
    }
}