13 September 2013

WebLogic Server - Using OSGi Bundles with Java EE Applications

The WLS 12c (12.1.2) release includes a new feature that enables OSGi bundles to be installed and used by deployed applications.

The full documentation is here:

http://docs.oracle.com/middleware/1212/wls/WLPRG/osgi.htm

In short this feature enables WLS to create an OSGi framework (Apache Felix 4.03) in which OSGi Bundles are installed and accessed by applications.

Applications provide an XML reference element to define the named OSGi framework and Bundle of interest, which is then  published into the JNDI tree from where it can be referenced and used.

An OSGi Bundle can be included as part of the application archive being deployed or it can be provided as part of the server library set.

To provide a simple example, the Apache Felix tutorial Dictionary Service was first implemented and packaged as an OSGi Bundle.

The Bundle manifest looks as follows:
  Manifest-Version: 1.0
  Bnd-LastModified: 1378960044511
  Build-Jdk: 1.7.0_17
  Built-By: sbutton
  Bundle-Activator: tutorial.example2.Activator
  Bundle-ManifestVersion: 2
  Bundle-Name: DictionaryService OSGi Bundle
  Bundle-SymbolicName: tutorial.example2.service.DictionaryService
  Bundle-Version: 1.0.0.SNAPSHOT
  Created-By: Apache Maven Bundle Plugin
  Export-Package: tutorial.example2.service;version="1.0.0.SNAPSHOT"
  Import-Package: org.osgi.framework;version="[1.6,2)"
  Tool: Bnd-1.50.0

The Bundle archive contains only a few classes, the DictionaryService interface and an Activator implementation which provides an inner class implementation of the DictionaryService that is registered when the Bundle is activated.


 
A small web application was then developed to make use of the DictionaryService Bundle using a Servlet.

The Servlet performs the following tasks:
  • Injects the Bundle reference from JNDI using the @Resource annotation
  • Looks up the ServiceReference for the DictionaryService.class to be used 
  • Obtains an instance of the DictionaryService from the ServiceReference
  • Makes calls on the DictionaryService to check whether a word is in the known word list

Inject Bundle reference from its JNDI location:
@WebServlet(name = "TestServlet", urlPatterns = {"/TestServlet", "/test", "/osgi"})
public class TestServlet extends HttpServlet {

    @Resource(lookup = "java:app/osgi/Bundle")
    Bundle bundle;
    ...
}

The Bundle provides access to the registered Services, in this case the DictionaryService:
  if (bundle != null) {              
                             
    BundleContext bc = bundle.getBundleContext();
           
    ServiceReference dictionaryServiceRef = 
      bc.getServiceReference(DictionaryService.class);
    DictionaryService dictionaryService = 
      (DictionaryService) bc.getService(dictionaryServiceRef);

    ...

  }
 
The methods on the DictionaryService can then be used:

  out.printf("<div>wordlist: %s </div>", 
             Arrays.toString(dictionaryService.wordlist()));

  out.printf("<div>checkWord(\"%s\"): %s</div>", 
             wordToCheck, dictionaryService.checkWord(wordToCheck));


The Bundle then needs to be defined for the web application to use, which is done using the weblogic deployment descriptor.  In this example, the weblogic.xml file contains the following entry:
  <osgi-framework-reference>
    <name>test</name>
    <application-bundle-symbolic-name>
      tutorial.example2.service.DictionaryService
    </application-bundle-symbolic-name>
  </osgi-framework-reference>

The bundle-symbolic-name is used to specify the bundle to be used.

The name element specifies the name of the OSGi framework that has been configured.  With WebLogic Server 12c (12.1.2) there are several ways to define and configure OSGi frameworks such as the Admin Console, WLST, programmatically with Java or by directly editing the domain config.xml file.

With this small web application, the DictionaryService Bundle was deployed as part of the WAR file itself.  This is performed by placing the JAR file in the WEB-INF/osgi-lib directory, whereupon WLS will detect it and install it.  The DictionaryService Bundle could also be installed by copying it into the $ORACLE_HOME/wlserver/server/osgi-lib directory.

With the WAR file packaged and deployed to WLS,  the console logs show the DictionaryService Bundle being Activated, where System.out.println() calls were inserted into the start and stop Activator methods to view them being called:

*** START Bundle org.apache.felix.framework.BundleContextImpl@21052189
tutorial.example2.service.DictionaryService ***
Finally the TestServlet is accessed, demonstrating the DictionaryService being accessed and used:



29 August 2013

23 August 2013

WebLogic Server 12.1.2 - Zip File Updates

I love the zip file distribution.  It's the flavour of WLS I mostly use.  Unzip, configure, go.  Delete it to remove it, start all over again.  Install it from a Maven goal.  Easy.
Small point to note is that the zip file distribution has been pulled down from OTN for the last couple of days (20 Aug 2013) while we fix a minor problem relating to the launching of the Configuration Wizard in this specific distribution.  Should be back up today or tomorrow (23 Aug 2013).
With the release of WLS 12.1.2 now available and getting some decent download traction, I thought I'd make a few comments about the zip file distribution as it has some minor changes in this version:
  1. It now contains an outer level directory wls12120, in which the zip file contents are stored.  Previously the contents were stored in an outer level directory.
  2. The configure.sh|cmd script now has an option to run in "silent" mode, where the progress of the configuration process is not displayed to the console.

  3. At the end of the configuration process you are now asked if you'd like to create a domain.  If the answer is yes, the relevant details are asked for and a new domain created and started.  This enables you to configure and start up a server from the one simple script.    
 The process of using the zip file distribution is shown below.

[sbutton@mbp] $ unzip ~/Downloads/WLS/1212/PROD/wls1212_dev.zip

Archive:  /Users/sbutton/Downloads/WLS/1212/PROD/wls1212_dev.zip
   creating: wls12120/
   creating: wls12120/coherence/
   creating: wls12120/coherence/bin/
   creating: wls12120/coherence/lib/
   creating: wls12120/coherence/lib/security/
   creating: wls12120/coherence/plugins/
   creating: wls12120/coherence/plugins/maven/
   ...

   ...
  inflating: wls12120/wlserver/common/bin/clonedunpack.cmd 
  inflating: wls12120/wlserver/common/bin/commEnv.cmd 
  inflating: wls12120/wlserver/common/bin/config.cmd 
  inflating: wls12120/wlserver/common/bin/config_builder.cmd 
  inflating: wls12120/wlserver/common/bin/pack.cmd 
  inflating: wls12120/wlserver/common/bin/prepareCustomProvider.cmd 
  inflating: wls12120/wlserver/common/bin/qs_config.cmd 
  inflating: wls12120/wlserver/common/bin/reconfig.cmd 
  inflating: wls12120/wlserver/common/bin/unpack.cmd 
  inflating: wls12120/wlserver/common/bin/wlsifconfig.cmd 
  inflating: wls12120/wlserver/common/bin/wlst.cmd 
  inflating: wls12120/wlserver/common/deployable-libraries/active-cache-1.0.jar.pack 
  inflating: wls12120/wlserver/common/deployable-libraries/jsf-1.2.war 
  inflating: wls12120/wlserver/common/deployable-libraries/jsf-2.0.war 
  ...

  ...
  inflating: wls12120/wlserver/common/bin/wlst.sh 
  inflating: wls12120/wlserver/server/bin/setWLSEnv.sh 
  inflating: wls12120/wlserver/server/bin/startNodeManager.sh 
  inflating: wls12120/wlserver/server/lib/unix/nodemanager.sh 
 

[sbutton@mbp]  $ cd wls12120

[sbutton@mbp]  $ ./configure.sh

**************************************************
WebLogic Server 12g (12.1.2.0) Zip Configuration

MW_HOME:   /tmp/wls12120
JAVA_HOME: /Library/Java/JavaVirtualMachines/jdk1.7.0_17.jdk/Contents/Home

Note:      MW_HOME not supplied, default used
**************************************************
...

...
...
...Unpacking done                                           0 to go

Your environment has been set.
Configuring WLS...

BUILD SUCCESSFUL
Total time: 1 second

Do you want to configure a new domain?  [y/n]? Y


<21/08/2013 12:05:41 PM CST> <Info> <Security> <BEA-090905> <Disabling the CryptoJ JCE Provider self-integrity check for better startup performance. To enable this check, specify -Dweblogic.security.allowCryptoJDefaultJCEVerification=true.>
<21/08/2013 12:05:41 PM CST> <Info> <Security> <BEA-090906> <Changing the default Random Number Generator in RSA CryptoJ from ECDRBG to FIPS186PRNG. To disable this change, specify -Dweblogic.security.allowCryptoJDefaultPRNG=true.>
<21/08/2013 12:05:42 PM CST> <Info> <WebLogicServer> <BEA-000377> <Starting WebLogic Server with Java HotSpot(TM) 64-Bit Server VM Version 23.7-b01 from Oracle Corporation.>
<21/08/2013 12:05:42 PM CST> <Info> <Management> <BEA-140013> </private/tmp/wls12120/user_projects/domains/mydomain/config not found>
<21/08/2013 12:05:42 PM CST> <Info> <Security> <BEA-090065> <Getting boot identity from user.>
 

Enter username to boot WebLogic server:weblogic   
Enter password to boot WebLogic server: ********

For confirmation, please re-enter password required to boot WebLogic server: ******** 
<21/08/2013 12:05:49 PM CST> <Info> <Management> <BEA-141254> <Generating new domain directory in /private/tmp/wls12120/user_projects/domains/mydomain.>
<21/08/2013 12:05:57 PM CST> <Info> <Management> <BEA-141255> <Domain generation completed in 8,737 milliseconds.>
<21/08/2013 12:05:57 PM CST> <Info> <Management> <BEA-141107> <Version: WebLogic Server 12.1.2.0.0  Fri Jun 7 15:16:15 PDT 2013 1530982 WLS_12.1.2.0.0_GENERIC_130607.1100>
<21/08/2013 12:05:59 PM CST> <Notice> <WebLogicServer> <BEA-000365> <Server state changed to STARTING.>
<21/08/2013 12:05:59 PM CST> <Info> <WorkManager> <BEA-002900> <Initializing self-tuning thread pool.>
<Aug 21, 2013 12:05:59 PM CST> <Notice> <Log Management> <BEA-170019> <The server log file /private/tmp/wls12120/user_projects/domains/mydomain/servers/myserver/logs/myserver.log is opened. All server side log events will be written to this file.>
<Aug 21, 2013 12:06:02 PM CST> <Notice> <Security> <BEA-090082> <Security initializing using security realm myrealm.>
<Aug 21, 2013 12:06:02 PM CST> <Warning> <Store> <BEA-280109> <Unable to load the native wlfileio library for the persistent file store "_WLS_myserver". The store will use buffered I/O. The store is still operating in a transactionally safe synchronous mode. See store open log messages for the requested and final write policies.>
<Aug 21, 2013 12:06:05 PM CST> <Notice> <WebLogicServer> <BEA-000365> <Server state changed to STANDBY.>
<Aug 21, 2013 12:06:05 PM CST> <Notice> <WebLogicServer> <BEA-000365> <Server state changed to STARTING.>
<Aug 21, 2013 12:06:05 PM CST> <Notice> <Log Management> <BEA-170027> <The server has successfully established a connection with the Domain level Diagnostic Service.>
<Aug 21, 2013 12:06:05 PM CST> <Notice> <WebLogicServer> <BEA-000365> <Server state changed to ADMIN.>
<Aug 21, 2013 12:06:05 PM CST> <Notice> <WebLogicServer> <BEA-000365> <Server state changed to RESUMING.>
<Aug 21, 2013 12:06:05 PM CST> <Notice> <Server> <BEA-002613> <Channel "Default[1]" is now listening on fdfa:13b7:e006:7177:196:ad1b:8c13:ef38:7001 for protocols iiop, t3, ldap, snmp, http.>
<Aug 21, 2013 12:06:05 PM CST> <Notice> <Server> <BEA-002613> <Channel "Default" is now listening on 10.187.112.164:7001 for protocols iiop, t3, ldap, snmp, http.>
<Aug 21, 2013 12:06:05 PM CST> <Notice> <Server> <BEA-002613> <Channel "Default[3]" is now listening on 127.0.0.1:7001 for protocols iiop, t3, ldap, snmp, http.>
<Aug 21, 2013 12:06:05 PM CST> <Notice> <Server> <BEA-002613> <Channel "Default[2]" is now listening on 0:0:0:0:0:0:0:1:7001 for protocols iiop, t3, ldap, snmp, http.>
<Aug 21, 2013 12:06:05 PM CST> <Notice> <WebLogicServer> <BEA-000331> <Started the WebLogic Server Administration Server "myserver" for domain "mydomain" running in development mode.>
<Aug 21, 2013 12:06:05 PM CST> <Warning> <Server> <BEA-002611> <The hostname "localhost", maps to multiple IP addresses: 127.0.0.1, 0:0:0:0:0:0:0:1, fe80:0:0:0:0:0:0:1%1.>
<Aug 21, 2013 12:06:05 PM CST> <Notice> <WebLogicServer> <BEA-000365> <Server state changed to RUNNING.>
<Aug 21, 2013 12:06:05 PM CST> <Notice> <WebLogicServer> <BEA-000360> <The server started in RUNNING mode.>



 At this point there is now a new domain called "mydomain" running and available on the default WLS port of 7001 protected with the credentials specified during the configuration process.

This process can be used immediately, or it can be stopped using any of the standard process control methods and restarted using the scripts in the generated domain directory, per the following:

Generating new domain directory in /private/tmp/wls12120/user_projects/domains/mydomain

As a final point, the zip file can be installed into a Maven repository using whatever coordinates you wish, then referenced and used with the weblogic-maven-plugin install goal to perform a local installation operation of the server.

Combined with the subsequent execution of the weblogic-maven-plugin create-domain and start-server goals, a fully operational server can be created from Maven in a short matter of time, ready and waiting to accept application deployments.







20 August 2013

WLS 10.3.6 Transaction Logs in Database

Was talking to one of our local partners yesterday who's in the middle of an infrastructure build for FMW using a 3rd party cloud provider.  He mentioned he was looking at how to handle the JTA transaction logs for their HA requirements without being able to rely on shared storage being available.

I mentioned that we'd added support for storing the XA logs in the database in WLS 10.3.6, which should make both his initial setup, replication and recovery processes more straight forward.
 
http://docs.oracle.com/cd/E23943_01/web.1111/e13852/toc.htm#BGGHGHBC

JTA

You can configure a JDBC TLOG store to persist transaction logs to a database, which allows you to leverage replication and HA characteristics of the underlying database, simplify disaster recovery, and improve Transaction Recovery service migration. See "Using a JDBC TLog Store" in Configuring Server Environments for Oracle WebLogic Server.


WebLogic Server 12c (12.1.2) Documentation

The WebLogic Server documentation set has undergone a nice facelift in the 12c (12.1.2) release. 

http://fmwdocs.us.oracle.com/vol/doclibs/middleware/production/E26099_01/wls/index.html

I find the breakdown sections generally quite useful to quickly locate the information I'm looking for, but I still regularly go directly to a specific book as a reference.

The entire book set is available here:

http://fmwdocs.us.oracle.com/vol/doclibs/middleware/production/E26099_01/wls/docs.htm

I also think the provision of the new mobi and epub formats is incredibly useful, allowing you to pull down a set of books onto a mobile device.   I flew to Melbourne yesterday and was able to read over a set of docs on the plane that I'd pulled onto my iPad before I left.

I've been wondering if it'd be possible to provide some sort of library/multi-book selection option to allow multiple book selections to be made at once and have them downloaded as a library that can be pushed to a mobile device?  I've not looked into it, but I think it'd be a nice usability feature.  Does iBooks have any APIs that could support that?  Will need to look int it at some point.

17 July 2013

WebLogic Server 12.1.2 Released

The WebLogic Server 12.1.2 release is now available for download from OTN.

Contains a raft of new features, check out the list in the What's New guide:

http://docs.oracle.com/middleware/1212/wls/NOTES/index.html#NOTES254

Lots of good stuff there, covering new development features as well as operational features such as dynamic servers, server templates and so forth.  

On one of my favourite topics I've written about here in the past, I'm pleased to say that the WebSockets implementation made it into the release.

WebSockets

This release of WebLogic Server adds support for the WebSocket Protocol (RFC 6455), which provides two-way, full-duplex communication over a single TCP connection between clients and servers, where each side can send data independently from the other. 

The WebSockets communication model occurs in real-time and promotes user interaction.

In WebLogic Server, you can use the WebSocket Protocol implementation and accompanying programming API to develop and deploy applications that communicate bidirectionally with clients.

For more information, including code examples, see "Using WebSockets in WebLogic Server" in Developing Applications for Oracle WebLogic Server.

Take a look at the developer documentation we have for it here:

http://docs.oracle.com/middleware/1212/wls/WLPRG/websockets_sse.htm#WLPRG806

The small API is documented and available from the main WLS API page:

http://docs.oracle.com/middleware/1212/wls/WLAPI/overview-summary.html

Look for the weblogic.websocket package.

The ServerSent-Event feature that I'd described here as being under investigation didn't make it into the 12.1.2 release.  There are plans are to look at it again for the next release.