【问题标题】:how to create an REST API then the REST API will put object/string to ActiveMQ如何创建 REST API 然后 REST API 会将对象/字符串放入 ActiveMQ
【发布时间】:2019-12-05 13:13:31
【问题描述】:

我正在尝试实现 spring-boot camel。我应该创建一个 REST API,然后 REST API 会将对象/字符串放入 ActiveMQ。 我已经完成了:

 rest("/test/").description("Teste REST Service")
        .id("api-route")
        .post("/bean")
        .consumes(MediaType.APPLICATION_JSON_VALUE)
        .produces(MediaType.APPLICATION_JSON_VALUE)
        .bindingMode(RestBindingMode.auto)
        .type(User.class)
        .enableCORS(true)
        .to("seda:next");

        from("seda:next")
        .routeId("direct-route")
        .tracing()
        .log(">>> ${body.id}")
        .log(">>> ${body.name}")
        .transform().simple("Hello ${in.body.name}")
        .to("activemq:queue:foo")
      //  .to("direct:remoteService1")
        ; 

错误日志 ============================================== ==========:

2019-12-05 19:51:19.750  INFO 23436 --- [ActiveMQ Task-1] o.a.a.t.failover.FailoverTransport       : Successfully connected to tcp://localhost:61616
2019-12-05 19:51:19.775  INFO 23436 --- [ActiveMQ Task-1] o.a.a.t.failover.FailoverTransport       : Successfully connected to tcp://localhost:61616
2019-12-05 19:51:40.553  WARN 23436 --- [rOnTimeout[foo]] o.a.c.c.j.r.TemporaryQueueReplyManager   : Timeout occurred after 20000 millis waiting for reply message with correlationID [Camel-ID-DESKTOP-PJH5GNF-1575550269030-0-7] on destination temp-queue://ID:DESKTOP-PJH5GNF-58146-1575550279560-1:1:1. Setting ExchangeTimedOutException on (MessageId: ID-DESKTOP-PJH5GNF-1575550269030-0-2 on ExchangeId: ID-DESKTOP-PJH5GNF-1575550269030-0-3) and continue routing.
2019-12-05 19:51:40.583 ERROR 23436 --- [rOnTimeout[foo]] o.a.camel.processor.DefaultErrorHandler  : Failed delivery for (MessageId: ID-DESKTOP-PJH5GNF-1575550269030-0-2 on ExchangeId: ID-DESKTOP-PJH5GNF-1575550269030-0-3). Exhausted after delivery attempt: 1 caught: org.apache.camel.ExchangeTimedOutException: The OUT message was not received within: 20000 millis due reply message with correlationID: Camel-ID-DESKTOP-PJH5GNF-1575550269030-0-7 not received on destination: temp-queue://ID:DESKTOP-PJH5GNF-58146-1575550279560-1:1:1. Exchange[ID-DESKTOP-PJH5GNF-1575550269030-0-3]


---------------------------------------------------------------------------------------------------------------------------------------
RouteId              ProcessorId          Processor                                                                        Elapsed (ms)
[direct-route      ] [direct-route      ] [seda://next                                                                   ] [     21171]
[route8            ] [to7               ] [seda:next                                                                     ] [         0]
[direct-route      ] [log1              ] [log                                                                           ] [        21]
[direct-route      ] [log2              ] [log                                                                           ] [         0]
[direct-route      ] [transform1        ] [transform[simple{Hello ${in.body.name}}]                                      ] [         1]
[direct-route      ] [to2               ] [activemq:queue:foo                                                            ] [         0]
---------------------------------------------------------------------------------------------------------------------------------------

org.apache.camel.ExchangeTimedOutException: The OUT message was not received within: 20000 millis due reply message with correlationID: Camel-ID-DESKTOP-PJH5GNF-1575550269030-0-7 not received on destination: temp-queue://ID:DESKTOP-PJH5GNF-58146-1575550279560-1:1:1. Exchange[ID-DESKTOP-PJH5GNF-1575550269030-0-3]
    at org.apache.camel.component.jms.reply.ReplyManagerSupport.processReply(ReplyManagerSupport.java:169) ~[camel-jms-3.0.0-M1.jar:3.0.0-M1]
    at org.apache.camel.component.jms.reply.TemporaryQueueReplyHandler.onTimeout(TemporaryQueueReplyHandler.java:60) ~[camel-jms-3.0.0-M1.jar:3.0.0-M1]
    at org.apache.camel.component.jms.reply.CorrelationTimeoutMap$1.run(CorrelationTimeoutMap.java:58) ~[camel-jms-3.0.0-M1.jar:3.0.0-M1]
    at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) ~[na:1.8.0_171]
    at java.util.concurrent.FutureTask.run(Unknown Source) ~[na:1.8.0_171]
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source) ~[na:1.8.0_171]
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source) ~[na:1.8.0_171]
    at java.lang.Thread.run(Unknown Source) ~[na:1.8.0_171]

【问题讨论】:

    标签: java spring apache-camel activemq


    【解决方案1】:

    如果您期待您的消息回复到您的 foo 队列,那么您需要通过发送 inOnly 类型的交换来告诉 activeMQ 您不期待回复,否则它将创建一个临时排队等待回复:

    from("seda:next")
        .routeId("direct-route")
        .tracing()
        .log(">>> ${body.id}")
        .log(">>> ${body.name}")
        .transform().simple("Hello ${in.body.name}")
        .inOnly("activemq:queue:foo")
    

    这将导致您的“Hello Name”消息作为seda:next 的结果返回,从而返回到其余调用。

    或者,你可以有

    .inOnly("seda:next")
    

    如果您不希望来自“下一个”路由的响应,则在您的第一条路由中,我认为这意味着您的休息调用将获得原始输入类作为响应(但不要不要引用我的话)。

    【讨论】:

    • @wahyuekosaputro 如果对您的问题有效,请将答案标记为正确的答案
    猜你喜欢
    • 2015-10-29
    • 2011-04-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-08
    • 2018-12-13
    • 1970-01-01
    • 2013-10-09
    相关资源
    最近更新 更多