검색

37.3. 소비자 템플릿 사용

download PDF

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);

M

Red Hat logoGithubRedditYoutubeTwitter

자세한 정보

평가판, 구매 및 판매

커뮤니티

Red Hat 문서 정보

Red Hat을 사용하는 고객은 신뢰할 수 있는 콘텐츠가 포함된 제품과 서비스를 통해 혁신하고 목표를 달성할 수 있습니다.

보다 포괄적 수용을 위한 오픈 소스 용어 교체

Red Hat은 코드, 문서, 웹 속성에서 문제가 있는 언어를 교체하기 위해 최선을 다하고 있습니다. 자세한 내용은 다음을 참조하세요.Red Hat 블로그.

Red Hat 소개

Red Hat은 기업이 핵심 데이터 센터에서 네트워크 에지에 이르기까지 플랫폼과 환경 전반에서 더 쉽게 작업할 수 있도록 강화된 솔루션을 제공합니다.

© 2024 Red Hat, Inc.