spring引导和spring集成问题

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

我有一个springboot应用程序。我有一个专用服务器,在那里我将通过httpget请求读取数据。我为spring集成模块配置了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:为什么自动布线不起作用?
问题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-核心

相关问题