-
Notifications
You must be signed in to change notification settings - Fork 328
/
BasicCamelToReactorInOutExample.java
157 lines (127 loc) · 4.71 KB
/
BasicCamelToReactorInOutExample.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.camel.example.reactive.streams;
import org.apache.camel.Exchange;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.component.reactive.streams.util.UnwrapStreamProcessor;
import org.reactivestreams.Publisher;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;
import reactor.core.publisher.Flux;
/**
* This example shows how Camel can asynchronously request data to a reactive stream framework
* and continue processing.
*
* The exchange pattern is in-out from Camel to Reactor.
*
* Note: the Camel and reactor components are placed in the same configuration class for the sake of clarity,
* but they can be moved in their own files.
*/
@Configuration
@ConditionalOnProperty("examples.basic.camel-to-reactor-in-out")
public class BasicCamelToReactorInOutExample {
/**
* The reactor streams.
*/
@Component("userBean")
public static class BasicCamelToReactorInOutExampleStreams {
/**
* This method will be called by a Camel route.
*/
public Publisher<UserInfo> getUserInfo(Publisher<Long> userId) {
return Flux.from(userId)
.map(UserInfo::new)
.flatMap(this::retrieveAddress)
.flatMap(this::retrieveName);
}
/**
* This is a sample utility method.
*/
private Publisher<UserInfo> retrieveAddress(UserInfo user) {
// you can do an async database retrieval here
return Flux.just(user.withAddress("Address" + user.getId()));
}
private Publisher<UserInfo> retrieveName(UserInfo user) {
// you can do an async database retrieval here
return Flux.just(user.withName("Name" + user.getId()));
}
}
/**
* The Camel Configuration.
*/
@Component
public static class BasicCamelToReactorInOutExampleRoutes extends RouteBuilder {
@Override
public void configure() throws Exception {
// Generate a Id and retrieve user data from reactor
from("timer:clock?period=9000&delay=1500&includeMetadata=true")
.setBody().header(Exchange.TIMER_COUNTER).convertBodyTo(Long.class) // Sample ID
.bean("userBean", "getUserInfo") // Get the user info from reactor code
.process(new UnwrapStreamProcessor()) // Unwrap the Publisher
.log("BasicCamelToReactorInOut - Got ${body}");
}
}
/**
* Sample bean used in the example.
*/
public static class UserInfo {
private Long id;
private String name;
private String address;
public UserInfo() {
}
public UserInfo(Long id) {
this.id = id;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public UserInfo withName(String name) {
this.name = name;
return this;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public UserInfo withAddress(String address) {
this.address = address;
return this;
}
@Override
public String toString() {
final StringBuilder sb = new StringBuilder("UserInfo{");
sb.append("id=").append(id);
sb.append(", name='").append(name).append('\'');
sb.append(", address='").append(address).append('\'');
sb.append('}');
return sb.toString();
}
}
}