You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@camel.apache.org by "per.ullberg" <pe...@klarna.com> on 2012/11/01 08:33:33 UTC

WS with Camel CXF Jetty Guice - No Spring!

Hi, 

I have an ESB based on Camel, Guice and Jetty. We're not doing any Spring at
the moment and would like to save that can of worms for another day.

But, Now I need to add an inbound CXF endpoint. All examples I can find is
with Spring. Could someone point me to a good example or write one up for
me?

This is basically what we're doing currently:

The very simple Route:
*******************************************************************************

package com....

import org.apache.camel.builder.RouteBuilder;

import javax.jws.WebService;
import java.util.Properties;

public class WSRouteBuilder extends RouteBuilder {

    @Override
    public void configure() throws Exception {
        from("cxf:/eventListener?setDefaultBus=true&serviceClass=" +
Service.class.getName()).to("log:POST");
    }
}


*******************************************************************************

The Servlet handle:
*******************************************************************************
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import java.util.Properties;

public class ApplicationServlet extends ApplicationBase implements
ServletContextListener {

...

    @Override
    public void contextInitialized(ServletContextEvent servletContextEvent)
{
        try {
            start();
            servletContextEvent.getServletContext().setAttribute("injector",
injector);
        } catch (Exception e) {
            LOG.error(e.getMessage(), e);
            throw new RuntimeException(e.getMessage(), e);
        }
    }
...
}

*******************************************************************************

As you see, I'm adding the guice injector to the servlet context. The guice
injector will provide the started CamelContext as a singleton. The
CamelContext contains the route. And then, the web.xml:

********************************************************************************
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
         version="2.5">

    <session-config>
        <session-timeout>30</session-timeout>
    </session-config>

    <listener>
        <listener-class>com....ApplicationServlet</listener-class>
    </listener>

    
    <servlet>
        <servlet-name>MyCXFServlet</servlet-name>
        <servlet-class>com....MyCXFServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>

    
    <servlet-mapping>
        <servlet-name>MyCXFServlet</servlet-name>
        <url-pattern>/webservices/*</url-pattern>
    </servlet-mapping>
</web-app>

**************************************************************************************

And finally extending the CXFNonSpringServlet as I could find in some
tutorials... But how do I create the bus and wire it properly???: 

**************************************************************************************
package com...

import com.google.inject.Inject;
import com.google.inject.Injector;
import org.apache.camel.CamelContext;
import org.apache.camel.component.cxf.CxfEndpointUtils;
import org.apache.camel.component.cxf.transport.CamelTransportFactory;
import org.apache.cxf.Bus;
import org.apache.cxf.bus.CXFBusFactory;
import org.apache.cxf.bus.CXFBusImpl;
import org.apache.cxf.transport.servlet.CXFNonSpringServlet;

import javax.servlet.ServletConfig;

public class MyCXFServlet extends CXFNonSpringServlet {

    @Inject
    private CamelContext camelContext;

    @Override
    protected void loadBus(ServletConfig sc) {
        super.loadBus(sc);
        // Getting the injector from the context
        Injector injector = (Injector)
sc.getServletContext().getAttribute("injector");
        // Inject dependencies into this instance
        injector.injectMembers(this);

        ????????????????????
        ??????setBus()???????
        ????????????????????
    }
}

**************************************************************************************



Please help me with the setBus step!

BR
/Pelle



--
View this message in context: http://camel.465427.n5.nabble.com/WS-with-Camel-CXF-Jetty-Guice-No-Spring-tp5721940.html
Sent from the Camel - Users mailing list archive at Nabble.com.

Re: WS with Camel CXF Jetty Guice - No Spring!

Posted by "per.ullberg" <pe...@klarna.com>.
Hi, 

Thanks... It is as you say. It was me not getting the url right...

Had to do this though:

***************************************************************************
package com....;

import com.google.inject.Inject;
import com.google.inject.Injector;
import org.apache.camel.CamelContext;
import org.apache.cxf.BusFactory;
import org.apache.cxf.transport.servlet.CXFNonSpringServlet;

import javax.servlet.ServletConfig;

public class MyCXFServlet extends CXFNonSpringServlet {

    @Override
    protected void loadBus(ServletConfig sc) {
        setBus(BusFactory.getDefaultBus());
    }
}

*************************************************************************






--
View this message in context: http://camel.465427.n5.nabble.com/WS-with-Camel-CXF-Jetty-Guice-No-Spring-tp5721940p5721982.html
Sent from the Camel - Users mailing list archive at Nabble.com.

Re: WS with Camel CXF Jetty Guice - No Spring!

Posted by Willem jiang <wi...@gmail.com>.
Hi,

You don't need to addition work to setup the bus as CXF bus can live without Spring.
If you need to setup the bus, you may consider to call some CXF Java API to setup the features etc.

-- 
Willem Jiang

Red Hat, Inc.
FuseSource is now part of Red Hat
Web: http://www.fusesource.com | http://www.redhat.com
Blog: http://willemjiang.blogspot.com (http://willemjiang.blogspot.com/) (English)
          http://jnn.javaeye.com (http://jnn.javaeye.com/) (Chinese)
Twitter: willemjiang 
Weibo: willemjiang





On Thursday, November 1, 2012 at 3:33 PM, per.ullberg wrote:

> Hi, 
> 
> I have an ESB based on Camel, Guice and Jetty. We're not doing any Spring at
> the moment and would like to save that can of worms for another day.
> 
> But, Now I need to add an inbound CXF endpoint. All examples I can find is
> with Spring. Could someone point me to a good example or write one up for
> me?
> 
> This is basically what we're doing currently:
> 
> The very simple Route:
> *******************************************************************************
> 
> package com....
> 
> import org.apache.camel.builder.RouteBuilder;
> 
> import javax.jws.WebService;
> import java.util.Properties;
> 
> public class WSRouteBuilder extends RouteBuilder {
> 
> @Override
> public void configure() throws Exception {
> from("cxf:/eventListener?setDefaultBus=true&serviceClass=" +
> Service.class.getName()).to("log:POST");
> }
> }
> 
> 
> *******************************************************************************
> 
> The Servlet handle:
> *******************************************************************************
> import javax.servlet.ServletContextEvent;
> import javax.servlet.ServletContextListener;
> import java.util.Properties;
> 
> public class ApplicationServlet extends ApplicationBase implements
> ServletContextListener {
> 
> ...
> 
> @Override
> public void contextInitialized(ServletContextEvent servletContextEvent)
> {
> try {
> start();
> servletContextEvent.getServletContext().setAttribute("injector",
> injector);
> } catch (Exception e) {
> LOG.error(e.getMessage(), e);
> throw new RuntimeException(e.getMessage(), e);
> }
> }
> ...
> }
> 
> *******************************************************************************
> 
> As you see, I'm adding the guice injector to the servlet context. The guice
> injector will provide the started CamelContext as a singleton. The
> CamelContext contains the route. And then, the web.xml (http://web.xml):
> 
> ********************************************************************************
> <?xml version="1.0" encoding="UTF-8"?>
> <web-app xmlns="http://java.sun.com/xml/ns/javaee"
> xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
> xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
> http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
> version="2.5">
> 
> <session-config>
> <session-timeout>30</session-timeout>
> </session-config>
> 
> <listener>
> <listener-class>com....ApplicationServlet</listener-class>
> </listener>
> 
> <servlet>
> <servlet-name>MyCXFServlet</servlet-name>
> <servlet-class>com....MyCXFServlet</servlet-class>
> <load-on-startup>1</load-on-startup>
> </servlet>
> 
> <servlet-mapping>
> <servlet-name>MyCXFServlet</servlet-name>
> <url-pattern>/webservices/*</url-pattern>
> </servlet-mapping>
> </web-app>
> 
> **************************************************************************************
> 
> And finally extending the CXFNonSpringServlet as I could find in some
> tutorials... But how do I create the bus and wire it properly???: 
> 
> **************************************************************************************
> package com...
> 
> import com.google.inject.Inject;
> import com.google.inject.Injector;
> import org.apache.camel.CamelContext;
> import org.apache.camel.component.cxf.CxfEndpointUtils;
> import org.apache.camel.component.cxf.transport.CamelTransportFactory;
> import org.apache.cxf.Bus;
> import org.apache.cxf.bus.CXFBusFactory;
> import org.apache.cxf.bus.CXFBusImpl;
> import org.apache.cxf.transport.servlet.CXFNonSpringServlet;
> 
> import javax.servlet.ServletConfig;
> 
> public class MyCXFServlet extends CXFNonSpringServlet {
> 
> @Inject
> private CamelContext camelContext;
> 
> @Override
> protected void loadBus(ServletConfig sc) {
> super.loadBus(sc);
> // Getting the injector from the context
> Injector injector = (Injector)
> sc.getServletContext().getAttribute("injector");
> // Inject dependencies into this instance
> injector.injectMembers(this);
> 
> ????????????????????
> ??????setBus()???????
> ????????????????????
> }
> }
> 
> **************************************************************************************
> 
> 
> 
> Please help me with the setBus step!
> 
> BR
> /Pelle
> 
> 
> 
> --
> View this message in context: http://camel.465427.n5.nabble.com/WS-with-Camel-CXF-Jetty-Guice-No-Spring-tp5721940.html
> Sent from the Camel - Users mailing list archive at Nabble.com (http://Nabble.com).