簡體   English   中英

帶有 Spring Boot 2.3 的 Jpos Q2 服務器

[英]Jpos Q2 Server with Spring boot 2.3

我們有一個帶有 Q2 Server 和 Spring MVC 的現有系統,配置如下。 Q2 服務器是在 Httpservlet 啟動時創建的,它運行良好,Spring bean 可以使用 ISORequestlistner 自動裝配。 現在正在轉換為 Spring boot 2.3。 一旦我在 Spring Boot 中使用 ServletRegistrationBean 啟動了相同的 Httpservlet,Q2 服務器就會啟動並且可以向它發送請求。 但是自動接線不起作用。 一旦我檢查。 一旦他的請求在 ISORequest 偵聽器中處理,Spring 上下文就不可見,因為 Q2 服務器使用不同的類加載器。

 <server class="org.jpos.q2.iso.QServer" logger="Q2" name="DownloadServer-A"> <attr name="port" type="java.lang.Integer">6400</attr> <attr name="minSessions" type="java.lang.Integer">10</attr> <attr name="maxSessions" type="java.lang.Integer">1100</attr> <channel name="DownloadServer-A-Channel" class="org.jpos.iso.channel.NACChannel" logger="Q2" packager="org.jpos.iso.packager.GenericPackager" header="6000010000"> <property name="packager-config" value="/app/repository/q2serverconfig/resources/Download_generic.xml" /> </channel> <request-listener class="DownloadServerAListener" logger="Q2"> <property name="space" value="transient:default" /> <property name="queue" value="TransactionQueue" /> <property name="timeout" value="35000" /> </request-listener> </server>
第一次嘗試嘗試使用 ApplicationContextAware 創建靜態 ApplicationContext 並在 ISORequestListner 中嘗試。 但是當 TCP 請求接收到 Q2 服務器時它變為空。

第二次嘗試我嘗試了幾種解決方案,如下面的 github repo。 但我沒有工作。 https://github.com/vmantek/chimera

有沒有人嘗試在 Spring Application 上下文中作為 bean 啟動 ISO Server? 我的意思是使用 Q2.start() 在 @Configuration 類中啟動 ISO 服務器。 Q2.start 將在單獨的類加載器中啟動。 我不希望它發生。

這幾天我一直在尋找,我嘗試了幾種方法。 問題是 Spring 在特定類加載器中啟動。 但是當你啟動 Q2 Server 時

Q2 q2Server = new Q2(<deploydir>);
q2Server.start();

Q2 服務器在不同的類加載器中啟動。 因此 SpringContext 不可用於自動裝配。 SpringBeanAutowiringSupport 依賴於 ContextLoader 來檢索當前的應用程序上下文,並且總是獲取 null。

解決方法

您可以注冊一個實現 org.springframework.boot.context.embedded.ServletContextInitializer 的 bean 以在啟動期間檢索應用程序上下文。

@Configuration
public class WebApplicationContextLocator implements ServletContextInitializer {
    private static WebApplicationContext webApplicationContext;

    public static WebApplicationContext getCurrentWebApplicationContext() {
        return webApplicationContext;
    }

    @Override
    public void onStartup(ServletContext servletContext) throws ServletException {
      webApplicationContext = 
            WebApplicationContextUtils.getWebApplicationContext(servletContext);
    }
}

然后你可以在你的 ISORequestListener 類中實現自我自動裝配。

 @Component
 public class ServiceImpl implements ISORequestListener {
    @Autowired
    private BackendService backendService;

    public ServiceImpl() {
           AutowiredAnnotationBeanPostProcessor bpp = new 
                       AutowiredAnnotationBeanPostProcessor();
           WebApplicationContext currentContext = 
                       WebApplicationContextLocator.getCurrentWebApplicationContext();
           bpp.setBeanFactory(currentContext.getAutowireCapableBeanFactory());
           bpp.processInjection(this);
    }

}

然后自動接線工作完美。 我受到以下答案的啟發。 Spring Boot 將 JAX-WS webservice 注冊為 bean

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM