Wednesday May 21, 2008
Embeddable GlassFish in Action - Servlet in a Maven project
Kohsuke
announced the embedability
of GlassFish v3 - this is really cool! Now you can run
GlassFish inside an existing JVM, without the need to start it
externally. The API javadocs are available here.
This blog explains how to host a Servlet using these APIs and write a
simple Maven test to invoke the Servlet - all within the same VM.
The blog creates a Maven project using NetBeans but Maven CLI can be
used as well.
In the NetBeans IDE,
if Maven plugin is not already installed, then install it using
"Tools", "Plugins","Available Plugins".




| mvn archetype:create -DarchetypeGroupId=org.apache.maven.archetypes -DgroupId=org.glassfish.embedded.samples -DartifactId=webtier |
| <repositories> <repository> <id>glassfish-repository</id> <name>Java.net Repository for Glassfish</name> <url>http://download.java.net/maven/glassfish</url> </repository> <repository> <id>download.java.net</id> <name>Java.net Maven Repository</name> <url>http://download.java.net/maven/2</url> </repository> </repositories> |
| <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>2.0.2</version> <configuration> <source>1.5</source> <target>1.5</target> </configuration> </plugin> </plugins> </build> |
| <dependency> <groupId>org.glassfish.distributions</groupId> <artifactId>web-all</artifactId> <version>10.0-build-20080430</version> </dependency> <dependency> <groupId>org.glassfish.embedded</groupId> <artifactId>gf-embedded-api</artifactId> <version>1.0-alpha-4</version> </dependency> |

| package
org.glassfish.embedded.samples.webtier; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * @author Arun Gupta */ public class SimpleServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { PrintWriter out = response.getWriter(); out.println("Wow, I'm embedded!"); } } |




| <?xml
version="1.0" encoding="UTF-8"?> <web-app version="2.5" 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"> <servlet> <servlet-name>SimpleServlet</servlet-name> <servlet-class>org.glassfish.embedded.samples.webtier.SimpleServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>SimpleServlet</servlet-name> <url-pattern>/SimpleServlet</url-pattern> </servlet-mapping> </web-app> |

|
private final String NAME = "AppTest"; public void testServlet() throws Exception { int port = 9999; GlassFish glassfish = newGlassFish(port); URL url = new URL("http://localhost:" + port + "/" + NAME + "/SimpleServlet"); BufferedReader br = new BufferedReader( new InputStreamReader( url.openConnection().getInputStream())); assertEquals("Wow, I'm embedded!", br.readLine()); glassfish.stop(); } private GlassFish newGlassFish(int port) throws Exception { GlassFish glassfish = new GlassFish(port); ScatteredWar war = new ScatteredWar(NAME, new File("src/main/resources"), new File("src/main/resources/WEB-INF/web.xml"), Collections.singleton(new File("target/classes").toURI().toURL())); glassfish.deploy(war); System.out.println("Ready ..."); return glassfish; } |





Posted by Arun Gupta in web2.0 | Comments[20]
|
|
|
|
|
Today's Page Hits: 69
Total # blog entries: 994
| « November 2009 | ||||||
| Sun | Mon | Tue | Wed | Thu | Fri | Sat |
|---|---|---|---|---|---|---|
1 | 2 | 4 | 6 | 7 | ||
8 | 9 | 10 | 11 | 12 | 13 | 14 |
15 | 16 | 17 | 18 | 19 | 20 | 21 |
22 | 23 | 24 | 25 | 26 | 27 | 28 |
29 | 30 | |||||
| Today | ||||||
Wow! This is kinda cool!
Posted by Jason on May 23, 2008 at 11:10 AM PDT #
Posted by Arun Gupta's Blog on June 26, 2008 at 06:17 AM PDT #
Really cool! Do you by chance know how to hook up a datasource to an embedded glassfish instance?
Posted by Justin Spradlin on June 26, 2008 at 12:50 PM PDT #
Posted by Arun Gupta's Blog on July 18, 2008 at 07:11 AM PDT #
Posted by Arun Gupta's Blog on August 13, 2008 at 06:23 AM PDT #
great post- online forex can use it
Posted by Forex on October 10, 2008 at 07:34 AM PDT #
2 Justin Spradlin. Yes really cool post
Posted by scommesse sportive on October 25, 2008 at 12:39 PM PDT #
Goog article. thk
Posted by forex forum on October 25, 2008 at 12:44 PM PDT #
Interesting. I tried the whole demo. Everything is working fine except my start up is longer than yours. I want to know if I can run the SimpleServlet as well. Let me know if I am not supposed to run the SimpleServlet directly. I tried to run it from NetBeans and here is the msg I got, any ideas. -Thanks! Doris
Scanning for projects...
project-execute
[#process-resources]
[resources:resources]
Using default encoding to copy filtered resources.
[#compile]
[compiler:compile]
Nothing to compile - all classes are up to date
[exec:exec]
Exception in thread "main" java.lang.NoSuchMethodError: main
[ERROR]The following mojo encountered an error while executing:
[ERROR]Group-Id: org.codehaus.mojo
[ERROR]Artifact-Id: exec-maven-plugin
[ERROR]Version: 1.1
[ERROR]Mojo: exec
[ERROR]brought in via: Direct invocation
[ERROR]While building project:
[ERROR]Group-Id: org.glassfish.embedded.samples
[ERROR]Artifact-Id: webtier
[ERROR]Version: 1.0-SNAPSHOT
[ERROR]From file: /Users/dorischen/NetBeansProjects/webtier/pom.xml
[ERROR]Reason: Result of /bin/sh -c cd /Users/dorischen/NetBeansProjects/webtier && java -classpath /Users/dorischen/NetBeansProjects/webtier/target/classes:/Users/dorischen/.m2/repository/org/glassfish/distributions/web-all/10.0-build-20080430/web-all-10.0-build-20080430.jar:/Users/dorischen/.m2/repository/org/glassfish/embedded/gf-embedded-api/1.0-alpha-4/gf-embedded-api-1.0-alpha-4.jar:/Users/dorischen/.m2/repository/org/glassfish/api/dtds/9.0.2/dtds-9.0.2-resources.jar:/Users/dorischen/.m2/repository/org/glassfish/api/schemas/9.0.2/schemas-9.0.2-resources.jar org.glassfish.embedded.samples.webtier.SimpleServlet execution is: '1'.
------------------------------------------------------------------------
For more information, run with the -e flag
------------------------------------------------------------------------
BUILD FAILED
------------------------------------------------------------------------
Total time: 1 second
Finished at: Fri Oct 31 12:29:21 PDT 2008
Final Memory: 63M/146M
------------------------------------------------------------------------
Posted by Doris on October 31, 2008 at 12:34 PM PDT #
Doris,
I tried the code listed above again and v3 started in 718 ms. It could be because of a different machine configuration. How much time is it taking on your machine ?
SimpleServlet is run and invoked from testServlet in AppTest. If you want to show the output form SimpleServlet in a browser, then you need to add sleep() before glassfish.stop() in testServlet().
Posted by Arun Gupta on November 04, 2008 at 10:38 AM PST #
Posted by Arun Gupta's Blog on November 06, 2008 at 06:36 AM PST #
Posted by Arun Gupta's Blog on November 11, 2008 at 04:32 AM PST #
Posted by Arun Gupta's Blog on November 19, 2008 at 05:41 AM PST #
after adding SimpleServlet.java netbeans says package javax.servlet doesnot exists
did i missed something or something needs to be configured?
Posted by jim on March 02, 2009 at 07:36 PM PST #
SimpleServlet.java netbeans says package javax.servlet doesnot exists...
Posted by antalya escort on March 08, 2009 at 02:26 PM PDT #
jim, antalya,
You may have to invoke mvn from CLI first so that it pulls the relevant binaries in your local repository. Then the class is resolved correctly.
Posted by Arun Gupta on March 27, 2009 at 03:14 PM PDT #
wow gold
Posted by wow gold on October 06, 2009 at 05:09 PM PDT #
Aion kinah
Posted by Aion kinah on October 06, 2009 at 05:10 PM PDT #
metin2 yang
Posted by metin2 yang on October 06, 2009 at 05:12 PM PDT #
<a href="http://www.rajayj.cn/product.htm">paper cup machine</a> <a href="http://www.lszwjx.cn">气动马达</a> <a href="http://www.lszwjx.cn">气动搅拌机</a> <a href="http://www.lszwjx.cn/proall_list.asp">油漆搅拌机</a>
Posted by ccc on October 18, 2009 at 11:18 PM PDT #