05 March 2008

Starting OC4J from Ant

Good question today from a colleague in Dublin, who basically wanted to know how to start an OC4J instance from Ant using the oc4j:restart.

On the surface that looks like a reasonable thing to do. The problem though is that the OC4J Ant tasks actually require a connection to be made to the server in order to execute the operation -- so if the OC4J instance is down, you can't issue a restart since you can't connect to it.

For situations where the OC4J instance is being managed by OPMN, then one possible solution to this is to call out to the opmnctl executable and ask it to start the OC4J instance.

To do this, you can make use of the ant:exec task as shown below:


<project name="opmn" basedir="." default="status">
<property name="oracle.home" value="d:/oracleas/10131/ohs_j2ee"/>
<property name="opmn.cmd" value="${oracle.home}/opmn/bin/opmnctl.exe"/>

<target name="base" description="Verify callout to opmnctl">
<exec executable="${opmn.cmd}"/>
</target>

<target name="status" description="Show the current status">
<exec executable="${opmn.cmd}">
<arg line="status"/>
</exec>
</target>

<!-- Start the oc4j.name instance-->
<target name="start-oc4j" description="Start the oc4j.name instance" depends="get-oc4j-name">
<echo message="${opmn.cmd} startproc process-type=${oc4j.name}"/>
<exec executable="${opmn.cmd}">
<arg line="startproc process-type=${oc4j.name}"/>
</exec>
<antcall target="status"/>
</target>

<target name="get-oc4j-name" unless="oc4j.name">
<input message="Specify OC4J instance: " addproperty="oc4j.name"/>
</target>
</project>


When this executes, it prompts for the OC4J instance name, calls opmnctl to execute a startproc on it and then displays the status.


>ant -f opmn.xml start-oc4j
Buildfile: opmn.xml

get-oc4j-name:
[input] Specify OC4J instance:
fred

start-oc4j:
[echo] d:/oracleas/10131/ohs_j2ee/opmn/bin/opmnctl.exe startproc process-type=fred
[exec] opmnctl: starting opmn managed processes...

status:

[exec] Processes in Instance: ohs_j2ee_changed
[exec] ---------------------------------+--------------------+---------+---------
[exec] ias-component | process-type | pid | status
[exec] ---------------------------------+--------------------+---------+---------
[exec] OC4JGroup:default_group | OC4J:foo | 2128 | Alive
[exec] OC4JGroup:default_group | OC4J:fred | 6012 | Alive
[exec] OC4JGroup:default_group | OC4J:home | N/A | Down

07 November 2007

Application Supplied System Properties

We added a feature in the 10.1.3.1 release which enables an application to supply its own set of System properties, which are added and removed to the global System property set as the application is started and stopped. The documentation for it is not extensive, but there's a how-to on OTN which walks through the use of it.

Check it out on OTN.

The System properties (name, value) are defined in the orion-application.xml descriptor provided with the application.

<orion-application>
<property name="application.tax.rate" value="10" />
<property name="application.tax.code" value="AU" />
</orion-application>
During deployment with ASC, the deployment plan editor can be used to view the set of specified System properties in the application. It can also be used to add, remove, edit the defined properties so they can be tailored to the specific deployment being performed.

05 November 2007

Run a Coherence Cache Server from JDeveloper

I was working on a Coherence based handson lab for the upcoming Open World conference last week. The lab configures Coherence to use a JPA based CacheStore to store/load cache entries into/from a database.

The labs are all based around an Ant script that runs the lab code, plus the Cache Servers. But one handy thing that could have been documented in the labs is the ability to run a Cache Server(s) directly from JDeveloper using its "Run Manager".

Here's how it works:

1. Have a project set up which has the Coherence JAR files added as a library.

2. Create a new Run Configuration for your Cache Server.

3. In the "Default Run Target" field enter:

C:\coherence-ejb3\coherence\lib\tangosol.jar!\com\tangosol\net\DefaultCacheServer.class

4. Specify any additional JVM or Coherence properties as desired.

Click save and you're done.



Now to run a Cache Server, simply select your Cache Server run target from the list and the Cache Server will start. A log window will open up showing the output from the Cache Server. The process running the Cache Server will be shown in the Process Manager window.



The other Run Configuration I found helpful was to launch the Console that comes with Coherence. This little application is a cache client, and allows you to interactively perform operations against the cache. Handy to quickly see the size of a cache or list out some elements from the cache to see that its containing the expected data objects.

To run this Coherence application, you create a new Run Configuration as above, except specifying the Default Run Target as:

C:\coherence-ejb3\coherence\lib\coherence.jar!\com\tangosol\coherence\component\application\Console.class



Since the Coherence console is an interactive application, one extra little step you need to do here is to enable "Allow Program Input" for this running process.

In the Run Configuration dialog, select the "Tool Settings" node and then check the "Allow Program Input" option.



Save the entry and execute it. You should then the Coherence Console application fire up and prompt for your command in the "Input: [ ]" field.










25 October 2007

Locating the process that has a specific network port open

Ever tried to start a process on Windows, only to be told that the network port it needs is unavailable?

TNSLSNR for 32-bit Windows: Version 10.2.0.1.0 - Production
Error listening on: (DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=mypc.oracle.com)(PORT=1521)))
TNS-12542: TNS:address already in use

Bugger. Now, which process is using that port? The task manager doesn't show you any of these sorts of details.

There's two ways I know of to work out which process which already has the port open.

1. If you have cygwin or MKS or some Unix styled set of command utilities -and who using Windows commands shells wouldn't? -- then you can use the grep and ps commands it provides with the standard netstat command.

First off, use the netstat command with the -ao switch and pipe it into grep to locate the listening details. The -o switch tells netstat to output the process-id of the owner of the port.

D:\>netstat -ao | grep 1521
TCP mypc:1521 mypc:0 LISTENING 2944

Then using the ps command, search for the process-id:

D:\>ps -eaf | grep 2944
SYSTEM 2944 1488 0 11:37:44 CONIN$ 0:00 vmware-authd

This could be combined into a nice awk script I suspect.

2. Use the very handy SysInternals utilities to locate the owning process. These used to be available on the sysinternals.com website, but the site now redirects to Microsoft, so presumably they have purchased them. Good idea Microsoft.

The specific utility for this operation is TCPView:

http://www.microsoft.com/technet/sysinternals/utilities/TcpView.mspx

This comes in two flavours, GUI and cmd line and identifies each process that is using a network port. The GUI is easy to use, just fire it up, look for the port in question which then identifies the process in the process table.

I prefer using the command line utility, tcpvcon.exe.

This results in a nicely formatted text output that shows the process and network port details for all processes that have a network port in use in some manner.

D:\>tcpvcon.exe -a
...
[TCP] vmware-authd.exe
PID: 2944
State: ESTABLISHED
Local: mypc:1521
Remote: mypc:0
...

To make it easier to identify a specific process which owns a specific port, then you can ask the output to be provided in CSV format, which lists each process on a single line, and which you can then pipe into find to locate a specific port

D:\>tcpvcon.exe -c -a | find "1521"
TCP,VMNET.EXE,2944,LISTENING,mypc:1521,mypc:0

----------------
Listening to: John Butler Trio - Good Excuse

11 October 2007

OC4J 10.1.3.3 Ant Taskname Error

OC4J 10.1.3.x supplies a set of Ant tasks that enable operations such as application deployment, resource configuration and server lifecycle tasks to be performed.

The abridged history of the tasks is:
  • OC4J 10.1.3.0 introduced the basic tasks
  • OC4J 10.1.3.1 and later versions extends the set of tasks to support JDBC/JMS resource configuration options and server shutdown/restart tasks
The OC4J Configuration and Administration guide documents the Ant tasks.

However in the OC4J 10.1.3.3 release several of the tasks are incorrectly defined in the antlib.xml and therefore not available using the names as described in the documentation.

The documentation is correct and shows how the tasks should be named. However if you use these tasks as documented you will get errors. The screen capture below highlights the specific differences.



The workarounds are either:

1. Revert to using the names as defined in antlib.xml for these tasks:

  • addDataSourceConnectionPool == createJDBCConnectionPool
  • addManagedDataSource == createManagedDataSource
  • addNativeDataSource == createNativeDataSource
  • testDataSourceConnectionPool == testConnectionPool
2. Update the /j2ee/utilities/ant-oracle-classes.jar!/oracle/antlib.xml to change the names of the tasks so they reflect the correct names as specified in the documentation:

Change: taskdef name="createJDBCConnectionPool"
To: taskdef name="addDataSourceConnectionPool"

Change: taskdef name="createManagedDataSource"
To: taskdef name="addManagedDataSource"

Change: taskdef name="addManagedDataSource"
To: taskdef name="createNativeDataSource"

Change: taskdef name="testConnectionPool"
To: taskdef name="testDataSourceConnectionPool"

A bug has been filed for this and a patch will be released which fixes the issue along the lines of option 2 shown above.

08 October 2007

Bring on UST + Stans

My Turner Flux is shod with a Mavic Crossmax SL wheelset which is UST compatible. I've been riding it with tubes + Maxxis Crossmarks (which I think are awesome) so far since I've been a bit slow to pick up some UST tires -- I'm commerced out I think. Plus I also figured with all the other knobs and dials to tweak, I had enough new stuff to worry about.

But last Friday some new Maxxis Crossmax LUSTs I'd purchased arrived. I was hoping to put them on over the but I didn't quite get a chance to do.

And as fate or that little Irish Murphy bloke would have it, my early Monday morning blast around the Eagle MTB park resulted in a pinch flat just at the end of the SouthSide trail.

I didn't have a spare tube, but thought I was lucky that I had some patches. But of course that little Murphy bloke, smiling ever more cunningly at this stage, somehow conspired to make the CO2 cartridge I carry to refill a tube be empty.

Bugger, a 2km walk, mostly uphill back to the car.

Yes, it's my fault, I know. Always carry a spare tube and a full canister. And if you use the spare tube, make sure you buy another one to replace it. And check the canister occasionally.

The good thing is this means I now have a hard requirement to put on the USTs. I picked up some Stans sealant from Pete at BMCR, so hopefully I won't be suffering too many more punctures.

But I'll will carry a spare tube just in case.

Update: Job done!

I just spent an short while out in the shed fitting the UST tires to the wheels. It's pretty straightforward to do. I think the best piece of advice I received was to do the initial inflation using an air compressor, to get enough volume into the tire to push the beads into place in the rim. The beauty of the Mavic wheelset I discovered is that they provide you with a schraeder screw-on adapter, which lets you use the standard car tire type fitting to pump out the tire. Once the tire is seated, deflate it and make sure there are no points around the rim where the bead has come away. Once you are satisfied pump it back up to the desired using a floor pump. Easy!

Adding Stans was easy too -- just read the direction and make sure you follow the inversion tips to ensure the micro particles are in the sealant portion being added to the tire. Unset the bead from a small portion of the tire, tip in the sealant, reseat and you are done.

Total time was about 30 mins for the first tire, with a bit of mucking around and working out how to do it. The next tire took about 5 minutes to do.

Should be able to get out for a few trails this weekend, I'll update this note when I see how they ride.

Update: Stans 1, Blue Gums 0

Out on my early monday morning ride, cruising across the top of the Blue Gums trail -- which is quite rocky -- I heard a noise that I thought sounded like I had something stuck in my spokes. It went away before I could stop to check it out. When I did stop, I saw a white patch on my tire. Ahh, Stans to the rescue. I must have punctured on a sharp rock and Stans sealed it up almost immediately. I lost a little pressure, but it was still very rideable and got me back to the car easily. Sure beats walking. Pumped it back up at the car and there was no further leakage.

Rock on Stan!

28 September 2007

Using JAXB 2.0 with OC4J 10.1.3.x

OC4J 10.1.3.x provides a JAXB 1.0 implementation as part of its standard runtime. If you want to use JAXB 2.x then its easy to do using the shared-library mechanism of OC4J.

As a simple test of the JAXB 2.0 capabilities, a simple application can be used that makes use of the JAXB 2.0 features. In the example used herein, the application is a simple Web application that makes use of two POJOs that are annotated with JAXB 2.0 annotations.

Build a Test Application:
package sab.demo.jaxb.model;

import java.util.ArrayList;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.annotation.*;

/**
*
@author sbutton
*
*/
@XmlType(propOrder = { "manufacturer", "model", "year",
"color"
, "registration", "owners" })
@XmlRootElement(name = "vehicle")
@XmlAccessorType(XmlAccessType.PROPERTY)
public class Vehicle {
long id;
String registration;
String manufacturer;
String model;
String year;
String color;
ArrayList<Owner> owners = new ArrayList<Owner>();

public Vehicle() {
// TODO Auto-generated constructor stub
}

...
...
}

Instances of the POJOS are then created from a JSP page, where they are marshalled into an XML document and then displayed by the JSP:
<%
try {
JAXBContext context = JAXBContext.newInstance(Vehicle.class);
Marshaller marshaller = context.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);

Vehicle xtrail = new Vehicle(1L, "Nissan", "X-Trail",
"2002"
, "Silver", "ABC-123");
xtrail.addOwner(new Owner(1L, "Fred", "Bloggs"));

Vehicle golf = new Vehicle(2L, "Volkswagen", "Golf",
"2002"
, "Silver", "XYZ-123");
golf.addOwner(new Owner(1L, "Fred", "Bloggs"));
golf.addOwner(new Owner(2L, "Freda", "Bloggs"));

StringWriter sw = new StringWriter();
marshaller.marshal(xtrail,sw);
out.println(escapeHtml(sw.toString()));

sw = new StringWriter();
marshaller.marshal(golf,sw);
out.println(escapeHtml(sw.toString()));

} catch (JAXBException e) {
e.printStackTrace(new PrintWriter(out));
}
%>
The application is then packaged up into a WAR and an EAR file ready for deployment.

Prepare the Server: Using ASC, publish a shared-library that contains the JAXB 2.0 JAR files.



Deploy the Application: When deploying the application, use the Classloading Task and import the "jaxb 2.0" shared-library so the application has access to the JAXB 2.0 implementation.



When the application is run, the Vehicle and Owner instances are marshalled to XML and displayed in the JSP page.



Voila.

20 September 2007

Command Line Monitoring of Thread Pool Sizes

Want to periodically view the OC4J thread pool sizes from the command line?

Using Groovy + JMX , its dead simple:
import demo.oc4j.jmx.*;
import java.text.*;

def period = 5000L
def client = new OC4JClient()
def df = DateFormat.getTimeInstance(DateFormat.LONG)

try {
client.connect("service:jmx:rmi://10.187.112.178:23791",
"oc4jadmin", "welcome1")

println "$client\n"

http = client.helper.createGroovyMBean(
"oc4j:j2eeType=ThreadPool,name=http,J2EEServer=standalone")

system = client.helper.createGroovyMBean(
"oc4j:j2eeType=ThreadPool,name=system,J2EEServer=standalone")
jca=client.helper.createGroovyMBean(
"oc4j:j2eeType=ThreadPool,name=jca,J2EEServer=standalone")


while(true) {
now = df.format(new Date(System.currentTimeMillis()));
println "$now HTTP:[$http.poolSize] JCA:[$jca.poolSize] System:[$system.poolSize]"
Thread.sleep(period)
}
} finally {
println 'Closing'
client.close()
}

This makes use of the OC4J helper library, which you can access here.

A simple script to run this would look like this:

@setlocal
set J2EE_HOME=d:\java\oc4j-10133-prod\j2ee\home
set GROOVY_HOME=d:\java\groovy-1.0
set classpath=lib\oc4jgroovy.jar;%GROOVY_HOME%\lib\commons-cli-1.0.jar;/
%GROOVY_HOME%\embeddable\groovy-all-1.0.jar;%J2EE_HOME%\admin_client.jar;%J2EE_HOME%\oc4jclient.jar

java -classpath %CLASSPATH% groovy.lang.GroovyShell src\threadlevel.groovy
@endlocal


When the script is run, it produces this sort of output:

Client is connected to: rmi://10.187.112.178:23791 oc4jadmin [connectionCaching:
true, httpTunneling:false, locale:not set]

1:03:54 HTTP:[7] JCA:[1] System:[7]
1:03:59 HTTP:[7] JCA:[1] System:[7]
1:04:06 HTTP:[7] JCA:[1] System:[7]
1:04:11 HTTP:[7] JCA:[1] System:[7]


----------------
Listening to: Ned's Atomic Dustbin - What Gives My Son?

13 September 2007

Remote copy with SCP and JDeveloper External Tools

I'm currently working on developing some examples for use cases for our Release 11 product. I tend to flick around between the IDEs I use, Eclipse and JDeveloper -- for example, I find that the support JDeveloper has for packaging J2EE applications is a bit easier to use than the analogs in Eclipse. Or perhaps its just that I've used it a bit more and know how to work around its foibles.

Anyway, the situation I ran into is that my install of 11AS is on a hosted server back at HQ. And I'm using JDeveloper 10.1.3 on my laptop here in Adelaide. So any time I wanted to deploy the applications to test them, I had to manually SCP/SFTP the application archive up to the server then deploy it. I know its trivial, but it was becoming a pain in the arse after a while. It's kind of funny how something so incidental sometimes becomes a beast of burden.

How could I make my life easier? Well I was just about to write myself a 5 line Ant script to do it, which used the task and then run that from JDeveloper to automate the copy process. But then I just happened to notice the "External Tools" menu item.
Hmm I said out loud -- could I just use that instead?

Well all you need to do is crack open the External Tools menu item and Add a new item.

To copy a file from my local PC to the server, I ultimately need a command that looks like this :

pscp -pw password username@server:/dir

All you need to do then is to fill in the relevant fields to produce that command.

There are some nice elements in the dialog that help you construct the generic command. I used three of these in my command:
  1. To allow me to enter the password when the command is run, I used the prompt directive:

    ${prompt:label=Password}

  2. To specify the file to upload, I used the file.path directive:

    ${file.path}

  3. To specify the target server, I used an environment variable substitution. I'm lazy and to avoid typing, I set the common target server details as an environment variable so that from the command line I can use some shorthand like this "pscp %SCP% ...". To include the environment variable, I used the env.var directive:

    ${env:var=SCP}
In the external tool dialog, putting it together looks like this:



Cool.

Now to put it to use.

When I want to copy an application archive up to my server for deployment, all I need to do is select the archive in the Navigator and select PSCP from the right mouse menu:



The password is then queried:



And finally the full command is constructed and run:



Voila.

One gotcha I found was that the deployment archives are not shown by default in the various JDeveloper Navigators (why it doesn't show them in a "deploy" directory in the navigator BTSOOM).

So to force JDeveloper to show you the generated archives, add the "deploy" directory to the Project Content directories:



----------------
Listening to: Ride - Twisterella

10 September 2007

Using Shared Libraries to configure Log4j

As I was mucking around with Log4j last week, it occurred to me that I could make use of the OC4J shared-library mechanism to inject the Log4J properties files into an an application when it was being deployed -- after all the properties file is just read from the classpath.

Even better, what this enables an administrator to do is to configure a set of shared-libraries that contain different log4j properties file, say enabling different log levels, and then choose between them when the application is deployed. Or additionally, make changes to in a post-deployment manner to switch between different logging settings.

OK, enough with the banal description, here's a few screen shots to show you what I'm dribbling on about here.

First off, lets assume you have a desire to capture and route your log4j entries into the OC4J log system using the OracleAppender as described here and you have a properties file that configures the appropriate settings and the log level you want to enable.

log4j.rootLogger=INFO,OJDL
log4j.appender.OJDL=oracle.core.ojdl.log4j.OracleAppender
log4j.appender.OJDL.LogDirectory=${oracle.j2ee.home}/log/oc4j
#log4j.appender.APP1.MaxSize=1000000
#log4j.appender.APP1.MaxSegmentSize=200000
#log4j.appender.APP1.Encoding=iso-8859-1
log4j.appender.OJDL.ComponentId=OracleProd

The first thing to do is to put the log4j.properties file into a JAR file, then deploy it as a shared-library to OC4J.





Once the shared-library has been published, you will see it available on the server shared-libraries page, ready to be imported. Note that in the below, I actually have two different log4j.config shared-libraries deployed.



Once the shared-library is deployed, it is then available to be imported by applications when they are deployed. By importing the log4j.info.config shared-library, the log4j.properties file is made accessible to the application and therefore used to to configure log4j for the application.

To import the shared-library during the deployment process, use the Configure Classloading button on the Deployment Tasks page.





By selecting the desired log4j.config shared-library, it will be made available to the application, and therefore dictate how the log4j log entries for the application are handled. In this specific case, the ROOT logger is set to the INFO level, and the OracleAppender is being employed to direct the log entries into the OC4J log system.

Ultimately the customized shared-library settings for the application are written into the orion-application.xml for the deployed application.



If at some point you wanted to change this application to use a lower log level such as DEBUG or TRACE then you can easily modify the import-shared-library statement to import the shared-library that has the relevant log4j.properties configuration file.

In summary, using the Oc4J shared-library mechanism and a consistent naming convention should enable you to have as many reusable log4j configurations as you need that can be applied to your applications.

----------------
Listening to: Billy Bragg - A New England