2015-10-19 138 views
0

我正嘗試創建一個動態Http入站通道適配器,其中包含由數據庫值生成的不同路徑。動態HTTP入站通道適配器

我使用[https://github.com/spring-projects/spring-integration-samples/tree/master/advanced/dynamic-ftp]中使用的相同主體。

的動態上下文看起來如下:

<context:property-placeholder /> 

    <int-http:inbound-channel-adapter id="dynamicInboundEdi846HttpRequest" channel="dynamicInboundEdi846HttpChannel" auto-startup="${startup}" 
            path="${url}" supported-methods="POST"/> 

    <int:header-enricher input-channel="dynamicInboundEdi846HttpChannel" output-channel="${successChannel}"> 
     <int:header name="tsp" value="${tsp}"/> 
    </int:header-enricher> 

    <bean id="multipartResolver" class="com.bstonetech.ptms.integration.service.http.CustomMultipartResolver"/> 


    <int:channel id="dynamicInboundEdi846HttpChannel"/> 
    <int:channel id="inboundDynamEdiHttpErrorChannel"/> 

動態適配器代碼如下:

public class DynamicHttpAdapter implements ApplicationContextAware { 
    private static ApplicationContext context; 
    private static final Logger log = LoggerFactory.getLogger(DynamicHttpAdapter.class); 

    @Override 
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { 
     context = applicationContext; 
    } 

    @Autowired 
    private IFileService fileService; 

    private final Map<String , ConfigurableApplicationContext> contexts = 
      new HashMap<String , ConfigurableApplicationContext>(); 

    private List<String> tspMap; 

    public DynamicHttpAdapter(List<String> tspMap) { 
     this.tspMap = tspMap; 
    } 

    public String resolve(String name, String propType, String autostartProp,String successChannel) { 
     /* loop through tsps and see if we have a context already deifined */ 
     for (String tsp : tspMap) { 
     String key = tsp + propType; 
     if (!contexts.containsKey(tsp)) { 
      log.debug("create context for: " + key); 
      createNewTspContext(name, propType, tsp, autostartProp, successChannel); 
     } 
     } 
     return "done"; 

    } 

    private synchronized void createNewTspContext(String name, String propType, String tsp, String autostartProp, 
               String successChannel) { 

     ConfigurableApplicationContext httpContext = new ClassPathXmlApplicationContext(
       new String[] { "/META-INF/spring/integration/dynamic-http-inbound-adapter-context.xml" }, 
       false, context); 

     if (this.setEnvironmentForTsp(httpContext, name, propType, tsp, autostartProp, successChannel)) { 
     httpContext.refresh(); 
     String key = tsp + propType; 
     this.contexts.put(tsp, httpContext); 
     } 

    } 

    /** 
    */ 
    private Boolean setEnvironmentForTsp(ConfigurableApplicationContext ctx, String name, String propType, String tsp, String autostartProp, String successChannel) { 
     StandardEnvironment env = new StandardEnvironment(); 
     Properties httpProps = new Properties(); 
     // populate properties for tsp 
     httpProps.setProperty("startup", fileService.getPropertyForTsp(tsp,autostartProp, propType)); 
     httpProps.setProperty("tsp", tsp); 
     httpProps.setProperty("successChannel", successChannel); 
     httpProps.setProperty("url", "/" + tsp.toLowerCase() + "/" + name); 

     PropertiesPropertySource pps = new PropertiesPropertySource("httpProps", httpProps); 
     env.getPropertySources().addLast(pps); 
     ctx.setEnvironment(env); 
     return true; 

    } 

    public Map<String , ConfigurableApplicationContext> getContexts(){ 
     return contexts; 
    } 

ConfigurableApplicationContext被創建,所述屬性中設置和上下文加入到ApplicationContext

IntegrationRequestMappingHandlerMapping是使用正確的處理程序和映射啓動的。

然而,當一個請求到達在IntegrationRequestMappingHandlerMapping未在DispatcherServlet的檢測。有一個IntegrationRequestMappingHandlerMapping它存在於DispatcherServlet但我沒有映射。

我懷疑這是因爲動態環境是一個孩子,並且不能通過DispatcherServlet

有什麼辦法讓這項工作?

回答

0

即使可以添加映射,DispatcherServlet也不能看到子上下文中的bean,因此它無法調用處理程序。

根據路徑變量,擁有單個入站端點並添加路由器以路由到不同的通道可能會更容易。

相關問題