Warm tip: This article is reproduced from serverfault.com, please click

java-Spring Boot和Spring集成。

(java - Spring Boot and Spring Integration. Problems)

发布于 2020-12-02 20:20:31

我有一个SpringBoot应用程序。我有一台专用的服务器,在这里我将通过HTTP GET请求读取数据。我为Spring Integration模块配置了http-outbound-config.xml文件。 当我运行以下代码时,一切都很好

http-outbound-config.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xmlns:int="http://www.springframework.org/schema/integration"
   xmlns:int-http="http://www.springframework.org/schema/integration/http"
   xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/integration https://www.springframework.org/schema/integration/spring-integration.xsd
    http://www.springframework.org/schema/integration/http https://www.springframework.org/schema/integration/http/spring-integration-http.xsd">

<int:channel id="requestChannel"/>
<int:channel id="replyChannel">
    <int:queue capacity='10'/>
</int:channel>

<int-http:outbound-gateway id="outboundGateway"
                           request-channel="requestChannel"
                           url="http://server/API.jsp?id=1"
                           http-method="GET"
                           expected-response-type="java.lang.String"
                           charset="UTF-8"
                           reply-channel="replyChannel"/>
</beans>

主要应用类别:

package test;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ImportResource;
import ru.eco.products.waste.egr.Integration;

@SpringBootApplication
@ImportResource("/META-INF/spring/integration/http-outbound-config.xml")
public class Application {
    public static void main(String[] args) {
        Integration integration = new Integration();
        integration.start();
        SpringApplication.run(WasteWebClientApplication.class,
                              args
                             );
    }
    
}

整合类:

package test;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportResource;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.messaging.Message;
import org.springframework.messaging.MessageChannel;
import org.springframework.messaging.PollableChannel;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

@Component
@Configuration
public class Integration { 
    public void start() {
        ClassPathXmlApplicationContext
               context = new ClassPathXmlApplicationContext("classpath:META-INF/spring/integration/http-outbound-config.xml");
        context.start();


        MessageChannel requestChannel = context.getBean("requestChannel", MessageChannel.class);
        PollableChannel replyChannel = context.getBean("replyChannel", PollableChannel.class);
        Message<?> message = MessageBuilder.withPayload("").build();
        requestChannel.send(message);
        
        Message<?> receivedMsg = replyChannel.receive();
        System.out.println("RESULT IS : " + receivedMsg.getPayload());
    }
}

但是,当我尝试自动连接MessageChannel和PollableChannel时,会收到一个空指针异常。

集成类(不工作示例):

package test;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportResource;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.messaging.Message;
import org.springframework.messaging.MessageChannel;
import org.springframework.messaging.PollableChannel;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

@Component
@Configuration
public class Integration {
    @Autowired
    @Qualifier("requestChannel")
    MessageChannel requestChannel;
    @Autowired
    @Qualifier("replyChannel")
    PollableChannel replyChannel;
    
    public void start() {
        Message<?> message = MessageBuilder.withPayload("").build();
        requestChannel.send(message);
        
        Message<?> receivedMsg = replyChannel.receive();
        System.out.println("RESULT IS : " + receivedMsg.getPayload()); 
    }
}

  1. 问题1:为什么自动装配不起作用?
  2. 问题2:从专用服务器获取数据并将其保存到DB的最佳方法是什么?这样的配置可以吗?我将为响应创建一个模型类,然后通过JPA将其保存到DB中。
  3. 问题3:我需要从服务器读取异步模式下的作品。如何在Spring Boot中实现它?因此,这里的主要思想是,我将从UI接收POST方法,并将启动与Web服务的集成。集成完成后,我需要通知用户。
  4. 问题4:也许 Camel 会是这里最好的解决方案?

堆栈:Java 11,Spring Boot,百里香+引导程序。

预先感谢你的答复。

Questioner
Andrei Gavrilov
Viewed
0
Artem Bilan 2020-12-03 04:34:11
  1. 既然这样做了new Integration();,你肯定不会有依赖项注入,因为不涉及控制容器的反转。尽管完全不清楚,new ClassPathXmlApplicationContext("classpath:META-INF/spring/integration/http-outbound-config.xml")如果已经做过Spring Boot并适当的话,为什么还需要这么做@ImportResource

  2. 与该@ImportResourceSpring Integration XML配置一样,这最好的方法然后,你需要访问ApplicationContextSpringApplication.run()getBean(Integration.class)调用你的start()方法。但是,你完全需要忘记new Integratio()Spring将为此管理一个bean,Integration然后依赖注入将起作用。

  3. 可以通过ExecutorChannelSpring Integration实现异步模式因此,当你向该通道发送消息时,逻辑将在其他线程上进行处理。但是,尚不清楚为什么要陈述这样的要求,因为你仍然要通过该条件进行阻止replyChannel.receive()...尽管应在单独的SO线程中解决此问题。

  4. Camel VS Spring Integration的问题超出了StackOverflow策略。

在这个问题的背景下,Thymeleaf和Bootstrap产生了误导。

请考虑考虑如何在此处正确询问:https : //stackoverflow.com/help/how-to-ask

另请阅读一些有关依赖项注入和控制反转的文档:https : //docs.spring.io/spring-framework/docs/current/reference/html/core.html#spring-core