【问题标题】:Need to send Json to JMS using Apache Camel Spring Boot需要使用 Apache Camel Spring Boot 将 Json 发送到 JMS
【发布时间】:2020-04-06 15:15:01
【问题描述】:

我正在为 Apache Camel 使用 spring-boot,我能够将消息从一个队列发送到另一个队列。

blow 是代码

import com.google.gson.Gson;
import org.apache.camel.Exchange;
import org.apache.camel.LoggingLevel;
import org.apache.camel.Processor;
import org.apache.camel.builder.RouteBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

@Component
public class JmsRoute extends RouteBuilder {

    static final Logger log = LoggerFactory.getLogger(JmsRoute.class);

    @Override
    public void configure() throws Exception {

        from("{{inbound.endpoint}}")
                .transacted()
                .log(LoggingLevel.INFO, log, "Recived Message")
                .process(new Processor() {
                    @Override
                    public void process(Exchange exchange) throws Exception {
                        Student student = new Student();
                        Gson gson = new Gson();
                        String json = gson.toJson(student);
                        log.info("Exchange: {}", exchange.getMessage().getBody());
                        log.info("**********:{}", exchange.getMessage());
                    }
                })
                .loop()
                .simple("{{outbound.loop.count}}")
                .to("{{outbound.endpoint}}")
                .log(LoggingLevel.INFO, log, "Message Sent")
                .end();
    }
}

我需要发送以将 Object 转换为 JSON(我可以使用 Gson 进行转换),然后通过队列发送。

我是 Camel 的新手,并试图通过互联网找到解决方案,但无法获得任何帮助。

有人可以帮忙吗?

【问题讨论】:

    标签: spring-boot apache-camel jms


    【解决方案1】:

    您没有将 json 设置为交换体。

    public void process(Exchange exchange) throws Exception {
                            Student student = new Student();
                            Gson gson = new Gson();
                            String json = gson.toJson(student);
                            exchange.getIn().setBody(json); //processor does not do this automatically
                            log.info("Exchange: {}", exchange.getMessage().getBody());
                            log.info("**********:{}", exchange.getMessage());
                        }
    

    我建议查看 apache camel 的新文档页面。他们都是伟大的。特别是如果您刚刚开始使用该框架。见https://camel.apache.org/manual/latest/getting-started.html

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-12-03
      • 1970-01-01
      • 1970-01-01
      • 2018-01-17
      • 1970-01-01
      • 1970-01-01
      • 2015-01-20
      • 1970-01-01
      相关资源
      最近更新 更多