Monday Feb 28, 2011

Application Scoped Resources in GlassFish

Application Scoped Resources in GlassFish

Application Scoped Resources


Often, all applications require various type of resources for its usage. These resources can be of JDBC, JMS, Connector, JavaMail, Custom, JNDI etc., User can create resources using appropriate CLI (Command Line Interface) or using GUI. GlassFish supports both of them. There is also support to define resources in an xml file that conforms to "glassfish-resources.dtd" and using "add-resources resources-xml-file" command, all the resources
defined in the resources xml file can be created. Almost all of these options involve multiple step ie., create the resources first and then let the application
use these resources. These resources are usually shared across applications. These resources must also be undeployed when application(s) no longer need them.

Application Scoped Resources helps to bundle the resource definitions along with the application and as part of deployment of the application, these
resources are created and made available to the application. Similarly, these resources will be automatically destroyed when the application is undeployed.
Also, by scoping these resources within the application, these resources are available only for the application and hence can be free from resource-starvation due to other application's usage of these resources.
Java EE 6 also introduces application component environment namespaces which includes the following scopes.
  • java:comp : per component namespace
  • java:module : namespace shared by all components in a module (eg: ejb's in a ejb-jar.xml)
  • java:app : namespace shared by all components, modules in an application (eg: application-client, web component, ejb component in an application, .ear)
  • java:global : global namespace for all applications in the server
Applications can define "application" scoped and "module" scoped resources in the archive.
  • resource's "name"/"jndi-name" attribute prefixed with value "java:module/" will make the resource available all the ejb components defined in its ejb-jar.xml (All EJBs in an ejb-jar.xml)
  • resource's "name"/"jndi-name" attribute prefixed with value "java:app/" will make the resource available across all components/modules in the application (EJBs, Servlet, Application Client in the application)


Sample archive layout that bundles glassfish-resources.xml:


ear
      -> META-INF
                              -> application.xml
                              -> glassfish-resources.xml [Application scoped resources]
      -> web-module
                  -> WEB-INF
                                        -> web.xml
                                        -> glassfish-web.xml
                                        -> glassfish-resources.xml [Module scoped resources]
     -> ejb-module 
                 -> META-INF
                                         -> ejb-jar.xml
                                         -> glassfish-ejb-jar.xml
                                         -> glassfish-resources.xml [Module scoped resources]


Demo application :

The sample application provided demonstrates the usage of jdbc-resources across the application,  on various scopes. The sample has following components
  • web component [Servlet.java]
  • ejb component(s) [HelloStatefulEJB.java and HelloEJB.java]
  • application client [Client.java]
There are three "glassfish-resources.xml" (s) in the application, one at the "ear" level, one each at "web-module" and "ejb-module" level  respectively. (Similar to the "sample archive layout" shown above)
  • application-level glassfish-resources.xml defines a jdbc-connection-pool and jdbc-resource by name "java:app/app-scoped-pool" and "java:app/app-scoped-resource" respectively.
  • glassfish-resources.xml in WEB-INF directory of web-module defines a jdbc-connection-pool and jdbc-resource by name "java:module/web-module-scoped-pool" and "java:module/web-module-scoped-resource" respectively.
  • glassfish-resources.xml in META-INF directory of ejb-module defines a jdbc-connection-pool and jdbc-resource by name "java:module/ejb-module-scoped-pool" and "java:module/ejb-module-scoped-resource" respectively.
When executed for a particular resource name, the sample application will do lookup of the specified resource in Application Client, EJB(s), Servlet and based on the availability
of the resource in the component's scope it will either use the provided resource or the default jdbc-resource (jdbc/__default) when the resource specified is not available
in the component's scope.

Following are the jdbc resources defined (or exported) by the application.


  1. java:app/app-scoped-resource
  2. java:module/ejb-module-scoped-resource
  3. java:module/web-module-scoped-resource



Steps to execute:

  1. Ensure that JDK 1.6 is installed and the environment variable JAVA_HOME is set
    1. Ensure that the environment variable PATH has JAVA_HOME/bin added to it.
  2. Ensure that ANT is installed and the environment variable ANT_HOME is set
    1. Ensure that the environment variable PATH has ANT_HOME/bin added to it.
  3. Download GlassFish 3.1
  4. Install GlassFish 3.1
  5. Start GlassFish (asadmin start-domain domain1)
  6. Start Derby database (asadmin start-database)
  7. Download the sample
  8. Extracted location of sample is referred as "SAMPLE_HOME"
  9. goto SAMPLE_HOME
  10. set environment variable GF_HOME to GlassFish's installation location.
  11. "ant all" will compile, assemble, deploy, run the application and then undeploy the application.


Sample Output :

Existing build.xml is configured to demostrate the availability of the following resources across Application Client, Servlet, EJB(s)

  • java:global/env/Servlet_Resource
  • java:app/env/HelloStatefulEJB_Resource
  • java:module/env/HelloEJB_Resource
  • java:comp/env/Appclient_Resource


Sample output for
"java:app/app-scoped-resource" will look like :


     [exec] Mode : appclient
     [exec] Resource name : java:app/app-scoped-resource
     [exec] ----------------------------------------------------------------------------------------------------------
     [exec] REQUESTED RESOURCE                   : java:app/app-scoped-resource
     [exec] ACTUAL RESOURCE USED                 : java:app/app-scoped-resource
     [exec] RESULT                               : RESOURCE ACCESSIBLE
     [exec] ----------------------------------------------------------------------------------------------------------
     [exec]
     [exec] Mode : servlet
     [exec] Resource name : java:app/app-scoped-resource
     [exec] ----------------------------------------------------------------------------------------------------------
     [exec] REQUESTED RESOURCE                   : java:app/app-scoped-resource
     [exec] ACTUAL RESOURCE USED                 : java:app/app-scoped-resource
     [exec] RESULT                               : RESOURCE ACCESSIBLE
     [exec] ----------------------------------------------------------------------------------------------------------
     [exec]
     [exec] Mode : stateful_ejb
     [exec] Resource name : java:app/app-scoped-resource
     [exec] ----------------------------------------------------------------------------------------------------------
     [exec] REQUESTED RESOURCE                   : java:app/app-scoped-resource
     [exec] ACTUAL RESOURCE USED                 : java:app/app-scoped-resource
     [exec] RESULT                               : RESOURCE ACCESSIBLE
     [exec] ----------------------------------------------------------------------------------------------------------
     [exec]
     [exec] Mode : stateless_ejb
     [exec] Resource name : java:app/app-scoped-resource
     [exec] ----------------------------------------------------------------------------------------------------------
     [exec] REQUESTED RESOURCE                   : java:app/app-scoped-resource
     [exec] ACTUAL RESOURCE USED                 : java:app/app-scoped-resource
     [exec] RESULT                               : RESOURCE ACCESSIBLE
     [exec] ----------------------------------------------------------------------------------------------------------


In the above sample, "RESULT" indicate that the jdbc-resource "java:app/app-scoped-resource" is available across all components (EJB(s), Servlet, Application Client)

Similarly, sample output for the resource of name "java:module/ejb-module-scoped-resource" will look like :


     [exec] Mode : appclient
     [exec] Resource name : java:module/ejb-module-scoped-resource
     [exec] ----------------------------------------------------------------------------------------------------------
     [exec] REQUESTED RESOURCE                   : java:module/ejb-module-scoped-resource
     [exec] ACTUAL RESOURCE USED                 : jdbc/__default
     [exec] RESULT                               : RESOURCE NOT ACCESSIBLE
     [exec] ----------------------------------------------------------------------------------------------------------
     [exec]
     [exec]
     [exec] Mode : servlet
     [exec] Resource name : java:module/ejb-module-scoped-resource
     [exec] ----------------------------------------------------------------------------------------------------------
     [exec] REQUESTED RESOURCE                   : java:module/ejb-module-scoped-resource
     [exec] ACTUAL RESOURCE USED                 : jdbc/__default
     [exec] RESULT                               : RESOURCE NOT ACCESSIBLE
     [exec] ----------------------------------------------------------------------------------------------------------
     [exec]
     [exec]
     [exec] Mode : stateful_ejb
     [exec] Resource name : java:module/ejb-module-scoped-resource
     [exec] ----------------------------------------------------------------------------------------------------------
     [exec] REQUESTED RESOURCE                   : java:module/ejb-module-scoped-resource
     [exec] ACTUAL RESOURCE USED                 : java:module/ejb-module-scoped-resource
     [exec] RESULT                               : RESOURCE ACCESSIBLE
     [exec] ----------------------------------------------------------------------------------------------------------
     [exec]
     [exec]
     [exec] Mode : stateless_ejb
     [exec] Resource name : java:module/ejb-module-scoped-resource
     [exec] ----------------------------------------------------------------------------------------------------------
     [exec] REQUESTED RESOURCE                   : java:module/ejb-module-scoped-resource
     [exec] ACTUAL RESOURCE USED                 : java:module/ejb-module-scoped-resource
     [exec] RESULT                               : RESOURCE ACCESSIBLE
     [exec] ----------------------------------------------------------------------------------------------------------


In the above sample, "RESULT" indicate that the datasource "java:module/ejb-module-scoped-resource" is available only for the EJB components in the ejb-jar.xml ie., HelloEJB and HelloStatefulEJB.

To list the resources defined in application/module scope :

$GF_HOME/bin/asadmin list-applications --resources --subcomponents

app-scoped-resources-demoApp  <ear, appclient, ejb, web>
  java:app/app-scoped-resource    <JdbcResource>
  java:app/app-scoped-pool    <JdbcConnectionPool>
  app-scoped-resources-demo-client.jar  <AppClientModule>
  app-scoped-resources-demo-ejb.jar     <EJBModule>       
    java:module/ejb-module-scoped-resource    <JdbcResource>
    java:module/ejb-module-scoped-pool    <JdbcConnectionPool>
  app-scoped-resources-demo-web.war     <WebModule>       
    java:module/web-module-scoped-resource    <JdbcResource>
    java:module/web-module-scoped-pool    <JdbcConnectionPool>
Command list-applications executed successfully.

For more information about application-scoped-resources including the domain.xml configuration representation, using dotted-names-get/set command to re-configure the resources, monitoring information,
flush-connection-pool and ping-connection-pool commands, please refer the demo.





References :



Tuesday Dec 29, 2009

Applying Scope for JDBC Resources in GlassFish

JDBC Resources With Scope in GlassFish

Applying Scope for JDBC Resources in GlassFish

Java EE 6 introduces new set of application component environment namespace scopes.
Using the scope, an application can define (or export) a name such that it is visible for other components or modules within the application or even visible globally.

Scope:

  • java:comp : per component namespace
  • java:module : namespace shared by all components in a module (eg: ejb's in a ejb-jar.xml)
  • java:app : namespace shared by all components, modules in an application (eg: application-client, web component, ejb component in an application, .ear)
  • java:global : global namespace for all applications in the server

It is possible to define names with scope and thus make it available across the module, application or globally.

eg:
  • <res-ref-name> element's "name" attribute with prefixed value "java:comp/env/" will make the resource available only for the component in which it is defined (eg: Servlet or EJB or Application Client)
  • <res-ref-name> element's "name" attribute with prefixed value "java:module/env/" will make the resource available all the ejb components defined in its ejb-jar.xml (All EJBs in an ejb-jar.xml)
  • <res-ref-name> element's "name" attribute with prefixed value "java:app/env/" will make the resource available across all components/modules in the application (EJBs, Servlet, Application Client in the application)
  • <res-ref-name> element's "name" attribute with prefixed value "java:global" will  make the resource available across the server

By default, if no prefix is specified it will be equivalent to "java:comp"

Demo application :

The sample application provided demonstrates the usage of various scopes across the application,  by specifying resource-ref in deployment-descriptors. The sample has following components
  • web component [Servlet.java]
  • ejb component(s) [HelloStatefulEJB.java and HelloEJB.java]
  • application client [Client.java]
When executed for a particular resource name, the sample application will do lookup of the resource in Application Client, EJB(s), Servlet and based on the availability
of the resource in the component's scope it will either use the provided resource (datasource) or the default jdbc-resource (jdbc/__default).

Following are the resources defined (or exported) by the application.
  • java:global/env/Servlet_Resource
  • java:app/env/HelloStatefulEJB_Resource
  • java:module/env/HelloEJB_Resource
  • java:comp/env/Appclient_Resource

From the name, the scope of the resource, component in which the resource is defined can be identified.
In the following example :java:app/env/HelloStatefulEJB_Resource
Scope is "app"
Resource is defined for "HelloStatefulEJB"



Steps to execute:

  1. Ensure that JDK 1.6 is installed and the environment variable JAVA_HOME is set
    1. Ensure that the environment variable PATH has JAVA_HOME/bin added to it.
  2. Ensure that ANT is installed and the environment variable ANT_HOME is set
    1. Ensure that the environment variable PATH has ANT_HOME/bin added to it.
  3. Download GlassFish v3
  4. Install GlassFish v3
  5. Start GlassFish (asadmin start-domain domain1)
  6. Start Derby database (asadmin start-database)
  7. Download the sample
  8. Extracted location of sample is referred as "SAMPLE_HOME"
  9. goto SAMPLE_HOME
  10. set environment variable GF_HOME to GlassFish's installation location.
  11. "ant all" will compile, assemble, deploy, run the application and then undeploy the application.


Sample Output :

Existing build.xml is configured to demostrate the availability of the following resources across Application Client, Servlet, EJB(s)

  • java:global/env/Servlet_Resource
  • java:app/env/HelloStatefulEJB_Resource
  • java:module/env/HelloEJB_Resource
  • java:comp/env/Appclient_Resource


Sample output for
"java:module/env/HelloEJB_Resource" will look like :

     [exec] Mode : appclient
     [exec] Resource name : java:module/env/HelloEJB_Resource
     [exec] ----------------------------------------------------------------------------------------------------------
     [exec] REQUESTED RESOURCE              : java:module/env/HelloEJB_Resource
     [exec] ACTUAL RESOURCE USED          : jdbc/__default
     [exec] RESULT                                        : RESOURCE NOT ACCESSIBLE
     [exec] ----------------------------------------------------------------------------------------------------------
     [exec]
     [exec]
     [exec] Mode : servlet
     [exec] Resource name : java:module/env/HelloEJB_Resource
     [exec] ----------------------------------------------------------------------------------------------------------
     [exec] REQUESTED RESOURCE              : java:module/env/HelloEJB_Resource
     [exec] ACTUAL RESOURCE USED          : jdbc/__default
     [exec] RESULT                                        : RESOURCE NOT ACCESSIBLE
     [exec] ----------------------------------------------------------------------------------------------------------
     [exec]
     [exec]
     [exec] Mode : stateful_ejb
     [exec] Resource name : java:module/env/HelloEJB_Resource
     [exec] ----------------------------------------------------------------------------------------------------------
     [exec] REQUESTED RESOURCE              : java:module/env/HelloEJB_Resource
     [exec] ACTUAL RESOURCE USED          : java:module/env/HelloEJB_Resource
     [exec] RESULT                                        : RESOURCE ACCESSIBLE
     [exec] ----------------------------------------------------------------------------------------------------------
     [exec]
     [exec]
     [exec] Mode : stateless_ejb
     [exec] Resource name : java:module/env/HelloEJB_Resource
     [exec] ----------------------------------------------------------------------------------------------------------
     [exec] REQUESTED RESOURCE              : java:module/env/HelloEJB_Resource
     [exec] ACTUAL RESOURCE USED          : java:module/env/HelloEJB_Resource
     [exec] RESULT                                        : RESOURCE ACCESSIBLE
     [exec] ----------------------------------------------------------------------------------------------------------



In the above sample, "RESULT" indicate that the datasource "java:module/env/HelloEJB_Resource" is available only for the EJB components in the ejb-jar.xml ie., HelloEJB and HelloStatefulEJB.


References :



About

jagadish

Search

Categories
Archives
« April 2014
SunMonTueWedThuFriSat
  
1
2
3
4
5
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