Sunday, December 14, 2008

Glassfish EJB3 remote interface on Linux problem

It's a problem struggles me for more than a year. Once I call a EJB Remote at Linux glassfish from any other machines, an strange exception will be thrown. The problem will only occur on Linux glassfish deployment. It was believed to be a bug as many other guys in glassfish forum). Here's the error messages:

"IOP00410201: (COMM_FAILURE) Connection failure: socketType: IIOP_CLEAR_TEXT; hostname: 127.0.0.1; port: 3700"
org.omg.CORBA.COMM_FAILURE: vmcid: SUN minor code: 201 completed: No
at com.sun.corba.ee.impl.logging.ORBUtilSystemException.connectFailure(ORBUtilSystemException.java:2690)
at com.sun.corba.ee.impl.logging.ORBUtilSystemException.connectFailure(ORBUtilSystemException.java:2711)
at com.sun.corba.ee.impl.transport.SocketOrChannelConnectionImpl.(SocketOrChannelConnectionImpl.java:261)
at com.sun.corba.ee.impl.transport.SocketOrChannelConnectionImpl.(SocketOrChannelConnectionImpl.java:274)


I come across this problem recently. After detail investigation, the problem is due of the Croba Connection on Linux Server resolve it's own ip address using the DNS name "localhost". The default Linux installation , /etc/host has the following line:
127.0.0.1 localhost

Then the client try to connect the server again using 127.0.0.1. The problem could be fixed by just modify the /etc/hosts file
{real ip address} localhost

Thursday, November 13, 2008

New Hiberinate Library Update on Glassfish

The Glassfish server has an update tool which could install updated application library. However, When I install the new hibernate 3.4 using the tool, the server fails to load up the application with the following error:
java.lang.NoSuchMethodError: org.objectweb.asm.ClassWriter.(Z)V

After some investigation, it placed a asm-3.1.jar in glassfish\lib which duplicated the original asm.jar. It encounters some class loading problem. It works fine after deleting the original asm.jar.

Tuesday, November 4, 2008

Using OpenEJB for unit testing with Hibernate on Netbeans

EJB3 has simplified EJB development a lot. However, it imposed new difficulties on out-of-container testing with the use of dependency injection. OpenEJB provides a way of to do EJB testing within J2Se environment in a lightweight container.

I am using Hibernate as the JPA implementation in current development, I would like to demonstrate how to test EJB under Netbeans 6.1 using OpenEJB.

Here are the steps:

1. Download OpenEJB from http://openejb.apache.org/, the current version is 3.1. Unzip the file, you may find all the required jar file under {somewhere to put the unzipped files}:\openejb-3.1\lib.

2. In Netbeans, under Tools --> Library --> new Library, create a library called openejb.

3. Assume that you have an existing EJB project with a test Stateless Session Bean as following:

@Stateless
public class MyTestBean implements MyTestLocal {

public String sayHello() {
System.out.println("hahahah");

return "haha";
}

}

Here's the persistence configuration:
<persistence-unit name="my-ejbPU" transaction-type="JTA">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<jta-data-source>myds</jta-data-source>
</persistence-unit>


4. Add ejb-jar.xml into configuration files of the EJB project as the followings:

<ejb-jar/>


The above declaration is used for telling the OpenEJB container to scan the jar file containing the EJBs and deploy it along with any annotated beans it may contain. http://openejb.apache.org/3.0/application-discovery-via-the-classpath.html

5. Create a J2SE project named testapp. Add the openejb and the EJB project as Library. If you use MySQL as the database, please add the mysql connectorJ library as well.

6. Create a unit test in the J2SE project.

public class NewJUnitTest extends TestCase {

private InitialContext initialContext;

public NewJUnitTest() {
}

@BeforeClass
public static void setUpClass() throws Exception {
}

@AfterClass
public static void tearDownClass() throws Exception {
}

@Before
public void setUp() {

//Init the OpenEJB Container
try {
Properties p = new Properties();
p.setProperty(Context.INITIAL_CONTEXT_FACTORY, "org.apache.openejb.client.LocalInitialContextFactory");
//Config the DataSource with MySQL
p.put("myds", "new://Resource?type=DataSource");
p.put("myds.JdbcDriver", "com.mysql.jdbc.Driver");
p.put("myds.JdbcUrl", "jdbc:mysql://localhost:3306/mydb");
p.setProperty("myds.UserName", "test");
p.setProperty("myds.Password", "test");

//It's specially for MySQL where hibernate.dialect must be explicitly set
p.put("my-ejbPU.hibernate.dialect", "org.hibernate.dialect.MySQLInnoDBDialect");

initialContext = new InitialContext(p);
} catch (NamingException ex) {

System.err.println(ex.getMessage());
}
}



//Here's the testing Code
@Test
public void testSayHello(){
try {

Object object = initialContext.lookup("MyTestBeanLocal");
assertNotNull(object);
assertTrue(object instanceof MyTestLocal);
MyTestLocal mt = (MyTestLocal) object;
assertEquals("haha",mt.sayHello());

} catch (NamingException ex) {
assertFalse(true);
System.err.println(ex.getMessage());
}
}

@After
public void tearDown() {
}

}


After all, you can test any Entity or SessionBeans in a J2SE environment. When you start the OpenEJB container in SetUp(), the Persistence Provider Implementation will check the entitiy mappings with Database. DBUnit could also be introduced if you would like to test Entity Beans.