gpt4 book ai didi

java - Camel : Route from direct to processor

转载 作者:行者123 更新时间:2023-11-30 08:18:46 25 4
gpt4 key购买 nike

我有一个像这样的 Spring DSL 路由:

<bean id="sendMsgProc" class="com.tc.infrastructure.utils.jms.SendMessageProcessor"/>   

<camel:camelContext id="folder-jms" xmlns="http://camel.apache.org/schema/spring" autoStartup="false">
<camel:propertyPlaceholder id="jmsProps" location="classpath:jms-jndi.properties"/>
<route id="folder-jms-route" autoStartup="true">
<!-- <from uri="{{jms.output.folder}}"/> -->
<from uri="direct:start"/>
<!-- <to uri="bean:camelMsgBean"/> -->
<camel:process ref="sendMsgProc"/>
<to uri="{{jms.in.send}}"/>
</route>
</camel:camelContext>

我的主类启动上下文如下:

SpringCamelContext conetx = (SpringCamelContext)camel.initContextCamel("camel-context.xml", "folder-jms");
Exchange ex = new DefaultExchange(conetx);
ex.getIn().setBody(executionTasks.entrySet().iterator().next().getValue(), CamelMessage.class);
conetx.start();
conetx.startRoute("folder-jms-route");

Thread.sleep(10000);
conetx.stopRoute("folder-jms-route");
conetx.stop();

我有一个处理器来获取我的对象形式交换,例如:

public class SendMessageProcessor implements Processor {


//This processor exist for set headers into sending message
public void process(Exchange exchange) throws Exception
{
System.out.println("adasdasd");
CamelMessage message = (CamelMessage)exchange.getIn().getBody(CamelMessage.class);
System.out.println("Message with correlationId get for exchange " + message.getMsgCorrelationId());
System.out.println("Body" + message.getBody());
}
}

我确实设置了在 Camel 中交换 Map 中的对象,例如:

public class CamelMessage extends Message {


private Map<String, Object> headersMap;
private StringBuffer body;
private String msgCorrelationId;

public CamelMessage(File msgPath, String msgCorrelationId)
{
super.setMsgPath(msgPath);
this.msgCorrelationId = msgCorrelationId;
}

public CamelMessage(String correlationID, Map<String, Object> headers, String body)
{
setMsgCorrelationId(correlationID);
setHeadersMap(headers);
setBody(body);
}

public Map<String, Object> getHeadersMap() {
return headersMap;
}
protected void setHeadersMap(Map<String, Object> headersMap) {

if(headersMap == null)
headersMap = new HashMap<String, Object>();

this.headersMap = headersMap;
}


public String getBody() {
return body.toString();
}
protected void setBody(String body) {
if(this.body == null)
this.body = new StringBuffer();

this.body.append(body);
}



public String getMsgCorrelationId() {
return msgCorrelationId;
}
private void setMsgCorrelationId(String msgCorrelationId) {
this.msgCorrelationId = msgCorrelationId;
}
}

我不明白为什么我的 Camel 处理器不工作(不自动触发)。我希望得到我设置的对象,以交换 Camel ,所有字段都已填满。

请帮忙。

最佳答案

我会在您的 conetx.startRoute("folder-jms-route"); 之后添加

ProducerTemplate pt = conetx.createProducerTemplate();
pt.send("direct:start", ex);

--看http://camel.apache.org/maven/current/camel-core/apidocs/org/apache/camel/impl/DefaultProducerTemplate.html了解更多信息

send(String endpointUri, Exchange exchange)
Sends the exchange to the given endpoint

Notice: that if the processing of the exchange failed with an Exception it is not thrown from this method, but you can access it from the returned exchange using Exchange.getException().

关于java - Camel : Route from direct to processor,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27244323/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com