Apache Camel 代理不工作

2022-01-19 00:00:00 java apache-camel

问题也可能与我对这个概念的理解有关.
ActionClass 正在调用代理 bean,即 AccountingInterface.代理 bean 接口是用 AccountingUtil 类实现的.所以我期望 AccountingUtil 返回的 xml 将通过 seda:accountingQueue 传递,然后在控制台上流出.
ApplicationContext

Issue might be related to my understanding of the concept too.
ActionClass is invoking proxy bean which is AccountingInterface. Proxy bean interface is implemented with AccountingUtil class. So I am expecting xml returned by AccountingUtil to be passed through seda:accountingQueue and then streamed out on console.
ApplicationContext

    <bean id="accountingInterface" class="org.apache.camel.spring.remoting.CamelProxyFactoryBean">
      <property name="serviceUrl" value="seda:accountingQueue"/>
      <property name="serviceInterface" value="acord.transaction.util.AccountingInterface"/>
    </bean>
    <route>
        <from uri="seda:accountingQueue"/>
        <setHeader headerName="nowInMillis">
           <groovy>new Date().getTime()</groovy>
         </setHeader>
         <to uri="stream:out"/>
    </route>

会计接口

public interface AccountingInterface 
{
    void send(String xml);
    String accountingUpdate(EDITDate accountingProcessDate);
}

AccountingUtil

public class AccountingUtil implements AccountingInterface
{
  public String accountingUpdate(EDITDate accountingProcessDate)
     {
       //doSomething
      return xml;
    }

动作类

AccountingInterface accountingInterface = (AccountingInterface) AppContext.getBean("accountingInterface");
accountingInterface.accountingUpdate(accountingProcessDate);

但我遇到了异常:

No body available of type: java.lang.String but has value: BeanInvocation public abstract java.lang.String acord.transaction.util.AccountingInterface.accountingUpdate(edit.common.EDITDate) with [2012/11/08]] of type: org.apache.camel.component.bean.BeanInvocation on: Message: BeanInvocation public abstract java.lang.String acord.transaction.util.AccountingInterface.accountingUpdate(edit.common.EDITDate) with [2012/11/08]]. Caused by: No type converter available to convert from type: org.apache.camel.component.bean.BeanInvocation to the required type: java.lang.String with value BeanInvocation public abstract java.lang.String acord.transaction.util.AccountingInterface.accountingUpdate(edit.common.EDITDate) with [2012/11/08]]. Exchange[Message: BeanInvocation public abstract java.lang.String acord.transaction.util.AccountingInterface.accountingUpdate(edit.common.EDITDate) with [2012/11/08]]]. Caused by: [org.apache.camel.NoTypeConversionAvailableException - No type converter available to convert from type: org.apache.camel.component.bean.BeanInvocation to the required type: java.lang.String with value BeanInvocation public abstract java.lang.String acord.transaction.util.AccountingInterface.accountingUpdate(edit.common.EDITDate) with [2012/11/08]]]
    at org.apache.camel.impl.MessageSupport.getMandatoryBody(MessageSupport.java:101)

还有一个问题我可以为单个 proxyBean(interface) 设置多个 serviceURL 吗?
我想要不同的方法来调用不同的serviceURL,但是是单个接口的一部分.

One more issue Can I have multiple serviceURL for single proxyBean(interface)?
I want different method to call different serviceURL but part of a single interface.

推荐答案

更新:啊,刚刚明白你的问题.一开始就有点第一了.

Update: Ah, just understood your question. Was a bit first in the first place.

在进行代理时,Camel 将调用(到哪个方法/接口和哪些参数)转换为一个 BeanInvocation 对象.然后通过选择的 Camel 端点(在您的情况下为 seda)进行处理.但是您正在尝试将其打印到标准输出,而不是调用 AccountingInterface 的 bean 实例(例如 AccountingUpdateACORDUtil 在您的情况下).

When doing Proxying Camel translates the call (to which method/interface and what arguments) into a BeanInvocation object. That is then passed over the Camel endpoint of choice (seda in your case) to be processed. But you are trying to print it to std out rather than invoke a bean instance of AccountingInterface (such as AccountingUpdateACORDUtil in your case).

不,不同的方法将调用相同的 URL,但在 BeanInvocation 中使用不同的方法.这当然可以通过在 Camel 路由中路由您的请求来实现,例如首先将其发送到一个简单的 direct:routeAccoutingRequest 然后查看调用的方法以确定它应该选择哪个端点构造.

And no, different methods will call the same URL but with .. well, different methods in the BeanInvocation. This can of course be achieved by routing your request in a Camel route such as first sending it to a simple direct:routeAccoutingRequest then look at the invoked method to figure out which endpoint it should go to in a choice construct.

您可以使用带有 ProducerTemplates 的普通对象构建自己的逻辑,以实现发送字符串、调用不同方法等任务.代理用于代理 bean 调用.

You can build your own logic using a plain object with ProducerTemplates to achive things such as sending Strings, invoking different methods etc. The proxy is for proxying bean invocations.

相关文章