[英]Rollback message in apache camel using spring framework
我的應用程序的工作流程:我已經設置了activemq並編寫了spring框架來監聽activemq的隊列。 只要隊列中有消息,偵聽器就會收到消息,然后將消息出列並執行業務邏輯。
現在我在測試用例中,如果我的業務邏輯中存在任何運行時錯誤,則該消息應該回滾到隊列。 這樣消費者可以再次使用消息並再次執行我的業務邏輯。
我怎樣才能用春駱駝來實現這個目標?
我為ActiveMqConsumer編寫的代碼
public class ActiveMqConsumer {
public static void main(String[] args){
try {
PropertyConfigurator.configure("C:/Users/awsdemo/src/main/resources/log4j.properties");
ApplicationContext springcontext = new FileSystemXmlApplicationContext("C:/Users/awsdemo/src/main/resources/activecamel.xml");
CamelContext context = springcontext.getBean("activeContext", CamelContext.class);
//context.addComponent("activemq", activeMQComponent("tcp://localhost:61616?broker.persistent=false"));
context.start();
//Thread.sleep(1000);
//context.stop();
} catch ( Exception e ) {
System.out.println(e);
}
}
}
ActiveMQRouterBuilder的代碼
public class ActiveMQRouterBuilder extends RouteBuilder {
@Override
public void configure() throws Exception {
String activeMqURI = "activemq:queue:ThermalMap";
System.out.print(activeMqURI);
from( activeMqURI).to("bean:activemqProcessor?method=processMessage");
}
}
ActiveMQProcessor的代碼
public class ActiveMQProcessor{
public void processMessage(Exchange exchange) throws Exception{
System.out.println("\ninside processMessage :Consumer1");
//System.out.println(exchange.getIn().getBody());
Object object = exchange.getIn().getBody();
FunctionNames functionNamesObject=new FunctionNames();
//Call Intergration function to execute .exe file
try {
/* my business logic*/
} catch (IOException e) {
/* message should rollback here to activemq*/
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InterruptedException e) {
/* or message should rollback here to activemq*/
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("ActiveMQProcessor: finished");
}
}
以上三個文件結合起來作為消費者。 這三個文件在activecamel.xml
文件中配置。 activecamel.xml
包含以下代碼
<camelContext id="activeContext" xmlns="http://camel.apache.org/schema/spring">
<routeBuilder ref="activeMQRouter" />
</camelContext>
<bean id="activemq" class="org.apache.activemq.camel.component.ActiveMQComponent">
<property name="brokerURL" value="tcp://localhost:61616?jms.prefetchPolicy.queuePrefetch=1" />
</bean>
<bean id="activeMQRouter" class="main.java.com.aranin.activemq.ActiveMQRouterBuilder"/>
<bean id="activemqProcessor" class="main.java.com.aranin.activemq.ActiveMQProcessor"/>
在ActiveMQProcessor中,我編寫了業務邏輯,如果有任何錯誤,它會拋出錯誤來捕獲塊。 在catch塊中,我應該編寫代碼來回滾消息。 應該回滾消息的代碼是什么?
如果在路由中使用transacted(),則應該從ActiveMQProcessor中拋出異常,Camel將自動回滾TX。
from( activeMqURI)
.transacted()
.to("bean:activemqProcessor?method=processMessage");
您還需要將ActiveMQ配置為事務處理
<bean id="activemq" class="org.apache.activemq.camel.component.ActiveMQComponent">
<property name="brokerURL" value="tcp://localhost:61616?jms.prefetchPolicy.queuePrefetch=1" />
<property name="transacted" value="true"/>
</bean>
並設置JMS事務管理器。 有關詳細信息,請訪問: http : //camel.apache.org/transactional-client.html
雖然后者,如果你有transacted = true,Camel應該默認設置一個。 但最好在xml文件中定義事務管理器,並參考activemq配置中的事務管理器。 以上鏈接的所有詳細信息。
如果你有一本Camel in Action書的副本,那么請閱讀第9章。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.