main website home
  • About this blog

    This blog features updates, opinions, and technical notes from Caucho engineers about Caucho products, the enterprise Java industry, and PHP. Caucho Technology is the creator of the Resin Application Server and the Quercus PHP in Java engine. A leader in Java performance since 1998, Caucho is a Sun JavaEE licensee with over 9000 customers worldwide.
  • Tags

    ajaxworld bam candi cdi cloud cluster comet configuration deploy devoxx eclipse ejb embedded flash flex google app engine hessian hmtp ioc java ee 6 javaone javazone jms messaging newsletter nyjug osgi php pomegranate quercus resin resin 4.0 REST servlet sfjug silicon valley code camp spring testing training tssjs watchdog webbeans web profile websockets wordpress
  • Meta

    • Register
    • Log in
    • Entries RSS
    • Comments RSS
    • WordPress.org
« JProfiler
Update: Newsletter, Hessian messaging, and Terracotta »

Embedded Resin… with WebBeans!

One of the coolest new features of Resin 3.1.5 is embedding. Basically with a few simple lines of code, you can embed Resin into any existing Java application. More than that though, you can take advantage of Resin IoC/WebBeans even within the embedded environment. Let me give you an example…

Let’s start with simple embedding:

package demo;

import com.caucho.resin.*;

public class EmbeddedResin {
  public static void main(String[] args)
    throws Exception
  {
    ResinEmbed resin = new ResinEmbed();
    HttpEmbed http = new HttpEmbed(8080);
    resin.addPort(http);

    resin.addWebApp(new WebAppEmbed("/", "ROOT"));
    resin.start();
    resin.join();
  }
}

This basically starts up a new Resin within this application listening to port 8080 and serving up a webapp whose root is at “ROOT”. The resin.start() call starts the instance while the resin.join() waits for it to finish. Depending on the application, you may want to separate those calls.

So that’s very cool, but now what if I have a bean that I want to inject somewhere. Let’s say this FooBean:

package demo;

public class FooBean {
  public String hello()
  {
    return "Hello from the world of Embedded Resin!";
  }
}

I can actually do annotation based injection into a JSP for example:

<%@ page import="javax.webbeans.*,demo.*" %>
<%! @In private FooBean _foo; %>

<%= _foo.hello() %>

All I have to do is add a call to resin.addBean() to the example above:

package demo;

import com.caucho.resin.*;

public class EmbeddedIoC {
  public static void main(String[] args)
    throws Exception
  {
    ResinEmbed resin = new ResinEmbed();
    HttpEmbed http = new HttpEmbed(8080);
    resin.addPort(http);

    resin.addBean(new BeanEmbed(new FooBean()));

    resin.addWebApp(new WebAppEmbed("/", "ROOT"));
    resin.start();
    resin.join();

  }
}

It’s pretty cool to me that you can drop a random bean into the embedded Resin, then just have it show up somewhere in a JSP. :-) I bundled this example up here. Just run “ant compile”, then “ant run” to use it.

Tags: embedded, ioc, resin, webbeans

This entry was posted on Friday, April 11th, 2008 at 10:12 am and is filed under Engineering. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

Leave a Reply

You must be logged in to post a comment.


Caucho Technology is proudly powered by WordPress and Quercus®
Entries (RSS) and Comments (RSS).

  • HOME |
  • CONTACT US |
  • DOCUMENTATION |
  • BLOG |
  • WIKI 4 |
  • WIKI 3 |
  • Resin: Java Application Server
Copyright (c) 1998-2012 Caucho Technology, Inc. All rights reserved.
caucho® , resin® and quercus® are registered trademarks of Caucho Technology, Inc.
resin® is a cloud optimized, java® application server that supports the java ee webprofile ®