Sunday, January 31, 2016

Setup for a simple EJB demo on Glassfish 4

I reviewed recently a very good serie of tutorials on JavaEE on the Coreservlet site. Though they all are excellent, they are a bit outdated, and I had to try a bit to get the simple example running.

I took the EJB 3 and Web Services tutorial, which describes setup for JBoss 5 and Glassfish. Using Eclipse Mars, I had no trouble to get the EJB - project deployed on Glassfish, and the Web client accessing this EJB.

The standalone client was missing a library, so I kept getting  javax.naming.NamingException: Lookup failed Exceptions when trying to access the remote service.

I searche the correct jndi configuration, but everything looked ok.

Finally, I found in this post in the Glassfish doc something that rang a bell: client libraries : my setup did not include the client libraries for Glassfish, so I added gf-client-module.jar from the Glassfish Libraries (under %GLASSFISH_HOME%/modules). Everything ran like a breeze.

My setup in short:

My client program consist of a single java file, where an initial context is initialized with a jndi.properties in the classpath (under ../src ).

java code for the context and service access:

...  
InitialContext context = new InitialContext();
  NumberService service = (NumberService)context.lookup("NumberCreator"); 
...

Content of jdni.properties:

java.naming.factory.initial=com.sun.enterprise.naming.SerialInitContextFactory
java.naming.factory.url.pkgs=com.sun.enterprise.naming
java.naming.factory.state=com.sun.corba.ee.impl.presentation.rmi.JNDIStateFactoryImpl
org.omg.CORBA.ORBInitialHost=localhost
org.omg.CORBA.ORBInitialPort=3700

 Beware that jndi.properties is specific to the server flavor: it is not the same for accessing a service on a JBoss server.

Of course, the service remote interfaces must also be accessible to the client program. Here is an example of one such interface:


package coreservlets.bean;

import javax.ejb.*;

@Remote
public interface NumberService {
  public double getNumber(double range);

}




Saturday, January 23, 2016

Correct usage of the re-render attribute in a JSF / richFaces page

I currently have to take care of a JSF application that causes us a bunch of troubles. Because of my lack of experience with Front-End components, I ran into a problem, that a careful study of the JSF - Documentation would have prevented.

Our JSF page has a radio button with 2 possible values (say "A" and "B"). Setting the value to "B" should trigger another component to be updated (on the view) - in this case, it's a boolean checkbox. On the JSF Bean, the value that is mapped to the checkbox is updated when the user selects "B" in the radio.

To make the Checkbox updated on the web page after selecting "B" in the radio button : just set the reRender attribute of the a4j - Tag of the radio button accordingly:


<h:selectOneRadio 
  id="myId" 
  immediate="true" 
  value="#{MyBean.myAttribute}">
<f:selectItem itemValue="A" itemLabel="A" />
<f:selectItem itemValue="B" itemLabel="B" />

<a4j:support event="onclick"
action="#{MyBean.myMethod}"
reRender="otherPanel,yetAnotherPanel" />