MULE事務(wù)配置

在mule的事務(wù)可能為jdbc事務(wù),jms事務(wù),xa事務(wù)等,多種事務(wù).這里講解事務(wù)的幾個(gè)動(dòng)作:

相關(guān)的文檔:https://www.mulesoft.org/documentation-3.2/display/MULE2USER/Transaction+Management

transaction可以使用的動(dòng)作為:

NONE - Never participate in a transaction.(不采用事務(wù))

ALWAYS_BEGIN - Always start a new transaction when receiving a message. If a previous transaction exists, it commits that transaction.(接收消息時(shí)候必須有一個(gè)新的事務(wù),如果事務(wù)已經(jīng)存在,則先commit,在創(chuàng)建新事務(wù).)

BEGIN_OR_JOIN - If a transaction is already in progress when an event is received, join the transaction, otherwise start a new transaction.(采用事務(wù),如果事務(wù)存在,直接使用,如果不存在,創(chuàng)建新事務(wù))

ALWAYS_JOIN - Always expects a transaction to be in progress when an event is received. If there is no transaction, an exception is thrown.(在接收消息時(shí)候總是期望有一個(gè)事務(wù),如果事務(wù)不存在,拋出異常信息)

JOIN_IF_POSSIBLE- Will join the current transaction if one is available. Otherwise, no transaction is created (如果事務(wù)存在則使用事務(wù),如果事務(wù)不存在,則不創(chuàng)建事務(wù))

舉例: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"/>  
       
     <!-- 創(chuàng)建鏈接 -->  
    <jms:activemq-connector name="jmsConnector" maxRedelivery="1">  
        <dispatcher-threading-profile doThreading="false"/>  
    </jms:activemq-connector>  
      
    <!-- 定義壓縮轉(zhuǎn)換器 -->  
    <gzip-compress-transformer name="Compress"/>  
    <gzip-uncompress-transformer name="Uncompress"/>  
      
    
    <!-- 定義輸入隊(duì)列 -->  
    <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>

java代碼
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();  
        }  
          
    }  
} 
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容