首页 文章

Apache骆驼写信给rabbitmq

提问于
浏览
2

我正在尝试从文件中读取数据并使用apache camel写入rabbitmq队列,但最终会出现错误

Exception in thread "main" org.apache.camel.FailedToCreateRouteException: Failed to create route route1 at: >>> To[rabbitmq://localhost:15672?queue=hello] <<< in route: Route(route1)[[From[file://target/?fileName=doctor.txt&chars... because of Failed to resolve endpoint: rabbitmq://localhost:15672?queue=hello due to: No URI path as the exchangeName for the RabbitMQEndpoint, the URI is rabbitmq://localhost:15672?queue=hello
    at org.apache.camel.model.RouteDefinition.addRoutes(RouteDefinition.java:945)
    at org.apache.camel.model.RouteDefinition.addRoutes(RouteDefinition.java:187)
    at org.apache.camel.impl.DefaultCamelContext.startRoute(DefaultCamelContext.java:794)
    at org.apache.camel.impl.DefaultCamelContext.startRouteDefinitions(DefaultCamelContext.java:2184)
    at org.apache.camel.impl.DefaultCamelContext.doStartCamel(DefaultCamelContext.java:1916)
    at org.apache.camel.impl.DefaultCamelContext.doStart(DefaultCamelContext.java:1777)
    at org.apache.camel.support.ServiceSupport.start(ServiceSupport.java:61)
    at org.apache.camel.impl.DefaultCamelContext.start(DefaultCamelContext.java:1745)
    at test.RMQCamelSender.main(RMQCamelSender.java:38)
Caused by: org.apache.camel.ResolveEndpointFailedException: Failed to resolve endpoint: rabbitmq://localhost:15672?queue=hello due to: No URI path as the exchangeName for the RabbitMQEndpoint, the URI is rabbitmq://localhost:15672?queue=hello
    at org.apache.camel.impl.DefaultCamelContext.getEndpoint(DefaultCamelContext.java:545)
    at org.apache.camel.util.CamelContextHelper.getMandatoryEndpoint(CamelContextHelper.java:71)
    at org.apache.camel.model.RouteDefinition.resolveEndpoint(RouteDefinition.java:202)
    at org.apache.camel.impl.DefaultRouteContext.resolveEndpoint(DefaultRouteContext.java:106)
    at org.apache.camel.impl.DefaultRouteContext.resolveEndpoint(DefaultRouteContext.java:112)
    at org.apache.camel.model.SendDefinition.resolveEndpoint(SendDefinition.java:61)
    at org.apache.camel.model.SendDefinition.createProcessor(SendDefinition.java:55)
    at org.apache.camel.model.ProcessorDefinition.makeProcessor(ProcessorDefinition.java:500)
    at org.apache.camel.model.ProcessorDefinition.addRoutes(ProcessorDefinition.java:213)
    at org.apache.camel.model.RouteDefinition.addRoutes(RouteDefinition.java:942)
    ... 8 more
Caused by: java.lang.IllegalArgumentException: No URI path as the exchangeName for the RabbitMQEndpoint, the URI is rabbitmq://localhost:15672?queue=hello
    at org.apache.camel.component.rabbitmq.RabbitMQComponent.createEndpoint(RabbitMQComponent.java:50)
    at org.apache.camel.component.rabbitmq.RabbitMQComponent.createEndpoint(RabbitMQComponent.java:31)
    at org.apache.camel.impl.DefaultComponent.createEndpoint(DefaultComponent.java:122)
    at org.apache.camel.impl.DefaultCamelContext.getEndpoint(DefaultCamelContext.java:525)
    ... 17 more

以下是我创建驼峰上下文和rabbitmq队列的类的实现 .

RMQCamleSender.java

package test;

import org.apache.camel.CamelContext;
import org.apache.camel.Exchange;
import org.apache.camel.Processor;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.impl.DefaultCamelContext;
import test.Producer;

public class RMQCamelSender {
    public static void main(String[] argv) throws Exception {

        Producer queueProd = new Producer();
        queueProd.setupConnection();
        System.out.println(queueProd.toString());

        CamelContext context = new DefaultCamelContext();

        context.addRoutes(new RouteBuilder() {

            public void configure() throws Exception {
                System.out.println("hello world");
                from("file://target/?fileName=doctor.txt&charset=utf-8")
                        .process(new Processor() {
                            public void process(Exchange msg) throws Exception {
                                System.out.println(msg.getIn().getBody(
                                        String.class));

                            }

                        }).to("rabbitmq://localhost:15672?queue=hello");

            }
        });

        context.start();
        Thread.sleep(4000);
        context.stop();
    }
}

Producer.java

package test;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

import com.rabbitmq.client.ConnectionFactory;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.Channel;

public class Producer {
    public static final String QUEUE_NAME = "hello";
    public static Connection connection;

    public void setupConnection() throws IOException, TimeoutException {
        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost("localhost");

        connection = factory.newConnection();
        Channel channel = connection.createChannel();
        channel.queueDeclare(QUEUE_NAME, false, false, false, null);
    }

}

如果我不使用Camel并尝试使用标准rabbitmq库联系队列,该程序正常工作 .

2 回答

  • 1

    错误消息非常具有说明性:您缺少 endpoints URI中的交换名称,它不能为空,因为the official Camel documentation表示必须遵循给定的格式:

    rabbitmq:// hostname [:port] / exchangeName?[options]

    我建议您尝试使用 amq.direct 交换(有关详细信息,请参阅official RabbitMQ documentation),如下所示:

    public void configure() throws Exception {
        System.out.println("hello world");
        from("file://target/?fileName=doctor.txt&charset=utf-8")
            .process(new Processor() {
                public void process(Exchange msg) throws Exception {
                    System.out.println(msg.getIn().getBody(String.class));
                }
    
            }).to("rabbitmq://localhost:5672/amq.direct?routingKey=hello");
    }
    

    此外,您正在使用 15672 端口,默认情况下,RabbitMQ的端口's web management console, I guess you haven' t更改了默认设置,因此端口需要为 5672 . 您还必须使用 routingKey 参数而不是 queue 参数,因为在RabbitMQ语义中,您发布到 exchange:routingKey 并且仅从队列中消耗 .

  • -1

    您需要包含rabbitmq交换名称 . 它在这里不见了;

    .to("rabbitmq://localhost:15672?queue=hello");
    

    它应该是这样的:

    .to("rabbitmq://localhost:15672/exchangeName?routingKey=hello");
    

    此外,如果您要发送到Exchange,为什么要指定队列?您只需要指定路由密钥,如果存在该路由密钥的绑定,交换将发送到该队列 .

相关问题