spring引导和spring集成问题

ffdz8vbo  于 2021-07-07  发布在  Java
关注(0)|答案(1)|浏览(470)

我有一个springboot应用程序。我有一个专用服务器,在那里我将通过httpget请求读取数据。我为spring集成模块配置了http-outbound-config.xml文件。运行以下代码时,一切正常:
http-outbound-config.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:int="http://www.springframework.org/schema/integration"
  5. xmlns:int-http="http://www.springframework.org/schema/integration/http"
  6. xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd
  7. http://www.springframework.org/schema/integration https://www.springframework.org/schema/integration/spring-integration.xsd
  8. http://www.springframework.org/schema/integration/http https://www.springframework.org/schema/integration/http/spring-integration-http.xsd">
  9. <int:channel id="requestChannel"/>
  10. <int:channel id="replyChannel">
  11. <int:queue capacity='10'/>
  12. </int:channel>
  13. <int-http:outbound-gateway id="outboundGateway"
  14. request-channel="requestChannel"
  15. url="http://server/API.jsp?id=1"
  16. http-method="GET"
  17. expected-response-type="java.lang.String"
  18. charset="UTF-8"
  19. reply-channel="replyChannel"/>
  20. </beans>

主要应用类别:

  1. package test;
  2. import org.springframework.boot.SpringApplication;
  3. import org.springframework.boot.autoconfigure.SpringBootApplication;
  4. import org.springframework.context.annotation.ImportResource;
  5. import ru.eco.products.waste.egr.Integration;
  6. @SpringBootApplication
  7. @ImportResource("/META-INF/spring/integration/http-outbound-config.xml")
  8. public class Application {
  9. public static void main(String[] args) {
  10. Integration integration = new Integration();
  11. integration.start();
  12. SpringApplication.run(WasteWebClientApplication.class,
  13. args
  14. );
  15. }
  16. }

集成类:

  1. package test;
  2. import org.springframework.beans.factory.annotation.Autowired;
  3. import org.springframework.beans.factory.annotation.Qualifier;
  4. import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
  5. import org.springframework.context.annotation.Configuration;
  6. import org.springframework.context.annotation.ImportResource;
  7. import org.springframework.context.support.ClassPathXmlApplicationContext;
  8. import org.springframework.messaging.Message;
  9. import org.springframework.messaging.MessageChannel;
  10. import org.springframework.messaging.PollableChannel;
  11. import org.springframework.messaging.support.MessageBuilder;
  12. import org.springframework.stereotype.Component;
  13. import org.springframework.web.client.RestTemplate;
  14. @Component
  15. @Configuration
  16. public class Integration {
  17. public void start() {
  18. ClassPathXmlApplicationContext
  19. context = new ClassPathXmlApplicationContext("classpath:META-INF/spring/integration/http-outbound-config.xml");
  20. context.start();
  21. MessageChannel requestChannel = context.getBean("requestChannel", MessageChannel.class);
  22. PollableChannel replyChannel = context.getBean("replyChannel", PollableChannel.class);
  23. Message<?> message = MessageBuilder.withPayload("").build();
  24. requestChannel.send(message);
  25. Message<?> receivedMsg = replyChannel.receive();
  26. System.out.println("RESULT IS : " + receivedMsg.getPayload());
  27. }
  28. }

但是,当我尝试自动连接messagechannel和pollablechannel时,我收到一个空指针异常。
集成类(非工作示例):

  1. package test;
  2. import org.springframework.beans.factory.annotation.Autowired;
  3. import org.springframework.beans.factory.annotation.Qualifier;
  4. import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
  5. import org.springframework.context.annotation.Configuration;
  6. import org.springframework.context.annotation.ImportResource;
  7. import org.springframework.context.support.ClassPathXmlApplicationContext;
  8. import org.springframework.messaging.Message;
  9. import org.springframework.messaging.MessageChannel;
  10. import org.springframework.messaging.PollableChannel;
  11. import org.springframework.messaging.support.MessageBuilder;
  12. import org.springframework.stereotype.Component;
  13. import org.springframework.web.client.RestTemplate;
  14. @Component
  15. @Configuration
  16. public class Integration {
  17. @Autowired
  18. @Qualifier("requestChannel")
  19. MessageChannel requestChannel;
  20. @Autowired
  21. @Qualifier("replyChannel")
  22. PollableChannel replyChannel;
  23. public void start() {
  24. Message<?> message = MessageBuilder.withPayload("").build();
  25. requestChannel.send(message);
  26. Message<?> receivedMsg = replyChannel.receive();
  27. System.out.println("RESULT IS : " + receivedMsg.getPayload());
  28. }
  29. }

问题1:为什么自动布线不起作用?
问题2:从专用服务器获取数据并将其保存到数据库中的最佳方法是什么?这样可以吗?我将为响应创建一个模型类,然后通过jpa将其保存到db中。
问题3:我需要从异步模式下工作的服务器读取数据。如何在spring boot中实现它?所以这里的主要思想是我将从ui接收post方法,并启动与web服务的集成。集成完成后,我需要通知用户。
问题4:也许 Camel 是最好的解决方案?
栈:java11,spring boot,thymeleaf+bootstrap。
提前谢谢你的回答。

w7t8yxp5

w7t8yxp51#

既然你这么做了 new Integration(); ,您肯定不会有依赖注入,因为不涉及控制容器的反转。虽然还不清楚你为什么需要这个 new ClassPathXmlApplicationContext("classpath:META-INF/spring/integration/http-outbound-config.xml") 如果你已经做了Spring Boot和适当的 @ImportResource .
最好的方法就是 @ImportResource 对于spring集成xml配置。然后你需要进入 ApplicationContextSpringApplication.run() 以及 getBean(Integration.class) 打电话给你的 start() 方法。但是你完全需要忘记 new Integratio() . 斯普林将为此管理一个豆子 Integration 然后依赖注入就可以工作了。
异步模式可以通过 ExecutorChannel Spring的时候。因此,当您向这个通道发送消息时,逻辑将在另一个线程上处理。然而,现在还不清楚为什么你会提出这样的要求,因为你仍然要通过它来阻止 replyChannel.receive() ... 尽管这应该在一个单独的so线程中解决。
camel vs spring集成问题超出了stackoverflow策略。
在这个问题上,thymeleaf和bootstrap是误导性的。
请考虑学习如何正确提问,以便:https://stackoverflow.com/help/how-to-ask
同时阅读一些关于依赖注入和控制反转的文档:https://docs.spring.io/spring-framework/docs/current/reference/html/core.html#spring-核心

相关问题