300.7.3. OSGi를 사용할 때 샘플
Camel 2.6.0.6.0에서 다음과 같은 블루프린트가 포함된 OSGi 서비스로 CamelHttpTransportServlet 을 게시할 수 있습니다.
<blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.osgi.org/xmlns/blueprint/v1.0.0 https://www.osgi.org/xmlns/blueprint/v1.0.0/blueprint.xsd">
<bean id="camelServlet" class="org.apache.camel.component.servlet.CamelHttpTransportServlet" />
<!--
Enlist it in OSGi service registry.
This will cause two things:
1) As the pax web whiteboard extender is running the CamelServlet will
be registered with the OSGi HTTP Service
2) It will trigger the HttpRegistry in other bundles so the servlet is
made known there too
-->
<service ref="camelServlet">
<interfaces>
<value>javax.servlet.Servlet</value>
<value>org.apache.camel.http.common.CamelServlet</value>
</interfaces>
<service-properties>
<entry key="alias" value="/camel/services" />
<entry key="matchOnUriPrefix" value="true" />
<entry key="servlet-name" value="CamelServlet" />
</service-properties>
</service>
</blueprint>Camel 경로에서 이 서비스를 다음과 같이 사용하십시오.
<blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0"
xmlns:ext="http://aries.apache.org/blueprint/xmlns/blueprint-ext/v1.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.osgi.org/xmlns/blueprint/v1.0.0 https://www.osgi.org/xmlns/blueprint/v1.0.0/blueprint.xsd">
<reference id="servletref" ext:proxy-method="classes" interface="org.apache.camel.http.common.CamelServlet">
<reference-listener ref="httpRegistry" bind-method="register" unbind-method="unregister" />
</reference>
<bean id="httpRegistry" class="org.apache.camel.component.servlet.DefaultHttpRegistry" />
<bean id="servlet" class="org.apache.camel.component.servlet.ServletComponent">
<property name="httpRegistry" ref="httpRegistry" />
</bean>
<bean id="servletProcessor" class="org.apache.camel.example.servlet.ServletProcessor" />
<camelContext xmlns="http://camel.apache.org/schema/blueprint">
<route>
<!-- Notice how we can use the servlet scheme which is that reference above -->
<from uri="servlet://hello" />
<process ref="servletProcessor" />
</route>
</camelContext>
</blueprint>
Camel 2.6 이전 버전의 경우 Activator 를 사용하여 OSGi 플랫폼에 CamelHttpTransportServlet 을 게시할 수 있습니다.
import java.util.Dictionary;
import java.util.Hashtable;
import org.apache.camel.component.servlet.CamelHttpTransportServlet;
import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceReference;
import org.osgi.service.http.HttpContext;
import org.osgi.service.http.HttpService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.osgi.context.BundleContextAware;
public final class ServletActivator implements BundleActivator, BundleContextAware {
private static final Logger LOG = LoggerFactory.getLogger(ServletActivator.class);
private static boolean registerService;
/**
* HttpService reference.
*/
private ServiceReference<?> httpServiceRef;
/**
* Called when the OSGi framework starts our bundle
*/
public void start(BundleContext bc) throws Exception {
registerServlet(bc);
}
/**
* Called when the OSGi framework stops our bundle
*/
public void stop(BundleContext bc) throws Exception {
if (httpServiceRef != null) {
bc.ungetService(httpServiceRef);
httpServiceRef = null;
}
}
protected void registerServlet(BundleContext bundleContext) throws Exception {
httpServiceRef = bundleContext.getServiceReference(HttpService.class.getName());
if (httpServiceRef != null && !registerService) {
LOG.info("Register the servlet service");
final HttpService httpService = (HttpService)bundleContext.getService(httpServiceRef);
if (httpService != null) {
// create a default context to share between registrations
final HttpContext httpContext = httpService.createDefaultHttpContext();
// register the hello world servlet
final Dictionary<String, String> initParams = new Hashtable<String, String>();
initParams.put("matchOnUriPrefix", "false");
initParams.put("servlet-name", "CamelServlet");
httpService.registerServlet("/camel/services", // alias
new CamelHttpTransportServlet(), // register servlet
initParams, // init params
httpContext // http context
);
registerService = true;
}
}
}
public void setBundleContext(BundleContext bc) {
try {
registerServlet(bc);
} catch (Exception e) {
LOG.error("Cannot register the servlet, the reason is " + e);
}
}
}