пятница, октября 14, 2016

Dynamic Webservice registration on ServiceMix

Рассмотрим как можно динамически зарегистрировать webservice на ServiceMix. В общем смысле подобным образом можно динамически регистрировать любые сервисы. Для примера реализуем простейший сервис c использованием Apache Camel. Сервис будет слушать файловую папку, в которую будет выгружаться файлы со ссылками на wsdl. Эти сервисы и будем динамически регистрировать.

Общий смысл такой. Создадим bean с методом registy который будет принимать ссылку на wsdl, создавать соответствующую конечную точку(endpoint) и динамически добавлять route, принимающий сообщения из нее.


package com.bssys.dynamic.cxf_example;

import javax.xml.namespace.QName;

import org.apache.camel.CamelContext;
import org.apache.camel.component.cxf.CxfEndpoint;
import org.apache.camel.component.cxf.DataFormat;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component("dynamicLogic")
public class DynamicLogic {
 
 @Autowired
 private CamelContext context;
 
 public void registry(String url) throws Exception{
  
  CxfEndpoint cxfEndpoint = new CxfEndpoint();
  cxfEndpoint.setDataFormat(DataFormat.RAW);
  cxfEndpoint.setWsdlURL(url);
  
  QName serviceName = cxfEndpoint.getServiceName();
  
  cxfEndpoint.setAddress("http://127.0.0.1:8383/"+serviceName.getLocalPart());
  
  context.addEndpoint(serviceName.getLocalPart(), cxfEndpoint);
  DynamicRouteBuilder builder = new DynamicRouteBuilder(serviceName.getLocalPart());
  context.addRoutes(builder);
  
 }
 
}
Динамический route принимающий сообщения из зарегистрированной конечной точки
package com.bssys.dynamic.cxf_example;

import org.apache.camel.builder.RouteBuilder;

public class DynamicRouteBuilder extends RouteBuilder {

 private String uri;

 public DynamicRouteBuilder(String uri) {
  this.uri = uri;
 }

 @Override
 public void configure() throws Exception {
  from(uri).to("direct:template");
 }

}
Маршрут Apache Camel
<?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:camel="http://camel.apache.org/schema/spring"
 xmlns:osgi="http://www.springframework.org/schema/osgi" xmlns:context="http://www.springframework.org/schema/context"
 xsi:schemaLocation="
       http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
       http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/osgi http://www.springframework.org/schema/osgi/spring-osgi.xsd
       http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd">

 <context:annotation-config />
 <context:component-scan base-package="com.bssys.dynamic.cxf_example" />

 <camelContext id="mainContext" xmlns="http://camel.apache.org/schema/spring">
  <route>
   <from uri="file://C:/data/registryws" />
   <camel:bean ref="dynamicLogic" method="registry" />
  </route>
  <route>
   <from uri="direct:template" />
   <log message="The message contains ${body}" />
  </route>
 </camelContext>

</beans>

Комментариев нет: