Giter VIP home page Giter VIP logo

producer-template-sample's Introduction

Spring-Boot Camel ProducerTemplate inside Processor

ProducerTemplateは、Camelで用意されたインタフェースです。このインタフェースを利用することで、Javaコード(Processor, Bean)の中でCamelが用意された豊富なコンポーネントを簡単にインスタンス化し、外部システムと送受信できるようになります。

このサンプルでは、Processorの中で、HTTPコンポーネントを使ったリクエストレスポンスの実装方法を紹介します。

実装方法

1. camel-conotext.xml

camelContext内で<template> (ProducerTemplateのこと)を初期化します。
また、MyProcessorはこのtemplate、とendpointUriで初期化します。

<bean id="myprocessor" class="org.mycompany.MyProcessor">
	<property name="producer" ref="myTemplate" />
	<property name="endpointUri" value="http://inet-ip.info/ip"/>
</bean>
<camelContext id="camel" ...">
	<template id="myTemplate" />
	...
</camelContext>

2. MyProcessor.java

@Override
public void process(Exchange exchange) throws Exception {
	
	// camel-contextにて初期化されたProducerTempalteを使ってリクエストレスポンスを実行します。	
	String respBody = producer.requestBody(endpointUri,"test message from myprocessor", String.class);
	LOG.info("respBody={}", respBody);
	
	// ProducerTemplateから得られたレスポンスを現在処理中のExchangeにセットします。
	exchange.getIn().setBody(respBody);
}

3. camel-conotext.xml

camelContext内のCamel Routeは以下に定義します。

<route>
	...
	<log id="log1" message="body before myprocessor >>> ${body}" />
	<process ref="myprocessor" />
	<log id="log2" message="body after myprocessor >>> ${body}" />
</route>

実行方法

mvn spring-boot:run

このサンプルを実行すると、10秒間隔で、MyProcessorの中から指定のendpointUri(Camelコンポーネントが使われる)にアクセスし、endpointのレスポンスをログに出力します。

[Camel (camel) thread #1 - timer://foo] INFO  simple-route - body before myprocessor >>> Hello World from camel-context.xml
[Camel (camel) thread #1 - timer://foo] INFO  MyProcessor - respBody=xx.xx.xx.xx
[Camel (camel) thread #1 - timer://foo] INFO  simple-route - body after myprocessor >>> xx.xx.xx.xx
  • 1行目のログは、simple-routeがMyProcessor実行前Bodyの出力
  • 2行目は、MyProcessorの中でendpointのレスポンスをログに出力
  • 3行目は、simple-routeがMyProcessor実行後Bodyの出力
  • 1 - 3は、同じスレッドで処理されていること

producer-template-sample's People

Contributors

jian-feng avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.