13

Spring Bean をセットアップしたいと思います (インターフェイスまたは Bean クラスを介して)。ルートを「開始」するために呼び出すことができます。

この単純な例では、コードから sayHello("world") を呼び出すときに、sayHello メソッドの戻り値をファイルに書き込むエンドポイントにルーティングしたいと考えています。

これが可能かどうか、またはこれを行う方法を知っている人はいますか? CXF を介して同じインターフェイスを公開し、これを機能させることができることはわかっていますが、本当にメソッドを呼び出したいだけで、jms メッセージを送信したり、Web サービスを呼び出したりする手間を省きたいのです。

public interface Hello{
   public String sayHello(String value);
}

from("bean:helloBean").to("file:/data/outbox?fileName=hello.txt");
4

5 に答える 5

13

ProducerTemplateを使用できます。

import org.apache.camel.Produce;
import org.apache.camel.ProducerTemplate;
import org.springframework.stereotype.Component;

import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;

@Component
public class HelloImpl implements Hello {

    @Produce(uri = "direct:start")
    private ProducerTemplate template;

    @Override
    public Object sayHello(String value) throws ExecutionException, InterruptedException {
        Future future = template.asyncSendBody(template.getDefaultEndpoint(), value);
        return future.get();
    }
}

ラクダのルートは次のようになります。

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:camel="http://camel.apache.org/schema/spring"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="
         http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
         http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd
         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

    <context:component-scan base-package="com.mycompany.camel"/>

    <camelContext xmlns="http://camel.apache.org/schema/spring">
        <route>
            <from uri="direct:start"/>
            <to uri="log:com.mycompany.camel?level=DEBUG"/>
        </route>
    </camelContext>

</beans>
于 2013-02-28T10:09:14.987 に答える
10

はい、Camel でプロキシ/リモーティングを使用してこれを行うことができます。

その後、sayHello(value) を呼び出すと、値が選択されたルートにルーティングされます。そして、ルートからの応答は、sayHello メソッドから返されます。

これらのリンクを参照してください
- http://camel.apache.org/spring-remoting.html
- http://camel.apache.org/hiding-middleware.html
- http://camel.apache.org/using-camelproxy. html

Camel in Action book の第 14 章では、これについてさらに詳しく説明しています: http://www.manning.com/ibsen

于 2010-07-30T07:13:43.257 に答える
1

クラウスの答えを調べる必要がありますが、すばやく汚い UI のために、別のアプローチを採用しました。

私は Spring MVC 3.1.X を使用していて、アプリケーション内のさまざまなアイテムの管理コンソールを持っています。ルートとそのステータスを表示するコントローラーを作成し、必要に応じてルートを開始および停止するためのリンクを提供しました。コードの一部を次に示します。

@Controller
public class CamelController {
    private static final Log LOG = LogFactory.getLog(CamelController.class);

    @Autowired
    @Qualifier("myCamelContextID")
    private CamelContext camelContext;

    @RequestMapping(value = "/dashboard", method = RequestMethod.GET)
    public String dashboard(Model model) {
        if (LOG.isDebugEnabled()) {
            LOG.debug("camel context is suspended : " + camelContext.isSuspended());
        }

        List<Route> routes = camelContext.getRoutes();
        List<RouteStatus> routeStatuses = new ArrayList<RouteStatus>();
        for (Route r : routes) {
            RouteStatus rs = new RouteStatus();
            rs.setId(r.getId());
            rs.setServiceStatus(camelContext.getRouteStatus(r.getId()));
            routeStatuses.add(rs);
        }

        model.addAttribute("routeStatuses", routeStatuses);

        return "dashboard";
    }

    @RequestMapping(value = "/dashboard/{routeId}/start", method = RequestMethod.GET)
    public String startRoute(@PathVariable String routeId) {
        try {
            camelContext.startRoute(routeId);
            if (LOG.isDebugEnabled()) {
                LOG.debug("camel context is starting route [" + routeId + "]");
            }
        } catch (Exception e) {
            LOG.error("failed to start camel context [" + camelContext + "]");
        }

        return "redirect:/dashboard";
     }

    @RequestMapping(value = "/dashboard/{routeId}/stop", method = RequestMethod.GET)
    public String stopRoute(@PathVariable String routeId) {
        try {
            camelContext.stopRoute(routeId);
            if (LOG.isDebugEnabled()) {
                LOG.debug("camel context is stopping route [" + routeId + "]");
            }
        } catch (Exception e) {
            LOG.error("failed to stop camel context [" + camelContext + "]");
        }
        return "redirect:/dashboard";
        }
    }
}

それに合わせて作成した小さな POJO があります。

public class RouteStatus {
    private String id;
    private ServiceStatus serviceStatus;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public ServiceStatus getServiceStatus() {
        return serviceStatus;
    }

    public void setServiceStatus(ServiceStatus serviceStatus) {
        this.serviceStatus = serviceStatus;
    }
}
于 2013-01-07T21:14:36.160 に答える
0

私のルートはCamelConfigurationを使用した春のコンポーネントだったので。キャメルルートでインターフェースを使用するために、次のことを行いました。

@Component
public class SomeRoute extends RouteBuilder {

    @Autowired
    private ApplicationContext applicationContext;

    @Override
    public void configure() throws Exception {
        from("direct:someroute")
        .bean(applicationContext.getBean(SomeInterface.class).getClass(), "someAbstractMethod")
        .to("direct:otherroute");
    }
}

同じインターフェースまたは抽象クラスを使用する複数の Bean がある場合、おそらく.getClass()Bean で使用する前にいくつかのロジックを実行する必要があります。

于 2016-10-16T07:24:55.670 に答える