1 2 Previous Next 15 Replies Latest reply on Nov 2, 2010 1:39 PM by rmaucher

    AS 7 demos

    kabirkhan

      I have started a demos project, which can be found in upstream.

       

      It currently contains the following examples

      -sar

      -managedbean

      -serviceloader

      -messaging

       

      To run an example, make sure AS7 is running and then

      cd demos

      mvn install -Dexample=sar

       

      A few issues I came across when working on this are:

       

      Deployment

       

      -Deployment to domain/deployments does not work, have to do standalone
      -Deleting .deployed archive still leaves it hanging around in standalone.xml. To undeploy need to delete from standalone.xml's deployments
      -.DS_Store gets picked up as a deployment
      -Slightly annoying having to specify the full module path in user deployments, not very user friendly? Will get worse when there are more modules.
      -BeanWithSimpleInjected.simple is sometimes null

      -ServiceLoader example no longer picks up services

       

      -I am currently just copying to the deployments/ directory, what is the status on the deployment API?

      -Deployment to domain/deployments does not work, have to copy to standalone/deployments

      -Deleting .deployed archive still leaves it hanging around in standalone.xml. To undeploy need to delete from standalone.xml's deployments

      -.DS_Store (on OS X) gets picked up as a deployment

       

      Module Dependencies

      -It is slightly annoying having to specify the full module path in user deployments, not very user friendly? This will get worse when there are more modules in the server.

       

      Examples

      The 'sar' and 'messaging' examples work fine, but there are some problems with the others.

       

      For the 'managedbean' example, there is

      public class BeanWithSimpleInjected {
      
          private final Logger log = Logger.getLogger(BeanWithSimpleInjected.class);
      
          @Resource
          private SimpleManagedBean simple;
      ...
      

       

      Sometimes the simple field is null, other times it is injected as expected. I added the functionality to have an @AroundInvoke method on the bean class itself.

       

      The 'serviceloader' example used to work fine in my development branch but following a rebase against upstream the service loader no longer finds the services.

        • 1. Re: AS 7 demos
          kabirkhan

          For the record, the way I am specifying the modules dependencies via the deployment's MANIFEST.MF, e.g. managedbean-example.sar:

           

          Manifest-Version: 1.0

          Dependencies: system,org.jboss.logging,javax.annotation.api,javax.interceptor.api,org.jboss.as.managed-beans,org.javassist

          • 2. Re: AS 7 demos
            brian.stansberry

            The managed bean issued is the one discussed in my third post on http://community.jboss.org/thread/157565?tstart=0 -- JIRA for that is https://jira.jboss.org/browse/JBAS-8518

             

            The remotable deployment API is in progress. I'm focusing on domain wide deployments. If someone has some cycles to work on making the standalone deployment API remotable, a lot of pieces are there to serve as a base; John Bailey and I could walk you through it. Otherwise I'll look at it Thursday.

             

            The domain/deployments dir is not a hot deployment directory. We should probably rename it. It's the equivalent to standalone/data/deployments. There is no filesystem based hot deploy to the domain.

             

            Can you file a JIRA on the .DS_Store problem? Or just fix it (have the scanner ignore it)?

             

            I haven't been able to reproduce your issue with deleting .deployed. I haven't tried using your maven module though; just manually. Does it happen it you do things manually?

            • 3. Re: AS 7 demos
              brian.stansberry

              Actually, the managed bean issue may not be what I said; reading more carefully maybe you are seeing something different.

              • 4. Re: AS 7 demos
                dmlloyd

                Brian Stansberry wrote:

                 

                The domain/deployments dir is not a hot deployment directory. We should probably rename it. It's the equivalent to standalone/data/deployments.

                 

                How about "content"?

                • 5. Re: AS 7 demos
                  brian.stansberry

                  On the module dependencies, we've talked about setting up a "EE classpath". Would that consist of a deployer that adds a certain set of standard module dependencies to all EE deployments?

                  • 6. Re: AS 7 demos
                    brian.stansberry

                    David Lloyd wrote:

                     

                    Brian Stansberry wrote:

                     

                    The domain/deployments dir is not a hot deployment directory. We should probably rename it. It's the equivalent to standalone/data/deployments.

                     

                    How about "content"?

                     

                    Ok by me. If anyone has a better idea, speak up or I'll change it sometime this afternoon.

                    • 7. Re: AS 7 demos
                      kabirkhan

                      I could look at the remote deployment API.

                       

                      Deleting .deployed happened when copying things manually. Although every time I try to get a clear set of steps to reproduce it works the way I would hope :-) I'll fix the .DS_Store stuff first (maybe it is noise coming from there) probably just by ignoring all deployments whose name start with a '.'

                      • 8. Re: AS 7 demos
                        emuckenhuber

                        This is the filter we used for AS5/6 deployment scanning, which most likely also applies now:

                        http://anonsvn.jboss.org/repos/jbossas/projects/vfs/trunk/src/main/java/org/jboss/vfs/util/ExtensibleFilter.java

                        • 9. Re: AS 7 demos
                          kabirkhan
                          • 10. Re: AS 7 demos
                            brian.stansberry

                            That's pushed upstream.

                            • 11. Re: AS 7 demos
                              kabirkhan

                              I am sometimes getting this error when deploying the 'messaging' demo:

                               

                              17:39:44,557 ERROR [deployment] Deployment [service jboss.deployment.messaging-mbean_sar] failed to start correctly.  Completely shutting down deployment.  Please see additional errors for details.: org.jboss.msc.service.StartException in service service jboss.mbean.service.jboss:name=test,type=messaging.create: Failed to start service

                              at org.jboss.msc.service.ServiceControllerImpl$StartTask.run(ServiceControllerImpl.java:959)

                              at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886) [:1.6.0_20]

                              at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908) [:1.6.0_20]

                              at java.lang.Thread.run(Thread.java:637) [:1.6.0_20]

                              Caused by: java.lang.NoClassDefFoundError: org/hornetq/api/core/Message

                              at java.lang.Class.getDeclaredConstructors0(Native Method) [:1.6.0_20]

                              at java.lang.Class.privateGetDeclaredConstructors(Class.java:2389) [:1.6.0_20]

                              at java.lang.Class.getConstructor0(Class.java:2699) [:1.6.0_20]

                              at java.lang.Class.getConstructor(Class.java:1657) [:1.6.0_20]

                              at org.jboss.msc.value.LookupConstructorValue.getValue(LookupConstructorValue.java:63)

                              at org.jboss.msc.value.LookupConstructorValue.getValue(LookupConstructorValue.java:33)

                              at org.jboss.msc.value.CachedValue.getValue(CachedValue.java:54)

                              at org.jboss.msc.value.ConstructedValue.getValue(ConstructedValue.java:55)

                              at org.jboss.msc.value.CachedValue.getValue(CachedValue.java:54)

                              at org.jboss.as.service.CreateDestroyService.getValue(CreateDestroyService.java:59)

                              at org.jboss.as.service.CreateDestroyService.start(CreateDestroyService.java:31)

                              at org.jboss.msc.service.ServiceControllerImpl$StartTask.run(ServiceControllerImpl.java:923)

                              ... 3 more

                              17:39:44,557 ERROR [deployment] Deployment [service jboss.deployment.messaging-mbean_sar] failed to start correctly.  Completely shutting down deployment.  Please see additional errors for details.: org.jboss.msc.service.StartException in service service jboss.mbean.service.jboss:name=test,type=messaging.create: Failed to start service
                              at org.jboss.msc.service.ServiceControllerImpl$StartTask.run(ServiceControllerImpl.java:959)
                              at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886) [:1.6.0_20]
                              at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908) [:1.6.0_20]
                              at java.lang.Thread.run(Thread.java:637) [:1.6.0_20]
                              Caused by: java.lang.NoClassDefFoundError: org/hornetq/api/core/Message
                              at java.lang.Class.getDeclaredConstructors0(Native Method) [:1.6.0_20]
                              at java.lang.Class.privateGetDeclaredConstructors(Class.java:2389) [:1.6.0_20]
                              at java.lang.Class.getConstructor0(Class.java:2699) [:1.6.0_20]
                              at java.lang.Class.getConstructor(Class.java:1657) [:1.6.0_20]
                              at org.jboss.msc.value.LookupConstructorValue.getValue(LookupConstructorValue.java:63)
                              at org.jboss.msc.value.LookupConstructorValue.getValue(LookupConstructorValue.java:33)
                              at org.jboss.msc.value.CachedValue.getValue(CachedValue.java:54)
                              at org.jboss.msc.value.ConstructedValue.getValue(ConstructedValue.java:55)
                              at org.jboss.msc.value.CachedValue.getValue(CachedValue.java:54)
                              at org.jboss.as.service.CreateDestroyService.getValue(CreateDestroyService.java:59)
                              at org.jboss.as.service.CreateDestroyService.start(CreateDestroyService.java:31)
                              at org.jboss.msc.service.ServiceControllerImpl$StartTask.run(ServiceControllerImpl.java:923)
                              ... 3 more
                              There are no steps to reproduce this apart from running the demo which works *most* of the time

                              • 12. Re: AS 7 demos
                                kabirkhan

                                I think the previous post can be ignored, I messed up my branches somehow… 
                                Trying out a JMS example I didn't see the ConnectionFactory and queue from standalone.xml getting bound in JNDI. JMSServerManagerImpl.bindToJndi() ignores the requests to bind to JNDI because no context has been set yet. If I modify this it gets bound in JNDI
                                +++ b/messaging/src/main/java/org/jboss/as/messaging/jms/JMSService.java
                                @@ -23,6 +23,8 @@
                                package org.jboss.as.messaging.jms;
                                import javax.naming.Context;
                                +import javax.naming.InitialContext;
                                +import javax.naming.NamingException;
                                import org.hornetq.core.server.HornetQServer;
                                import org.hornetq.jms.server.JMSServerManager;
                                @@ -31,10 +33,10 @@ import org.jboss.as.messaging.MessagingSubsystemElement;
                                import org.jboss.logging.Logger;
                                import org.jboss.msc.service.BatchBuilder;
                                import org.jboss.msc.service.Service;
                                -import org.jboss.msc.service.ServiceController.Mode;
                                import org.jboss.msc.service.StartContext;
                                import org.jboss.msc.service.StartException;
                                import org.jboss.msc.service.StopContext;
                                +import org.jboss.msc.service.ServiceController.Mode;
                                import org.jboss.msc.value.InjectedValue;
                                /**
                                @@ -49,10 +51,15 @@ public class JMSService implements Service<JMSServerManager> {
                                     private JMSServerManager jmsServer;
                                     public static void addService(final BatchBuilder builder) {
                                -        final JMSService service = new JMSService();
                                -        builder.addService(JMSSubsystemElement.JMS_MANAGER, service)
                                -            .addDependency(MessagingSubsystemElement.JBOSS_MESSAGING, HornetQServer.class, service.getHornetQServer())
                                -            .setInitialMode(Mode.ACTIVE);
                                +        try {
                                +            final JMSService service = new JMSService();
                                +            builder.addService(JMSSubsystemElement.JMS_MANAGER, service)
                                +                .addDependency(MessagingSubsystemElement.JBOSS_MESSAGING, HornetQServer.class, service.getHornetQServer())
                                +                .addInjection(service.getContextInjector(), (Context)new InitialContext())
                                +                .setInitialMode(Mode.ACTIVE);
                                +        } catch (NamingException e) {
                                +            throw new RuntimeException(e);
                                +        }
                                     }
                                     protected JMSService() {
                                @@ -65,7 +72,7 @@ public class JMSService implements Service<JMSServerManager> {
                                             final JMSServerManager jmsServer = new JMSServerManagerImpl(hornetQServer.getValue());
                                             final Context jndiContext = contextInjector.getOptionalValue();
                                -            if(context != null) {
                                +            if(jndiContext != null) {
                                                 jmsServer.setContext(jndiContext);
                                             }
                                ~
                                Next when I try to look up 'ConnectionFactory' from my example I get the following error:
                                00:12:02,953 ERROR [deployment] Caught exception handling update (param is 643db326-7dd6-4da3-97ca-f1ea0d2de596): org.jboss.msc.service.StartException in service service jboss.mbean.service.jboss:name=test,type=jms.start: Failed to execute legacy service start
                                at org.jboss.as.service.StartStopService.start(StartStopService.java:62)
                                at org.jboss.msc.service.ServiceControllerImpl$StartTask.run(ServiceControllerImpl.java:923)
                                at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886) [:1.6.0_20]
                                at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908) [:1.6.0_20]
                                at java.lang.Thread.run(Thread.java:637) [:1.6.0_20]
                                Caused by: java.lang.reflect.InvocationTargetException
                                at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) [:1.6.0_20]
                                at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) [:1.6.0_20]
                                at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) [:1.6.0_20]
                                at java.lang.reflect.Method.invoke(Method.java:597) [:1.6.0_20]
                                at org.jboss.as.service.StartStopService.start(StartStopService.java:59)
                                ... 4 more
                                Caused by: java.lang.ClassCastException: javax.naming.Reference cannot be cast to javax.jms.QueueConnectionFactory
                                at org.jboss.as.demos.jms.mbean.Test.start(Test.java:58)
                                ... 9 more
                                It appears to happen because in this path ObjectFactoryBuilder.factoryFromReference()'s contextCl is null, resulting in an exception, which returns 'this' i.e. the original Reference and not the ObjectFactory. Since the reference is a not a ModularReference we are trying the TCL.
                                ObjectFactoryBuilder.factoryFromReference(Reference, Hashtable<?,?>) line: 104
                                ObjectFactoryBuilder.createObjectFactory(Object, Hashtable<?,?>) line: 58
                                NamingManager.getObjectInstance(Object, Name, Context, Hashtable<?,?>) line: 282
                                NamingContext.getObjectInstance(Object, Name, Hashtable) line: 430
                                NamingContext.lookup(Name) line: 163
                                NamingContext.lookup(String) line: 173
                                InitialContext.lookup(String) line: 392
                                Test.start() line: 58
                                The object that is originally passed in to NamingContext.rebind() is HornetQConectionFactory which is an instance of Referenceable and whose getReference() method returns a plain Reference:
                                   public Reference getReference() throws NamingException
                                   {
                                      return new Reference(this.getClass().getCanonicalName(),
                                                           new SerializableObjectRefAddr("HornetQ-CF", this),
                                                           ConnectionFactoryObjectFactory.class.getCanonicalName(),
                                                           null);
                                   }
                                this then is what bound in JNDI, I don't think there is a way to override this to create a ModularReference. I can see queue/test and topic/test getting bound the same way so there will probably be a similar exception looking this up. Putting in some debug statements
                                        InitialContext ctx = new InitialContext();
                                        System.out.println(ctx.lookup("ConnectionFactory"));
                                        System.out.println(ctx.lookup("queue/test"));
                                Shows this:
                                09:27:22,965 INFO  [stdout] Reference Class Name: org.hornetq.jms.client.HornetQConnectionFactory
                                09:27:22,966 INFO  [stdout] Type: HornetQ-CF
                                09:27:22,966 INFO  [stdout] Content: [B@f4b2263
                                09:27:22,967 INFO  [stdout]
                                09:27:22,968 INFO  [stdout] Reference Class Name: org.hornetq.jms.client.HornetQQueue
                                09:27:22,968 INFO  [stdout] Type: HornetQ-DEST
                                09:27:22,968 INFO  [stdout] Content: [B@60da5686
                                09:27:22,969 INFO  [stdout]

                                I think the previous post can be ignored, I messed up my branches somehow… 

                                 

                                Trying out a JMS example I didn't see the ConnectionFactory and queue from standalone.xml getting bound in JNDI. JMSServerManagerImpl.bindToJndi() ignores the requests to bind to JNDI because no context has been set yet. If I modify this it gets bound in JNDI

                                +++ b/messaging/src/main/java/org/jboss/as/messaging/jms/JMSService.java

                                @@ -23,6 +23,8 @@

                                package org.jboss.as.messaging.jms;

                                 

                                import javax.naming.Context;

                                +import javax.naming.InitialContext;

                                +import javax.naming.NamingException;

                                 

                                import org.hornetq.core.server.HornetQServer;

                                import org.hornetq.jms.server.JMSServerManager;

                                @@ -31,10 +33,10 @@ import org.jboss.as.messaging.MessagingSubsystemElement;

                                import org.jboss.logging.Logger;

                                import org.jboss.msc.service.BatchBuilder;

                                import org.jboss.msc.service.Service;

                                -import org.jboss.msc.service.ServiceController.Mode;

                                import org.jboss.msc.service.StartContext;

                                import org.jboss.msc.service.StartException;

                                import org.jboss.msc.service.StopContext;

                                +import org.jboss.msc.service.ServiceController.Mode;

                                import org.jboss.msc.value.InjectedValue;

                                 

                                /**

                                @@ -49,10 +51,15 @@ public class JMSService implements Service<JMSServerManager> {

                                     private JMSServerManager jmsServer;

                                 

                                     public static void addService(final BatchBuilder builder) {

                                -        final JMSService service = new JMSService();

                                -        builder.addService(JMSSubsystemElement.JMS_MANAGER, service)

                                -            .addDependency(MessagingSubsystemElement.JBOSS_MESSAGING, HornetQServer.class, service.getHornetQServer())

                                -            .setInitialMode(Mode.ACTIVE);

                                +        try {

                                +            final JMSService service = new JMSService();

                                +            builder.addService(JMSSubsystemElement.JMS_MANAGER, service)

                                +                .addDependency(MessagingSubsystemElement.JBOSS_MESSAGING, HornetQServer.class, service.getHornetQServer())

                                +                .addInjection(service.getContextInjector(), (Context)new InitialContext())

                                +                .setInitialMode(Mode.ACTIVE);

                                +        } catch (NamingException e) {

                                +            throw new RuntimeException(e);

                                +        }

                                     }

                                 

                                     protected JMSService() {

                                @@ -65,7 +72,7 @@ public class JMSService implements Service<JMSServerManager> {

                                             final JMSServerManager jmsServer = new JMSServerManagerImpl(hornetQServer.getValue());

                                 

                                             final Context jndiContext = contextInjector.getOptionalValue();

                                -            if(context != null) {

                                +            if(jndiContext != null) {

                                                 jmsServer.setContext(jndiContext);

                                             }

                                 

                                ~

                                 

                                Next when I try to look up 'ConnectionFactory' from my example I get the following error:

                                 

                                00:12:02,953 ERROR [deployment] Caught exception handling update (param is 643db326-7dd6-4da3-97ca-f1ea0d2de596): org.jboss.msc.service.StartException in service service jboss.mbean.service.jboss:name=test,type=jms.start: Failed to execute legacy service start

                                at org.jboss.as.service.StartStopService.start(StartStopService.java:62)

                                at org.jboss.msc.service.ServiceControllerImpl$StartTask.run(ServiceControllerImpl.java:923)

                                at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886) [:1.6.0_20]

                                at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908) [:1.6.0_20]

                                at java.lang.Thread.run(Thread.java:637) [:1.6.0_20]

                                Caused by: java.lang.reflect.InvocationTargetException

                                at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) [:1.6.0_20]

                                at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) [:1.6.0_20]

                                at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) [:1.6.0_20]

                                at java.lang.reflect.Method.invoke(Method.java:597) [:1.6.0_20]

                                at org.jboss.as.service.StartStopService.start(StartStopService.java:59)

                                ... 4 more

                                Caused by: java.lang.ClassCastException: javax.naming.Reference cannot be cast to javax.jms.QueueConnectionFactory

                                at org.jboss.as.demos.jms.mbean.Test.start(Test.java:58)

                                ... 9 more

                                 

                                It appears to happen because in this path ObjectFactoryBuilder.factoryFromReference()'s contextCl is null, resulting in an exception, which returns 'this' i.e. the original Reference and not the ObjectFactory. Since the reference is a not a ModularReference we are trying the TCL.

                                 

                                ObjectFactoryBuilder.factoryFromReference(Reference, Hashtable<?,?>) line: 104

                                ObjectFactoryBuilder.createObjectFactory(Object, Hashtable<?,?>) line: 58

                                NamingManager.getObjectInstance(Object, Name, Context, Hashtable<?,?>) line: 282

                                NamingContext.getObjectInstance(Object, Name, Hashtable) line: 430

                                NamingContext.lookup(Name) line: 163

                                NamingContext.lookup(String) line: 173

                                InitialContext.lookup(String) line: 392

                                Test.start() line: 58

                                 

                                The object that is originally passed in to NamingContext.rebind() is HornetQConectionFactory which is an instance of Referenceable and whose getReference() method returns a plain Reference:

                                 

                                   public Reference getReference() throws NamingException

                                   {

                                      return new Reference(this.getClass().getCanonicalName(),

                                                           new SerializableObjectRefAddr("HornetQ-CF", this),

                                                           ConnectionFactoryObjectFactory.class.getCanonicalName(),

                                                           null);

                                   }

                                this then is what bound in JNDI, I don't think there is a way to override this to create a ModularReference. I can see queue/test and topic/test getting bound the same way so there will probably be a similar exception looking this up. Putting in some debug statements

                                 

                                        InitialContext ctx = new InitialContext();

                                 

                                        System.out.println(ctx.lookup("ConnectionFactory"));

                                        System.out.println(ctx.lookup("queue/test"));

                                 

                                Shows this:

                                09:27:22,965 INFO  [stdout] Reference Class Name: org.hornetq.jms.client.HornetQConnectionFactory

                                09:27:22,966 INFO  [stdout] Type: HornetQ-CF

                                09:27:22,966 INFO  [stdout] Content: [B@f4b2263

                                09:27:22,967 INFO  [stdout]

                                09:27:22,968 INFO  [stdout] Reference Class Name: org.hornetq.jms.client.HornetQQueue

                                09:27:22,968 INFO  [stdout] Type: HornetQ-DEST

                                09:27:22,968 INFO  [stdout] Content: [B@60da5686

                                09:27:22,969 INFO  [stdout]

                                 

                                 

                                • 13. Re: AS 7 demos
                                  kabirkhan

                                  Setting TCL in the MBean that does the work to the classloader of the MBean class fixes these naming lookups. I also needed to define the connector-ref in standalone.xml to get it to work

                                   

                                              <subsystem xmlns="urn:jboss:domain:messaging:jms:1.0">

                                                  <connection-factory name="testConnectionFactory">

                                                     <connectors>

                                                         <connector-ref connector-name="in-vm" backup-connector-name="netty"/>

                                                     </connectors>

                                                     <entries>

                                                        <entry name="ConnectionFactory" />

                                                     </entries>

                                                  </connection-factory>

                                  • 14. Re: AS 7 demos
                                    kabirkhan

                                    I am trying the following but am seeing 'no ds injected'. Should this work at this stage?

                                     

                                    package org.jboss.as.demos.warwithds.archive;

                                     

                                    import java.io.IOException;

                                    import java.io.Writer;

                                    import java.sql.Connection;

                                    import java.sql.ResultSet;

                                     

                                    import javax.annotation.Resource;

                                    import javax.naming.InitialContext;

                                    import javax.servlet.ServletException;

                                    import javax.servlet.annotation.WebServlet;

                                    import javax.servlet.http.HttpServlet;

                                    import javax.servlet.http.HttpServletRequest;

                                    import javax.servlet.http.HttpServletResponse;

                                    import javax.sql.DataSource;

                                     

                                    import org.jboss.logging.Logger;

                                     

                                    /**

                                    *

                                    * @author <a href="kabir.khan@jboss.com">Kabir Khan</a>

                                    * @version $Revision: 1.1 $

                                    */

                                    @WebServlet(name="SimpleServlet", urlPatterns={"/simple", "/other"})

                                    public class SimpleServlet extends HttpServlet {

                                     

                                        private static final long serialVersionUID = -2579304186167063651L;

                                     

                                        Logger log = Logger.getLogger(SimpleServlet.class.getName());

                                     

                                        @Resource(mappedName="java:/H2DS")

                                        DataSource ds;

                                     

                                        @Override

                                        protected void doGet(HttpServletRequest req, HttpServletResponse resp)

                                                throws ServletException, IOException {

                                     

                                            if (ds == null) {

                                                System.out.println("No ds injected");

                                                try {

                                                    ds = (DataSource)new InitialContext().lookup("java:/H2DS");

                                                } catch (Exception e) {

                                                    throw new ServletException("Could not find ds", e);

                                                }

                                            }

                                            String result = "Failure";

                                            try {

                                                Connection conn = ds.getConnection();

                                                ResultSet rs = conn.prepareStatement("select 1").executeQuery();

                                                if (rs.next()) {

                                                    result = "Success";

                                                }

                                            } catch (Exception e) {

                                                throw new ServletException(e);

                                            }

                                     

                                            Writer writer = resp.getWriter();

                                            writer.write("Simple Servlet datasource " + result);

                                        }

                                     

                                        @Override

                                        protected void doPost(HttpServletRequest req, HttpServletResponse resp)

                                                throws ServletException, IOException {

                                            this.doGet(req, resp);

                                        }

                                    }

                                    1 2 Previous Next