这是用户在 2024-3-28 22:19 为 https://camel.apache.org/manual/pojo-producing.html 保存的双语快照页面,由 沉浸式翻译 提供双语支持。了解如何保存?

POJO producing POJO 生成

There are two different ways to send messages to any Camel Endpoint from a POJO:
从 POJO 向任何 Camel 端点发送消息有两种不同的方式:

  • Using @Produce or @EndpointInject
    使用 @Produce@EndpointInject

  • Or to hide using an interface
    或者使用一个界面来隐藏

The following steps use the ActiveMQ component which is not yet supported on Camel 4.
以下步骤使用的 ActiveMQ 组件在 Camel 4 上尚不支持。

Using @Produce 使用@PRODUCE

To allow sending of messages from POJOs you can use the @Produce annotation. This will inject a org.apache.camel.ProducerTemplate so that the bean can send messages.
为了允许从 POJO 发送消息,您可以使用 @Produce 注解。这将注入一个 org.apache.camel.ProducerTemplate ,以便该 bean 可以发送消息。

The @Produce POJO annotations are not part of any Camel routes, and you cannot use errorHandler or onException with that.
@Produce POJO 注解不是任何 Camel 路由的一部分,您不能使用 errorHandler 或 onException。

For example to send a message to the foo queue on ActiveMQ:
例如,向 ActiveMQ 上的 foo 队列发送消息

public class Foo {
  @Produce("activemq:foo")
  ProducerTemplate producer;

  public void doSomething() {
    if (whatever) {
      producer.sendBody("<hello>world!</hello>");
    }
  }
}

The downside of this is that your code is now dependent on a Camel API, the ProducerTemplate. The next section describes how to remove this dependency.
这样做的缺点是,您的代码现在依赖于一个名为 Camel API 的东西。下一节将介绍如何消除这种依赖性。

See POJO Consuming for how to use a property on the bean as endpoint configuration, e.g., using the property attribute on @Produce or @EndpointInject.
请参考 POJO 消费者的使用方式,例如,使用 property 属性在 @Produce@EndpointInject 上作为端点配置。

Hiding the Camel APIs From Your Code
隐藏您的代码中的骆驼 API

You can hide Camel APIs from your application code. You can add the @Produce annotation to an injection point (a field or property setter) using some interface you use in your business logic. Example:
您可以在应用程序代码中隐藏 Camel API。您可以在业务逻辑中使用的某个接口的注入点(字段或属性设置器)上添加 @Produce 注释。示例:

public interface MyListener {
    // this method is request/reply (InOut) because the method has a return value
    // to use one way (InOnly) then the method should be a void method
    String sayHello(String name);
}

public class MyBean {
    @Produce("activemq:foo")
    protected MyListener producer;

    public void doSomething() {
        // lets send a message and get a response back
        String response = producer.sayHello("James");
    }
}

Here Camel will automatically inject a smart client side proxy at the @Produce annotation - an instance of the MyListener interface.
在这里,Camel 将自动在 @Produce 注解处注入一个智能客户端代理 - 一个 MyListener 接口的实例。

When we invoke methods on this interface the method call is turned into an object and is sent to the endpoint; in this case the ActiveMQ endpoint to queue foo. Because the sayHello method has a return type (String) then Camel will use Request Reply (InOut) messaging.
当我们在这个接口上调用方法时,方法调用会被转换成一个对象并发送到终端点;在这种情况下,是发送到 ActiveMQ 终端点的队列 foo 。因为 sayHello 方法有一个返回类型( String ),所以 Camel 将使用请求回复(InOut)消息传递。

public interface MyListener {
    void sayHello(String name);
}

If the method is a void method, then Camel will use Event Message (InOnly) messaging.
如果该方法是一个 void 方法,那么 Camel 将使用事件消息(InOnly)传递。