POJO Consuming POJO 消费
To consume a message you use the @Consume
annotation to mark a particular method of a bean as being a consumer method. The value of the annotation defines the Camel Endpoint to consume from.
使用 @Consume
注解来消费消息,将特定的 bean 方法标记为消费者方法。注解的值定义了要从中消费的 Camel Endpoint。
The @Consume POJO annotations are not part of any Camel routes, and you cannot use errorHandler or onException with that. @Consume POJO 注解不是任何 Camel 路由的一部分,您不能使用 errorHandler 或 onException。 |
The following steps use the ActiveMQ component which is not yet supported on Camel 4. |
For example lets invoke the onCheese()
method with the String body of the inbound JMS message from ActiveMQ on the cheese queue; this will use the Type Converter to convert the JMS ObjectMessage or BytesMessage to a String - or just use a TextMessage from JMS
例如,让我们使用来自 ActiveMQ 上的 cheese 队列的入站 JMS 消息的 String 正文来调用 onCheese()
方法;这将使用类型转换器将 JMS ObjectMessage 或 BytesMessage 转换为 String - 或者只是使用来自 JMS 的 TextMessage
public class Foo {
@Consume("activemq:cheese")
public void onCheese(String name) {
// do something here
}
}
The Bean Binding is then used to convert the inbound Message to the parameter list used to invoke the method .
Bean 绑定然后用于将传入的消息转换为用于调用方法的参数列表。
This basically creates a route that looks kinda like this:
这基本上创建了一个看起来有点像这样的路由:
from(uri).bean(theBean, "methodName");
Using a property to define the endpoint
使用属性来定义终点
The following annotations @Consume
, @Produce
, @EndpointInject
, now offers a property
attribute you can use to define the endpoint as a property on the bean. Then Camel will use the getter method to access the property.
以下注释 @Consume
, @Produce
, @EndpointInject
现在提供了一个 property
属性,您可以使用它将端点定义为 bean 的属性。然后 Camel 将使用 getter 方法访问该属性。
For example: 例如:
public class MyService {
private String serviceEndpoint;
public void setServiceEndpoint(String uri) {
this.serviceEndpoint = uri;
}
public String getServiceEndpoint() {
return serviceEndpoint;
}
@Consume(property = "serviceEndpoint")
public void onService(String input) {
// do something
}
}
The bean MyService
has a property named serviceEndpoint
which has getter/setter for the property. Now we want to use the bean for POJO Consuming, and hence why we use @Consume
in the onService
method. Notice how we use the property = "serviceEndpoint
to configure the property that has the endpoint url.
bean MyService
有一个名为 serviceEndpoint
的属性,该属性具有用于获取/设置属性的 getter/setter。现在我们想要将该 bean 用于 POJO 消费,因此我们在 onService
方法中使用 @Consume
。请注意我们如何使用 property = "serviceEndpoint
来配置具有端点 URL 的属性。
If you define the bean in Spring XML, then you can configure the property as follows:
如果你在 Spring XML 中定义了 bean,那么你可以按照以下方式配置属性:
<bean id="myService" class="com.foo.MyService">
<property name="serviceEndpoint" value="activemq:queue:foo"/>
</bean>
This allows you to configure the bean without with any dependency injection style.
这样可以让您在不使用任何依赖注入风格的情况下配置 bean。
Advanced use with property naming convention
高级用法与属性命名约定
Camel offers a naming convention which allows you to not have to explicit name the property. Camel uses this algorithm to find the getter method. The method must be a getXXX
method.
Camel 提供了一种命名约定,允许您不必显式命名属性。Camel 使用此算法来查找 getter 方法。该方法必须是一个 getXXX
方法。
-
Use the property name if explicit given
如果明确给出,请使用属性名称 -
If no property name was configured, then use the method name
如果未配置属性名称,则使用方法名称 -
Try to get the property with nameEndpoint (eg with Endpoint as postfix)
尝试获取名称为 nameEndpoint 的属性(例如以 Endpoint 作为后缀) -
Try to get the property with the name as is (eg no postfix or postfix)
尝试以原样获取具有相同名称的属性(例如没有前缀或后缀) -
If the property name starts with on then omit that, and try step 3 and 4 again.
如果属性名称以 on 开头,则省略该属性,然后再次尝试步骤 3 和 4。
So in the example above, we could have defined the @Consume
annotation as:
所以在上面的例子中,我们可以将 @Consume
注释定义为:
@Consume(property = "service")
public void onService(String input) {
Now the property is named "service" which then would match step 3 from the algorithm, and have Camel invoke the getServiceEndpoint
method.
现在该属性被命名为"service",这将与算法的第 3 步匹配,并使 Camel 调用 getServiceEndpoint
方法。
We could also have omitted the property attribute, to make it implicit
我们也可以省略属性属性,使其隐式化
@Consume
public void onService(String input) {
Now Camel matches step 5, and loses the prefix on in the name, and looks for 'service' as the property. And because there is a getServiceEndpoint
method, Camel will use this method.
现在 Camel 匹配到步骤 5,并且去掉了名称中的前缀'in',然后寻找属性'service'。由于存在 getServiceEndpoint
方法,Camel 将使用该方法。