How to create a Servlet component?

classic Classic list List threaded Threaded
10 messages Options
Reply | Threaded
Open this post in threaded view
|

How to create a Servlet component?

janylj
Hello,

Camel is pretty new to me. This might be a simple question but I cannot find the answer in the web.

I am following Servlet Component page (http://camel.apache.org/servlet.html). However, when I start Tomcat, there are repeating warning message said,

Aug 10, 2009 3:44:03 PM org.apache.camel.impl.DefaultPollingConsumerPollStrategy rollback
WARNING: Consumer Consumer[servlet:/hello] could not poll endpoint: servlet:/hello caused by: unsupported protocol: 'servlet'
java.lang.IllegalStateException: unsupported protocol: 'servlet'
        at org.apache.commons.httpclient.protocol.Protocol.lazyRegisterProtocol(Protocol.java:149)
        at org.apache.commons.httpclient.protocol.Protocol.getProtocol(Protocol.java:117)
        at org.apache.commons.httpclient.HttpHost.<init>(HttpHost.java:107)
        at org.apache.commons.httpclient.HttpMethodBase.setURI(HttpMethodBase.java:280)
        at org.apache.commons.httpclient.HttpMethodBase.<init>(HttpMethodBase.java:220)
        at org.apache.commons.httpclient.methods.GetMethod.<init>(GetMethod.java:89)
        at org.apache.camel.component.http.HttpPollingConsumer.createMethod(HttpPollingConsumer.java:110)
        at org.apache.camel.component.http.HttpPollingConsumer.receiveNoWait(HttpPollingConsumer.java:60)
        at org.apache.camel.impl.DefaultScheduledPollConsumer.poll(DefaultScheduledPollConsumer.java:45)
        at org.apache.camel.impl.ScheduledPollConsumer.run(ScheduledPollConsumer.java:98)
        at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:417)
        at java.util.concurrent.FutureTask$Sync.innerRunAndReset(FutureTask.java:280)
        at java.util.concurrent.FutureTask.runAndReset(FutureTask.java:135)
        at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$101(ScheduledThreadPoolExecutor.java:65)
        at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.runPeriodic(ScheduledThreadPoolExecutor.java:142)
        at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:166)
        at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:650)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:675)
        at java.lang.Thread.run(Thread.java:595)

My web.xml has:

       
        <context-param>
                <param-name>contextConfigLocation</param-name>
                <param-value>classpath:camel-config.xml</param-value>
        </context-param>

       
        <listener>
                <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
        </listener>
       
        <servlet>
                <servlet-name>CamelServlet</servlet-name>
                <servlet-class>
                        org.apache.camel.component.servlet.CamelHttpTransportServlet
                </servlet-class>
                <init-param>
                        <param-name>matchOnUriPrefix</param-name>
                        <param-value>true</param-value>
                </init-param>
        </servlet>
       
        <servlet-mapping>
                <servlet-name>CamelServlet</servlet-name>
                <url-pattern>/services/*</url-pattern>
        </servlet-mapping>

The camel-context.xml has:

   
        <camelContext id="camel" xmlns="http://camel.apache.org/schema/spring">
                <package>package.has.routebuilderl</package>
        </camelContext>
        <bean id="servlet" class="org.apache.camel.component.http.HttpComponent">
                <property name="camelContext" ref="camel" />
        </bean>

The RouteBuilder is exactly what lists in the web page.

from("servlet:///hello").process(new Processor() {
    public void process(Exchange exchange) throws Exception {
        String contentType = exchange.getIn().getHeader(Exchange.CONTENT_TYPE, String.class);
        String path = exchange.getIn().getHeader(Exchange.HTTP_PATH, String.class);
        assertEquals("Get a wrong content type", CONTENT_TYPE, contentType);
        String charsetEncoding = exchange.getIn().getHeader(Exchange.HTTP_CHARACTER_ENCODING, String.class);
        assertEquals("Get a wrong charset name", "UTF-8", charsetEncoding);
        exchange.getOut().setHeader(Exchange.CONTENT_TYPE, contentType + "; charset=UTF-8");                        
        exchange.getOut().setHeader("PATH", path);
        exchange.getOut().setBody("Hello World");
    }
});



Reply | Threaded
Open this post in threaded view
|

Re: How to create a Servlet component?

janylj

janylj wrote
The camel-context.xml has:

   
        <camelContext id="camel" xmlns="http://camel.apache.org/schema/spring">
                <package>package.has.routebuilderl</package>
        </camelContext>
        <bean id="servlet" class="org.apache.camel.component.http.HttpComponent">
                <property name="camelContext" ref="camel" />
        </bean>
I figure that I should not have a HttpComponent for servlet scheme because HttpComponent is only for output data. Then what component I should set for servlet. If I delete HttpComponent part, Tomat has error.

SEVERE: Context initialization failed
org.apache.camel.ResolveEndpointFailedException: Failed to resolve endpoint: servlet:///hello due to: No component found with scheme: servlet
        at org.apache.camel.impl.DefaultCamelContext.getEndpoint(DefaultCamelContext.java:411)
Reply | Threaded
Open this post in threaded view
|

Re: How to create a Servlet component?

Willem.Jiang
Administrator
Hi,

You need to include the camel-servlet jar which is not a part of
camel-http module and the camel-servlet component is a new component for
camel 2.0 M3.

I will update the wiki page for it.

Willem

janylj wrote:

>
>
> janylj wrote:
>>
>> The camel-context.xml has:
>>
>>     <!-- create a camel context as to start Camel -->
>> <camelContext id="camel" xmlns="http://camel.apache.org/schema/spring">
>> <package>package.has.routebuilderl</package>
>> </camelContext>
>> <bean id="servlet" class="org.apache.camel.component.http.HttpComponent">
>> <property name="camelContext" ref="camel" />
>> </bean>
>>
>>
>
> I figure that I should not have a HttpComponent for servlet scheme because
> HttpComponent is only for output data. Then what component I should set for
> servlet. If I delete HttpComponent part, Tomat has error.
>
> SEVERE: Context initialization failed
> org.apache.camel.ResolveEndpointFailedException: Failed to resolve endpoint:
> servlet:///hello due to: No component found with scheme: servlet
> at
> org.apache.camel.impl.DefaultCamelContext.getEndpoint(DefaultCamelContext.java:411)
>

Reply | Threaded
Open this post in threaded view
|

Re: How to create a Servlet component?

janylj
I add camel-servlet to pom.xml and in the camel-context.xml, I added:

        <bean id="servlet" class="org.apache.camel.component.servlet.ServletComponent">
                <property name="camelContext" ref="camel" />
        </bean>

However, I still get the following error when starting Tomcat.

SEVERE: Context initialization failed
org.apache.camel.RuntimeCamelException: java.lang.IllegalArgumentException: Can't find the deployied servlet, please set the ServletComponent with it or delopy a CamelHttpTransportServlet int the web container
        at org.apache.camel.util.ObjectHelper.wrapRuntimeCamelException(ObjectHelper.java:1011)
        at org.apache.camel.spring.SpringCamelContext.onApplicationEvent(SpringCamelContext.java:121)
        at org.apache.camel.spring.CamelContextFactoryBean.onApplicationEvent(CamelContextFactoryBean.java:461)
        at org.springframework.context.event.SimpleApplicationEventMulticaster$1.run(SimpleApplicationEventMulticaster.java:78)
        at org.springframework.core.task.SyncTaskExecutor.execute(SyncTaskExecutor.java:49)
        at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:76)
        at org.springframework.context.support.AbstractApplicationContext.publishEvent(AbstractApplicationContext.java:275)
        at org.springframework.context.support.AbstractApplicationContext.finishRefresh(AbstractApplicationContext.java:737)
        at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:384)
        at org.springframework.web.context.ContextLoader.createWebApplicationContext(ContextLoader.java:255)
        at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:199)
        at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:45)
        at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:3843)
        at org.apache.catalina.core.StandardContext.start(StandardContext.java:4350)
        at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1045)
        at org.apache.catalina.core.StandardHost.start(StandardHost.java:719)
        at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1045)
        at org.apache.catalina.core.StandardEngine.start(StandardEngine.java:443)
        at org.apache.catalina.core.StandardService.start(StandardService.java:516)
        at org.apache.catalina.core.StandardServer.start(StandardServer.java:710)
        at org.apache.catalina.startup.Catalina.start(Catalina.java:578)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
        at java.lang.reflect.Method.invoke(Method.java:585)
        at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:288)
        at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:413)
willem.jiang wrote
Hi,

You need to include the camel-servlet jar which is not a part of
camel-http module and the camel-servlet component is a new component for
camel 2.0 M3.

I will update the wiki page for it.

Willem

janylj wrote:
>
>
> janylj wrote:
>>
>> The camel-context.xml has:
>>
>>    
>> <camelContext id="camel" xmlns="http://camel.apache.org/schema/spring">
>> <package>package.has.routebuilderl</package>
>> </camelContext>
>> <bean id="servlet" class="org.apache.camel.component.http.HttpComponent">
>> <property name="camelContext" ref="camel" />
>> </bean>
>>
>>
>
> I figure that I should not have a HttpComponent for servlet scheme because
> HttpComponent is only for output data. Then what component I should set for
> servlet. If I delete HttpComponent part, Tomat has error.
>
> SEVERE: Context initialization failed
> org.apache.camel.ResolveEndpointFailedException: Failed to resolve endpoint:
> servlet:///hello due to: No component found with scheme: servlet
> at
> org.apache.camel.impl.DefaultCamelContext.getEndpoint(DefaultCamelContext.java:411)
>
Reply | Threaded
Open this post in threaded view
|

Re: How to create a Servlet component?

Willem.Jiang
Administrator
Hi,

You don't need to specify the servlet component in spring, if there is
only one camel servlet created in the web context.

If you create the servlet component, you need to specify the servlet
which the component will attach to.
<bean id="servlet"
class="org.apache.camel.component.servlet.ServletComponent">
  <property name="camelContext" ref="camel" />
         <property name="servletName" value="CamelServlet" />
</bean>

Web.xml

<web-app>

   <servlet>
     <servlet-name>CamelServlet</servlet-name>
     <display-name>Camel Http Transport Servlet</display-name>
     <servlet-class>
         org.apache.camel.component.servlet.CamelHttpTransportServlet
     </servlet-class>
     <init-param>
       <param-name>matchOnUriPrefix</param-name>
       <param-value>true</param-value>
     </init-param>
   </servlet>

   <servlet-mapping>
     <servlet-name>CamelServlet</servlet-name>
     <url-pattern>/services/*</url-pattern>
   </servlet-mapping>

</web-app>

Willem
janylj wrote:

> I add camel-servlet to pom.xml and in the camel-context.xml, I added:
>
> <bean id="servlet"
> class="org.apache.camel.component.servlet.ServletComponent">
> <property name="camelContext" ref="camel" />
> </bean>
>
> However, I still get the following error when starting Tomcat.
>
> SEVERE: Context initialization failed
> org.apache.camel.RuntimeCamelException: java.lang.IllegalArgumentException:
> Can't find the deployied servlet, please set the ServletComponent with it or
> delopy a CamelHttpTransportServlet int the web container
> at
> org.apache.camel.util.ObjectHelper.wrapRuntimeCamelException(ObjectHelper.java:1011)
> at
> org.apache.camel.spring.SpringCamelContext.onApplicationEvent(SpringCamelContext.java:121)
> at
> org.apache.camel.spring.CamelContextFactoryBean.onApplicationEvent(CamelContextFactoryBean.java:461)
> at
> org.springframework.context.event.SimpleApplicationEventMulticaster$1.run(SimpleApplicationEventMulticaster.java:78)
> at
> org.springframework.core.task.SyncTaskExecutor.execute(SyncTaskExecutor.java:49)
> at
> org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:76)
> at
> org.springframework.context.support.AbstractApplicationContext.publishEvent(AbstractApplicationContext.java:275)
> at
> org.springframework.context.support.AbstractApplicationContext.finishRefresh(AbstractApplicationContext.java:737)
> at
> org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:384)
> at
> org.springframework.web.context.ContextLoader.createWebApplicationContext(ContextLoader.java:255)
> at
> org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:199)
> at
> org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:45)
> at
> org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:3843)
> at
> org.apache.catalina.core.StandardContext.start(StandardContext.java:4350)
> at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1045)
> at org.apache.catalina.core.StandardHost.start(StandardHost.java:719)
> at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1045)
> at org.apache.catalina.core.StandardEngine.start(StandardEngine.java:443)
> at org.apache.catalina.core.StandardService.start(StandardService.java:516)
> at org.apache.catalina.core.StandardServer.start(StandardServer.java:710)
> at org.apache.catalina.startup.Catalina.start(Catalina.java:578)
> at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
> at
> sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
> at
> sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
> at java.lang.reflect.Method.invoke(Method.java:585)
> at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:288)
> at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:413)
>
> willem.jiang wrote:
>> Hi,
>>
>> You need to include the camel-servlet jar which is not a part of
>> camel-http module and the camel-servlet component is a new component for
>> camel 2.0 M3.
>>
>> I will update the wiki page for it.
>>
>> Willem
>>
>> janylj wrote:
>>>
>>> janylj wrote:
>>>> The camel-context.xml has:
>>>>
>>>>     <!-- create a camel context as to start Camel -->
>>>> <camelContext id="camel" xmlns="http://camel.apache.org/schema/spring">
>>>> <package>package.has.routebuilderl</package>
>>>> </camelContext>
>>>> <bean id="servlet"
>>>> class="org.apache.camel.component.http.HttpComponent">
>>>> <property name="camelContext" ref="camel" />
>>>> </bean>
>>>>
>>>>
>>> I figure that I should not have a HttpComponent for servlet scheme
>>> because
>>> HttpComponent is only for output data. Then what component I should set
>>> for
>>> servlet. If I delete HttpComponent part, Tomat has error.
>>>
>>> SEVERE: Context initialization failed
>>> org.apache.camel.ResolveEndpointFailedException: Failed to resolve
>>> endpoint:
>>> servlet:///hello due to: No component found with scheme: servlet
>>> at
>>> org.apache.camel.impl.DefaultCamelContext.getEndpoint(DefaultCamelContext.java:411)
>>>
>>
>>
>

Reply | Threaded
Open this post in threaded view
|

Re: How to create a Servlet component?

janylj
willem.jiang wrote
If you create the servlet component, you need to specify the servlet
which the component will attach to.
<bean id="servlet"
class="org.apache.camel.component.servlet.ServletComponent">
  <property name="camelContext" ref="camel" />
         <property name="servletName" value="CamelServlet" />
</bean>
doesn't work, because Tomcat starts error said,

Caused by: org.springframework.beans.InvalidPropertyException: Invalid property 'servletName' of bean class [org.apache.camel.component.servlet.ServletComponent]: No property 'servletName' found

willem.jiang wrote
You don't need to specify the servlet component in spring, if there is
only one camel servlet created in the web context.
doesn't work, because Tomcat starts error said,

Caused by: java.lang.IllegalArgumentException: Can't find the deployied servlet, please set the ServletComponent with it or delopy a CamelHttpTransportServlet int the web container

What I am missing? The problem seems to be the Spring ContextLoaderListener is loaded first, then CamelHttpTransportServlet is loaded when Tomcat starts. So when ContextLoaderListener set up RouteBuilder, there is no Camel Servlet yet. Any advise? Thanks a lot.

my web.xml,

       
        <context-param>
                <param-name>contextConfigLocation</param-name>
                <param-value>classpath:camel-config.xml</param-value>
        </context-param>

       
        <listener>
                <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
        </listener>
       
        <servlet>
                <servlet-name>CamelServlet</servlet-name>
                <servlet-class>
                        org.apache.camel.component.servlet.CamelHttpTransportServlet
                </servlet-class>
                <init-param>
                        <param-name>matchOnUriPrefix</param-name>
                        <param-value>true</param-value>
                </init-param>
                <load-on-startup>1</load-on-startup>
        </servlet>
       
        <servlet-mapping>
                <servlet-name>CamelServlet</servlet-name>
                <url-pattern>/services/*</url-pattern>
        </servlet-mapping>


Reply | Threaded
Open this post in threaded view
|

Re: How to create a Servlet component?

Willem.Jiang
Administrator
Hi,

Can you show me the camel-config.xml?
A simple demo help me debug the issue.

Willem

janylj wrote:

>
> willem.jiang wrote:
>>
>> If you create the servlet component, you need to specify the servlet
>> which the component will attach to.
>> <bean id="servlet"
>> class="org.apache.camel.component.servlet.ServletComponent">
>>   <property name="camelContext" ref="camel" />
>>          <property name="servletName" value="CamelServlet" />
>> </bean>
>>
>>
>
> doesn't work, because Tomcat starts error said,
>
> Caused by: org.springframework.beans.InvalidPropertyException: Invalid
> property 'servletName' of bean class
> [org.apache.camel.component.servlet.ServletComponent]: No property
> 'servletName' found
>
>
> willem.jiang wrote:
>>
>> You don't need to specify the servlet component in spring, if there is
>> only one camel servlet created in the web context.
>>
>>
>
> doesn't work, because Tomcat starts error said,
>
> Caused by: java.lang.IllegalArgumentException: Can't find the deployied
> servlet, please set the ServletComponent with it or delopy a
> CamelHttpTransportServlet int the web container
>
> What I am missing? Please note that I am using Spring ContextLoaderListener.
> Thanks a lot.
>
> my web.xml,
>
> <!-- location of spring xml files -->
> <context-param>
> <param-name>contextConfigLocation</param-name>
> <param-value>classpath:camel-config.xml</param-value>
> </context-param>
>
> <!-- the listener that kick-starts Spring -->
> <listener>
>
> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
> </listener>
>
> <servlet>
> <servlet-name>CamelServlet</servlet-name>
> <servlet-class>
> org.apache.camel.component.servlet.CamelHttpTransportServlet
> </servlet-class>
> <init-param>
> <param-name>matchOnUriPrefix</param-name>
> <param-value>true</param-value>
> </init-param>
> <load-on-startup>1</load-on-startup>
> </servlet>
>
> <servlet-mapping>
> <servlet-name>CamelServlet</servlet-name>
> <url-pattern>/services/*</url-pattern>
> </servlet-mapping>
>
>
>

Reply | Threaded
Open this post in threaded view
|

Re: How to create a Servlet component?

janylj
Sure.

camel-config.xml:

<beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="
            http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd
            http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
     
   
        <camelContext id="camel" xmlns="http://camel.apache.org/schema/spring">
                <package>package.contains.route.builder</package>
        </camelContext>
</beans>

web.xml:
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
        xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
        id="WebApp_ID" version="2.5">
               
        <servlet>
                <servlet-name>CamelServlet</servlet-name>
                <servlet-class>
                        org.apache.camel.component.servlet.CamelHttpTransportServlet
                </servlet-class>
                <init-param>
                        <param-name>matchOnUriPrefix</param-name>
                        <param-value>true</param-value>
                </init-param>
                <load-on-startup>1</load-on-startup>
        </servlet>
       
        <servlet-mapping>
                <servlet-name>CamelServlet</servlet-name>
                <url-pattern>/services/*</url-pattern>
        </servlet-mapping>
       
       
        <context-param>
                <param-name>contextConfigLocation</param-name>
                <param-value>classpath:camel-config.xml</param-value>
        </context-param>
   
       
        <listener>
                <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
        </listener>               
</web-app>

RouteBuilder java class:
public class ServletToJmsRouteBuilder extends RouteBuilder {

        public void configure() throws Exception {
                from("servlet:///hello?servletName=CamelServlet").process(new Processor() {

            public void process(Exchange exchange) {
                String contentType = (String) exchange.getIn().getHeader(Exchange.CONTENT_TYPE, String.class);
                String path = (String) exchange.getIn().getHeader(Exchange.HTTP_PATH, String.class);
                String charsetEncoding = (String) exchange.getIn().getHeader(Exchange.HTTP_CHARACTER_ENCODING, String.class);
                exchange.getOut().setHeader(Exchange.CONTENT_TYPE, contentType + "; charset=UTF-8");                        
                exchange.getOut().setHeader("PATH", path);
                exchange.getOut().setBody("Hello World");
            }
        });
        }
}

Please let me know if you need anything else. Thanks a lot.

willem.jiang wrote
Hi,

Can you show me the camel-config.xml?
A simple demo help me debug the issue.

Willem

janylj wrote:
>
> willem.jiang wrote:
>>
>> If you create the servlet component, you need to specify the servlet
>> which the component will attach to.
>> <bean id="servlet"
>> class="org.apache.camel.component.servlet.ServletComponent">
>>   <property name="camelContext" ref="camel" />
>>          <property name="servletName" value="CamelServlet" />
>> </bean>
>>
>>
>
> doesn't work, because Tomcat starts error said,
>
> Caused by: org.springframework.beans.InvalidPropertyException: Invalid
> property 'servletName' of bean class
> [org.apache.camel.component.servlet.ServletComponent]: No property
> 'servletName' found
>
>
> willem.jiang wrote:
>>
>> You don't need to specify the servlet component in spring, if there is
>> only one camel servlet created in the web context.
>>
>>
>
> doesn't work, because Tomcat starts error said,
>
> Caused by: java.lang.IllegalArgumentException: Can't find the deployied
> servlet, please set the ServletComponent with it or delopy a
> CamelHttpTransportServlet int the web container
>
> What I am missing? Please note that I am using Spring ContextLoaderListener.
> Thanks a lot.
>
> my web.xml,
>
>
> <context-param>
> <param-name>contextConfigLocation</param-name>
> <param-value>classpath:camel-config.xml</param-value>
> </context-param>
>
>
> <listener>
>
> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
> </listener>
>
> <servlet>
> <servlet-name>CamelServlet</servlet-name>
> <servlet-class>
> org.apache.camel.component.servlet.CamelHttpTransportServlet
> </servlet-class>
> <init-param>
> <param-name>matchOnUriPrefix</param-name>
> <param-value>true</param-value>
> </init-param>
> <load-on-startup>1</load-on-startup>
> </servlet>
>
> <servlet-mapping>
> <servlet-name>CamelServlet</servlet-name>
> <url-pattern>/services/*</url-pattern>
> </servlet-mapping>
>
>
>
Reply | Threaded
Open this post in threaded view
|

Re: How to create a Servlet component?

Claus Ibsen-2
Hi

Just to inform Camel have the camel-jetty component that allows you to
expose a HTTP service and let Camel handle the request and return a
reply.
http://camel.apache.org/jetty

But you got the right source about the Camel Servlet as it was Willem
who wrote it.


On Wed, Aug 12, 2009 at 6:28 PM, janylj<[hidden email]> wrote:

>
> Sure.
>
> camel-config.xml:
>
> <beans xmlns="http://www.springframework.org/schema/beans"
>        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
>        xsi:schemaLocation="
>            http://camel.apache.org/schema/spring
> http://camel.apache.org/schema/spring/camel-spring.xsd
>            http://www.springframework.org/schema/beans
> http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
>
>    <!-- create a camel context as to start Camel -->
>        <camelContext id="camel" xmlns="http://camel.apache.org/schema/spring">
>                <package>package.contains.route.builder</package>
>        </camelContext>
> </beans>
>
> web.xml:
> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
>        xmlns="http://java.sun.com/xml/ns/javaee"
> xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
>        xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
> http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
>        id="WebApp_ID" version="2.5">
>
>        <servlet>
>                <servlet-name>CamelServlet</servlet-name>
>                <servlet-class>
>                        org.apache.camel.component.servlet.CamelHttpTransportServlet
>                </servlet-class>
>                <init-param>
>                        <param-name>matchOnUriPrefix</param-name>
>                        <param-value>true</param-value>
>                </init-param>
>                <load-on-startup>1</load-on-startup>
>        </servlet>
>
>        <servlet-mapping>
>                <servlet-name>CamelServlet</servlet-name>
>                <url-pattern>/services/*</url-pattern>
>        </servlet-mapping>
>
>        <!-- location of spring xml files -->
>        <context-param>
>                <param-name>contextConfigLocation</param-name>
>                <param-value>classpath:camel-config.xml</param-value>
>        </context-param>
>
>        <!-- the listener that kick-starts Spring -->
>        <listener>
>
> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
>        </listener>
> </web-app>
>
> RouteBuilder java class:
> public class ServletToJmsRouteBuilder extends RouteBuilder {
>
>        public void configure() throws Exception {
>                from("servlet:///hello?servletName=CamelServlet").process(new Processor()
> {
>
>            public void process(Exchange exchange) {
>                String contentType = (String)
> exchange.getIn().getHeader(Exchange.CONTENT_TYPE, String.class);
>                String path = (String)
> exchange.getIn().getHeader(Exchange.HTTP_PATH, String.class);
>                String charsetEncoding = (String)
> exchange.getIn().getHeader(Exchange.HTTP_CHARACTER_ENCODING, String.class);
>                exchange.getOut().setHeader(Exchange.CONTENT_TYPE,
> contentType + "; charset=UTF-8");
>                exchange.getOut().setHeader("PATH", path);
>                exchange.getOut().setBody("Hello World");
>            }
>        });
>        }
> }
>
> Please let me know if you need anything else. Thanks a lot.
>
>
> willem.jiang wrote:
>>
>> Hi,
>>
>> Can you show me the camel-config.xml?
>> A simple demo help me debug the issue.
>>
>> Willem
>>
>> janylj wrote:
>>>
>>> willem.jiang wrote:
>>>>
>>>> If you create the servlet component, you need to specify the servlet
>>>> which the component will attach to.
>>>> <bean id="servlet"
>>>> class="org.apache.camel.component.servlet.ServletComponent">
>>>>     <property name="camelContext" ref="camel" />
>>>>          <property name="servletName" value="CamelServlet" />
>>>> </bean>
>>>>
>>>>
>>>
>>> doesn't work, because Tomcat starts error said,
>>>
>>> Caused by: org.springframework.beans.InvalidPropertyException: Invalid
>>> property 'servletName' of bean class
>>> [org.apache.camel.component.servlet.ServletComponent]: No property
>>> 'servletName' found
>>>
>>>
>>> willem.jiang wrote:
>>>>
>>>> You don't need to specify the servlet component in spring, if there is
>>>> only one camel servlet created in the web context.
>>>>
>>>>
>>>
>>> doesn't work, because Tomcat starts error said,
>>>
>>> Caused by: java.lang.IllegalArgumentException: Can't find the deployied
>>> servlet, please set the ServletComponent with it or delopy a
>>> CamelHttpTransportServlet int the web container
>>>
>>> What I am missing? Please note that I am using Spring
>>> ContextLoaderListener.
>>> Thanks a lot.
>>>
>>> my web.xml,
>>>
>>>      <!-- location of spring xml files -->
>>>      <context-param>
>>>              <param-name>contextConfigLocation</param-name>
>>>              <param-value>classpath:camel-config.xml</param-value>
>>>      </context-param>
>>>
>>>      <!-- the listener that kick-starts Spring -->
>>>      <listener>
>>>
>>> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
>>>      </listener>
>>>
>>>      <servlet>
>>>              <servlet-name>CamelServlet</servlet-name>
>>>              <servlet-class>
>>>                      org.apache.camel.component.servlet.CamelHttpTransportServlet
>>>              </servlet-class>
>>>              <init-param>
>>>                      <param-name>matchOnUriPrefix</param-name>
>>>                      <param-value>true</param-value>
>>>              </init-param>
>>>              <load-on-startup>1</load-on-startup>
>>>      </servlet>
>>>
>>>      <servlet-mapping>
>>>              <servlet-name>CamelServlet</servlet-name>
>>>              <url-pattern>/services/*</url-pattern>
>>>      </servlet-mapping>
>>>
>>>
>>>
>>
>>
>>
>
> --
> View this message in context: http://www.nabble.com/How-to-create-a-Servlet-component--tp24909157p24940161.html
> Sent from the Camel - Users mailing list archive at Nabble.com.
>
>



--
Claus Ibsen
Apache Camel Committer

Open Source Integration: http://fusesource.com
Blog: http://davsclaus.blogspot.com/
Twitter: http://twitter.com/davsclaus
Reply | Threaded
Open this post in threaded view
|

Re: How to create a Servlet component?

Willem.Jiang
Administrator
In reply to this post by janylj
Hi,

I just wrote a simple unit test which is based on your test case, I got
the same error.
That is because the camel context is created before the servlet instance
is initialized.
So I added an init parameter in the CamelHttpTransportServlet to
consumer the spring application context configure file, and make sure
the camel context is created after servlet initialization.

You can find the example here.
http://svn.apache.org/viewvc?rev=803805&view=rev

Willem

janylj wrote:

> Sure.
>
> camel-config.xml:
>
> <beans xmlns="http://www.springframework.org/schema/beans"
> xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
> xsi:schemaLocation="
>             http://camel.apache.org/schema/spring
> http://camel.apache.org/schema/spring/camel-spring.xsd
>             http://www.springframework.org/schema/beans
> http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
>      
>     <!-- create a camel context as to start Camel -->
> <camelContext id="camel" xmlns="http://camel.apache.org/schema/spring">
> <package>package.contains.route.builder</package>
> </camelContext>
> </beans>
>
> web.xml:
> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
> xmlns="http://java.sun.com/xml/ns/javaee"
> xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
> xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
> http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
> id="WebApp_ID" version="2.5">
>
> <servlet>
> <servlet-name>CamelServlet</servlet-name>
> <servlet-class>
> org.apache.camel.component.servlet.CamelHttpTransportServlet
> </servlet-class>
> <init-param>
> <param-name>matchOnUriPrefix</param-name>
> <param-value>true</param-value>
> </init-param>
> <load-on-startup>1</load-on-startup>
> </servlet>
>
> <servlet-mapping>
> <servlet-name>CamelServlet</servlet-name>
> <url-pattern>/services/*</url-pattern>
> </servlet-mapping>
>
> <!-- location of spring xml files -->
> <context-param>
> <param-name>contextConfigLocation</param-name>
> <param-value>classpath:camel-config.xml</param-value>
> </context-param>
>    
> <!-- the listener that kick-starts Spring -->
> <listener>
>
> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
> </listener>
> </web-app>
>
> RouteBuilder java class:
> public class ServletToJmsRouteBuilder extends RouteBuilder {
>
> public void configure() throws Exception {
> from("servlet:///hello?servletName=CamelServlet").process(new Processor()
> {
>
>             public void process(Exchange exchange) {
>                 String contentType = (String)
> exchange.getIn().getHeader(Exchange.CONTENT_TYPE, String.class);
>                 String path = (String)
> exchange.getIn().getHeader(Exchange.HTTP_PATH, String.class);
>                 String charsetEncoding = (String)
> exchange.getIn().getHeader(Exchange.HTTP_CHARACTER_ENCODING, String.class);
>                 exchange.getOut().setHeader(Exchange.CONTENT_TYPE,
> contentType + "; charset=UTF-8");                        
>                 exchange.getOut().setHeader("PATH", path);
>                 exchange.getOut().setBody("Hello World");
>             }
>         });
> }
> }
>
> Please let me know if you need anything else. Thanks a lot.
>
>
> willem.jiang wrote:
>> Hi,
>>
>> Can you show me the camel-config.xml?
>> A simple demo help me debug the issue.
>>
>> Willem
>>
>> janylj wrote:
>>> willem.jiang wrote:
>>>> If you create the servlet component, you need to specify the servlet
>>>> which the component will attach to.
>>>> <bean id="servlet"
>>>> class="org.apache.camel.component.servlet.ServletComponent">
>>>>   <property name="camelContext" ref="camel" />
>>>>          <property name="servletName" value="CamelServlet" />
>>>> </bean>
>>>>
>>>>
>>> doesn't work, because Tomcat starts error said,
>>>
>>> Caused by: org.springframework.beans.InvalidPropertyException: Invalid
>>> property 'servletName' of bean class
>>> [org.apache.camel.component.servlet.ServletComponent]: No property
>>> 'servletName' found
>>>
>>>
>>> willem.jiang wrote:
>>>> You don't need to specify the servlet component in spring, if there is
>>>> only one camel servlet created in the web context.
>>>>
>>>>
>>> doesn't work, because Tomcat starts error said,
>>>
>>> Caused by: java.lang.IllegalArgumentException: Can't find the deployied
>>> servlet, please set the ServletComponent with it or delopy a
>>> CamelHttpTransportServlet int the web container
>>>
>>> What I am missing? Please note that I am using Spring
>>> ContextLoaderListener.
>>> Thanks a lot.
>>>
>>> my web.xml,
>>>
>>> <!-- location of spring xml files -->
>>> <context-param>
>>> <param-name>contextConfigLocation</param-name>
>>> <param-value>classpath:camel-config.xml</param-value>
>>> </context-param>
>>>
>>> <!-- the listener that kick-starts Spring -->
>>> <listener>
>>>
>>> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
>>> </listener>
>>>
>>> <servlet>
>>> <servlet-name>CamelServlet</servlet-name>
>>> <servlet-class>
>>> org.apache.camel.component.servlet.CamelHttpTransportServlet
>>> </servlet-class>
>>> <init-param>
>>> <param-name>matchOnUriPrefix</param-name>
>>> <param-value>true</param-value>
>>> </init-param>
>>> <load-on-startup>1</load-on-startup>
>>> </servlet>
>>>
>>> <servlet-mapping>
>>> <servlet-name>CamelServlet</servlet-name>
>>> <url-pattern>/services/*</url-pattern>
>>> </servlet-mapping>
>>>
>>>
>>>
>>
>>
>