动态HTTP入站通道适配器

时间:2015-10-19 21:50:56

标签: spring spring-integration

我正在尝试使用由数据库值生成的不同路径创建动态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。

但是,当请求进入时, DispatcherServlet 中未检测到 IntegrationRequestMappingHandlerMapping 。有一个 IntegrationRequestMappingHandlerMapping ,它存在于 DispatcherServlet 中,但我没有映射。

我怀疑这是因为动态上下文是一个孩子而 DispatcherServlet 无法看到?

有没有办法使这项工作?

1 个答案:

答案 0 :(得分:0)

即使您可以添加映射,DispatcherServlet也无法查看子上下文中的bean,因此无法调用处理程序。

根据路径变量,拥有单个入站端点并添加路由器以路由到不同的通道可能更容易。