Category Archives: glassfish

Java EJB passing string parameters from the front end.

Firstly, my apologies I’ve not been very active writing for the blog, I have been building a huge enterprise application at my work  that handles billions of records of data, it’s been keeping me VERY busy.

Recently I’ve been working on a project which has a “CRUD” module as part of it (to save me time), the software generates a complete Grid, editing forms, adding ‘thing’.

I had real problems because I wanted to pass a joined string parameter.

It was called from a prime faces menu.

<p:menuitem value="Edit counterfiet serial numbers" 
 ajax="true" 
 update="CRUDDLGDIALOG"
 actionListener="#{cRUD.init(
'dateofreport=timestamp,reportedby='+srs.thisUser.username))}" 
 onclick="CRUDDIALOG.show();"/>

Ofcourse this did not work, for some reason the backing bean thought that I was trying to add two numbers together… WHY????

So the solution, write a new function in the backing bean to join the two strings together.

<p:menuitem value="Edit counterfiet serial numbers" 
 ajax="true" 
 update="CRUDDLGDIALOG"
 actionListener="#{cRUD.init(
cRUD.stringJoin('dateofreport=timestamp,reportedby=',srs.thisUser.username))}"
 onclick="CRUDDIALOG.show();"/>

Here is the backing bean code. Note that I have two backing beans cRUD for managing the CRUD form and SRS which is a general purpose backing bean for the application. I definitely did not want to have one mixed into the other because it was very useful to have a CRUD system I could use when generating any EJB application.

public String stringJoin(String string1,String string2) {
 return string1.concat(string2);
 }
Advertisements

Building a processing application with EJB’s (don’t use THREADS!)

I have for some time had to build processing applications. These normally consist of thread based things that run off and do a lot of calculations and database work. These days however I use EJB’s and the @Asynchrounous tag above a routine to make it work asynchronously.

To make it work you need to create 2 stateless session beans.

One will hold your routines that you wish to run, lets call this runnerBean, you need to create a local interface to it.

Second, create the timer bean that will start off the threads in runner bean.

In the timerBean that starts all the threads off create an @Schedule routine and a reference to the runnerBean.

@EJB
private doRunnerLocal doRunner;

@Schedule(second = “*/5”, minute = “*”, hour = “*”, persistent = false)
private void importThreadManager() {

while (true) {

List serialNos=new ArrayList();

try {
serialNos.em.createNativeQuery(“select * from serialnos where processed<1”,SerialNos.class).getResultList();
} catch (Exception e) {

}

if (serialNos.isEmpty()) {
// Have a sleep for a while if nothing found
try {
Thread.sleep(10000);
} catch (InterruptedException x) {
}
} else {
for (i=0;i<serialNos.size();++i) {
doRunner.doSomething((String)serialNos.get(i));
}
}

}

In the runner bean then create your Async threads really simply.

@Asynchronous
public void doSomthing(String serialNo) {

// Process serial numbers

}

Job Done!

Creating ‘mini-caches’ in Java to speed up database calls.

Java has a cool construct called a Hashmap, this can be used to cache the result of a routine using a key.

For example.

Map cache=new HashMap();
public String getUnitsAtSite(String siteCode) {
     // is the sitecode already in the cache?
     if (cache.containsKey(siteCode)) return (String)cache.get(siteCode);
     String getSites=.... < get data from data base

     cache.put(siteCode,getSites);
     return getSites;

}

Now ofcourse there are downsides, if the data changes we won't see it - ofcourse we could do a cache.clear(); or cache.delete(key) on save of a change of record.

Installing Glassfish 3.1 ubuntu

Install Java first!

sudo su  
apt-get install unzip
wget http://dlc.sun.csom.edgesuite.net/glassfish/3.1/release/glassfish-3.1.zip
sudo unzip glassfish-3.1.zip
mv glassfish3 /opt
useradd --system glassfish -d /opt/glassfishv3
chgrp -R admin /opt/glassfishv3
cd /opt/glassfish3
chmod -R +x bin/
cd bin
./asadmin start-domain domain1
cd /etc/init.d
nano glassfish

(or you can use your favourite editor!)

GLASSFISHPATH=/opt/glassfish3/bin
echo "starting glassfish from $GLASSFISHPATH"
cd /opt/glassfish3/bin
./asadmin start-domain domain1

(PRESS CTRL X) Y (save)

sudo update-rc.d glassfish defaults
chmod +x glassfish

And reboot to check it comes up.

http://youripaddress:4848/

Gotchas!

OpenJDK doesn’t seem to work you need to use the Java SDK

If you install java you may find that there is no link to it, change to the bin folder.

cd /bin

and create a link

ln -s /usr/lib/jvm/java-6-sun/jre/bin/java java

Netbeans Problem “Enable-secure-admin”

Gotcha 2 is when trying to connect Netbeans to the server and you get some message about Enable-secure-admin here’s how to fix it

run asadmin on the glass fish server in terminal execute the following:

a) enable-secure-admin
b) stop-domain domain1
c) start-domain domain1
d) list-domains

See: http://download.oracle.com/docs/cd/E18930_01/html/821-2435/gkofl.html

accept the certificate. If you type in ‘y’

 

 

Another Glassfish Gotcha!

Logs again folks.

We had a big system we deployed, got 23 users in it ran like a DOG, updated Glassfish 2.1.1 still ran like a dog (but didn’t crash with permgen problems).

What was happening was simple the web stack was writing hundreds of INFO statements about combos and drop downs. I set all the logs to only report SEVERE errors and it all sped up!

A couple of Glassfish Gotchas!

First Gotcha!

Logs! I had a server pulling 5.2 million records and joining them together from a set of database servers. Ran fine for about 2 weeks then fell over. Could I get it to work? No, thats because Glassfish servers have their logs set to rotate at 200000 k or something strange, which means the server just simply ran out of disk space… it was a console based Solaris box so it didn’t TELL me what was wrong!

Connections to the Database Server – If you have a lot of users make sure your maximum connections are set high enough in the EJB, if not you’ll find it works fine with 5 users but slows down or even breaks if not!

 

My application won’t deploy!

You know one of those problems that makes you kick yourself when you find out the solution!

I’ve got this Solaris server running Glassfish, the application runs fine locally, but when deploying it just hangs.

I THOUGHT it was a problem with a slow network, so just sat there waiting but nothing – had the network checked as well. Then one of my guys was dealing with a different problem (Uthay!) and started talking about JDK versions.

The application I had was configured as JDK 5, so I changed it to JDK 6 and guess what – it deployed straight away.

THere are two morals to this story:-

a. Listen to what is going on around you if you work in a programming team as someone elses solution might be yours as well.
b. Make sure the version of the JDK you are running on the development environment MATCHES the one on the server!