37.3. 소비자 템플릿 사용

37.3.1. 개요

소비자 템플릿은 들어오는 메시지를 수신하기 위해 소비자 엔드포인트를 폴링하는 방법을 제공합니다. 교환 개체 또는 메시지 본문의 형태로 수신되는 메시지를 수신하도록 선택할 수 있습니다(메시지 본문이 내장형 형식 변환기를 사용하여 특정 형식으로 캐스팅될 수 있음).

37.3.2. 폴링 교환의 예

소비자 템플릿을 사용하여 다음과 같은 폴링 방법 중 하나를 사용하여 소비자 끝점을 폴링할 수 있습니다. 즉, receive() 를 차단하거나 시간 초과를 사용하여 receive()수신하거나, 즉시 반환하는NoWait() 를 받을 수 있습니다. 소비자 엔드포인트가 서비스를 나타내기 때문에 교환을 위해 폴링을 시도하기 전에 start() 를 호출하여 서비스 스레드를 시작하는 것도 중요합니다.

다음 예제에서는 blocking receive() 메서드를 사용하여 seda:foo 소비자 끝점의 교환을 폴링하는 방법을 보여줍니다.

import org.apache.camel.ProducerTemplate;
import org.apache.camel.ConsumerTemplate;
import org.apache.camel.Exchange;
...
ProducerTemplate template = context.createProducerTemplate();
ConsumerTemplate consumer = context.createConsumerTemplate();

// Start the consumer service
consumer.start();
...
template.sendBody("seda:foo", "Hello");
Exchange out = consumer.receive("seda:foo");
...
// Stop the consumer service
consumer.stop();

소비자 템플릿 인스턴스인 consumerCamelContext.createConsumerTemplate() 메서드를 사용하여 인스턴스화되고 소비자 서비스 스레드는 ConsumerTemplate.start() 를 호출하여 시작됩니다.

37.3.3. 폴링 메시지 본문의 예

다음 방법 중 하나를 사용하여 수신 메시지 본문에 소비자 끝점을 폴링할 수도 있습니다. 즉, receiveBody() 를 시간 초과로 수신하거나, 즉시 반환하는 receiveBodyNoWait() 입니다. 이전 예에서와 마찬가지로 교환을 위해 폴링을 시도하기 전에 start() 를 호출하여 서비스 스레드를 시작하는 것도 중요합니다.

다음 예제에서는 차단 receiveBody() 메서드를 사용하여 seda:foo 소비자 끝점에서 들어오는 메시지 본문을 폴링하는 방법을 보여줍니다.

import org.apache.camel.ProducerTemplate;
import org.apache.camel.ConsumerTemplate;
...
ProducerTemplate template = context.createProducerTemplate();
ConsumerTemplate consumer = context.createConsumerTemplate();

// Start the consumer service
consumer.start();
...
template.sendBody("seda:foo", "Hello");
Object body = consumer.receiveBody("seda:foo");
...
// Stop the consumer service
consumer.stop();

37.3.4. 교환을 폴링하는 방법

소비자 끝점에서 교환을 폴링하는 세 가지 기본 방법이 있습니다. 시간 초과 블록이 없는 receive(); 지정된 시간 초과 블록이 있는 receive(); 밀리초 동안 시간 초과 블록이 있는 receive(); 수신NoWait() 는 차단되지 않습니다. 소비자 끝점을 끝점 URI 또는 끝점 인스턴스로 지정할 수 있습니다.

Exchange receive(String endpointUri);
Exchange receive(String endpointUri, long timeout);
Exchange receiveNoWait(String endpointUri);

Exchange receive(Endpoint endpoint);
Exchange receive(Endpoint endpoint, long timeout);
Exchange receiveNoWait(Endpoint endpoint);

37.3.5. 메시지 본문을 폴링하는 방법

소비자 끝점에서 메시지 본문을 폴링하는 세 가지 기본 방법이 있습니다. 시간 초과 블록이 없는 receiveBody(); 지정된 기간 동안 시간 초과 블록이 있는 receiveBody(), receiveBodyNoWait() 는 차단되지 않습니다. 소비자 끝점을 끝점 URI 또는 끝점 인스턴스로 지정할 수 있습니다. 또한 이러한 메서드의 템플릿 양식을 호출하여 반환된 본문을 기본 제공 형식 변환기를 사용하여 특정 유형 T 로 변환할 수 있습니다.

Object receiveBody(String endpointUri);
Object receiveBody(String endpointUri, long timeout);
Object receiveBodyNoWait(String endpointUri);

Object receiveBody(Endpoint endpoint);
Object receiveBody(Endpoint endpoint, long timeout);
Object receiveBodyNoWait(Endpoint endpoint);

<T> T receiveBody(String endpointUri, Class<T> type);
<T> T receiveBody(String endpointUri, long timeout, Class<T> type);
<T> T receiveBodyNoWait(String endpointUri, Class<T> type);

<T> T receiveBody(Endpoint endpoint, Class<T> type);
<T> T receiveBody(Endpoint endpoint, long timeout, Class<T> type);
<T> T receiveBodyNoWait(Endpoint endpoint, Class<T> type);