[CAMEL] Transformation

.·2021년 12월 11일
0

CAMEL

목록 보기
3/5

transform 메소드를 이용한 transformation

transform() 메소드를 사용해 바디 메시지 변환

@Component
public class TransformationUsingTransformMethod extends RouteBuilder {

	@Override
	public void configure() throws Exception {
		// 1) starting end point 정의
		from("timer:nyj-timer") // body : null
		// 2-1) body 메시지 변환
		// : transform()
		.transform()
		// 2-2) 변경해줄 상수 메시지 정의
		// : constant()
		.constant("현재 시각은 " + LocalDateTime.now()) // body : 현재 시각은 2021-11-14T18:23:00.586301200
		// 3) ending end point 정의
		.to("log:nyj-timer");
	}
}

로그보기

Routes startup summary (total:1 started:1)
// body메시지가 설정해준 상수값으로 변경됨
// 상수이기 때문에 모든 시간이 처음 실행된 상수값으로 고정됨
2m2021-11-14 18:23:01.727
Exchange[ExchangePattern: InOnly, BodyType: String, Body: 현재 시각은 2021-11-14T18:23:00.586301200]]
2m2021-11-14 18:23:02.725
Exchange[ExchangePattern: InOnly, BodyType: String, Body: 현재 시각은 2021-11-14T18:23:00.586301200]]
...

channel(endpoint)간의 연결 사이에서 메시지를 변환
메시지를 가져와서 특정값으로 변경하고 로그로 출력

bean 메소드를 이용한 transformation

스프링 빈을 사용해 바디 메시지를 변환

@Component
public class TransformationUsingBeanMethod extends RouteBuilder{
	
	// Bean 주입받기
	@Autowired
	private GetCurrentTimeBean getCurrentTimeBean;
	
	@Override
	public void configure() throws Exception {

		// 1) starting end point 정의
		from("timer:nyj-timer") // null
		// 2) 빈을 사용한 메시지 가져오기
		// .bean(사용할 빈)
		// 2-1) 클래스 전체를 가져오기
		// 해당 클래스에 메소드가 하나일 경우에 사용
		// bean({클래스객체명})
		.bean(getCurrentTimeBean)  // 현재 시각은 2021-12-11T15:16:17.313051100
		// 2-2) 해당 클래스의 특정 메소드 가져오기
		// 해당 클래스에 두개 이상의 메소드가 있을 경우 사용
		// .bean({클래스객체명}, "{가져올 메소드명}")
		.bean(getCurrentTimeBean,"getCurrentTime")  // 현재 시각은 2021-12-11T15:16:17.313051100
		// 3) ending end point 정의
		.to("log:nyj-timer");
	}

	// 사용할 Bean
	@Component
	class GetCurrentTimeBean {
		public String getCurrentTime() {
			return "현재 시각은 " + LocalDateTime.now();
		}
	}
}

로그보기

2m2021-12-11 15:16:17.313
Exchange[ExchangePattern: InOnly, BodyType: String, Body: 현재 시각은 2021-12-11T15:16:17.313051100]
2m2021-12-11 15:16:18.314
Exchange[ExchangePattern: InOnly, BodyType: String, Body: 현재 시각은 2021-12-11T15:16:18.313492100]
...

0개의 댓글

관련 채용 정보