You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@camel.apache.org by leon <it...@gmail.com> on 2016/05/05 16:13:40 UTC

How to use cdi annotation to reference shared netty server in karaf?

Hi guys,

I just followed this example
(http://camel.apache.org/netty-http-server-example.html) and successfully
deployed a shared netty http server to apache karaf. But I could not figure
out how to reference this shared netty http server in my Rest Java DSL route
file. Here is my route file:

import javax.inject.Inject;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.cdi.ContextName;
import org.apache.camel.component.netty4.http.NettySharedHttpServer;
import org.ops4j.pax.cdi.api.OsgiService;

@ContextName("sample")
public class SampleRoute extends RouteBuilder {

  @Inject @OsgiService
  private NettySharedHttpServer server;

  @Override
  public void configure() {
   
restConfiguration().component("netty4-http").endpointProperty("nettySharedHttpServer",
"#WHAT_IS_THIS_ID");
    rest("/say").get("/hello").to("mock:hello");
  }

}

I wonder to know if I inject netty shared http server into my route file
like above sample code instead of using OSGi blueprint, what is the id of
netty shared http server? Must I use blueprint xml configuration file? Could
someone shed some light on this please? Thanks in advance!

Leon



--
View this message in context: http://camel.465427.n5.nabble.com/How-to-use-cdi-annotation-to-reference-shared-netty-server-in-karaf-tp5782250.html
Sent from the Camel - Users mailing list archive at Nabble.com.

Re: How to use cdi annotation to reference shared netty server in karaf?

Posted by leon <it...@gmail.com>.
Hi Antonin,

I just saw your response and tried it right away. And it worked like a
charm. Thanks for your help!!!

Leon



--
View this message in context: http://camel.465427.n5.nabble.com/How-to-use-cdi-annotation-to-reference-shared-netty-server-in-karaf-tp5782250p5782412.html
Sent from the Camel - Users mailing list archive at Nabble.com.

Re: How to use cdi annotation to reference shared netty server in karaf?

Posted by Antonin Stefanutti <an...@stefanutti.fr>.
Hi Leon,

In order to get the NettySharedHttpServer service exposed as a named bean in your Java DSL bundle, you could do:

@ContextName("sample") 
public class SampleRoute extends RouteBuilder { 

  @Inject
  @OsgiService
  private NettySharedHttpServer server;

  @Produces
  @Named("sharedNettyHttpServer")
  private NettySharedHttpServer server() {
    return server;
  }

  @Override 
  public void configure() { 
    restConfiguration().component("netty4-http").endpointProperty("nettySharedHttpServer", "#sharedNettyHttpServer"); 
    rest("/say").get("/hello").to("mock:hello"); 
  }
}

That’d be the equivalent of the declaration in blueprint hereafter:

 <reference id=“sharedNettyHttpServer” interface="org.apache.camel.component.netty.http.NettySharedHttpServer”/>

I’ve updated the Netty HTTP example accordingly:

https://github.com/apache/camel/blob/62ac1a9ef9c8f61754de2450303023ee165edabf/examples/camel-example-netty-http/myapp-cdi/src/main/java/org/apache/camel/example/netty/cdi/NettyHttpRoute.java#L31-L39

And tested it successfully with Camel 2.17.0 and Karaf 2.4.4.

Let us know if you need further assistance.

Antonin

> On 05 May 2016, at 18:13, leon <it...@gmail.com> wrote:
> 
> Hi guys,
> 
> I just followed this example
> (http://camel.apache.org/netty-http-server-example.html) and successfully
> deployed a shared netty http server to apache karaf. But I could not figure
> out how to reference this shared netty http server in my Rest Java DSL route
> file. Here is my route file:
> 
> import javax.inject.Inject;
> import org.apache.camel.builder.RouteBuilder;
> import org.apache.camel.cdi.ContextName;
> import org.apache.camel.component.netty4.http.NettySharedHttpServer;
> import org.ops4j.pax.cdi.api.OsgiService;
> 
> @ContextName("sample")
> public class SampleRoute extends RouteBuilder {
> 
>  @Inject @OsgiService
>  private NettySharedHttpServer server;
> 
>  @Override
>  public void configure() {
> 
> restConfiguration().component("netty4-http").endpointProperty("nettySharedHttpServer",
> "#WHAT_IS_THIS_ID");
>    rest("/say").get("/hello").to("mock:hello");
>  }
> 
> }
> 
> I wonder to know if I inject netty shared http server into my route file
> like above sample code instead of using OSGi blueprint, what is the id of
> netty shared http server? Must I use blueprint xml configuration file? Could
> someone shed some light on this please? Thanks in advance!
> 
> Leon
> 
> 
> 
> --
> View this message in context: http://camel.465427.n5.nabble.com/How-to-use-cdi-annotation-to-reference-shared-netty-server-in-karaf-tp5782250.html
> Sent from the Camel - Users mailing list archive at Nabble.com.