Freitag, 11. Juli 2008

A software quality metric for JBoss AS

When you thought about creating a global keyboard shortcut to restart your development JBoss, then software quality is not good enough!

link (0 Kommentare)   ... comment

Thema: EJB3


Mittwoch, 9. Juli 2008

EJB3: PostGIS and Hibernate

Ever dreamed of container managed persistence for your PostGIS database? Here's one solution.

PostGIS is a spatial extension to the PostgreSQL database which adds support for geographic objects and operations. Because of these new database types special treatment is needed when defining your persistent unit and generating your entity beans.

First of all you have to download the hibernate spatial extension libraries hibernate-spatial-1.0-M1.jar and hibernate-spatial-postgis-1.0-M1.jar from http://www.hibernatespatial.org. Additionally the Java Transaction Service library jts-1.8.jar (http://www.vividsolutions.com) is needed. Put all these JAR-files to your JBoss lib folder.

When defining your persistence unit your have to register the new hibernate dialect org.hibernatespatial.postgis.PostgisDialect, e.g.:

<?xml version="1.0" encoding="UTF-8"?>
<persistence
        version="1.0"
        xmlns="http://java.sun.com/xml/ns/persistence"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
                http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd ">
    <persistence-unit name="SpacialDatabasePU" transaction-type="JTA">
        <jta-data-source>java:/SpatialDatabaseJNDIName</jta-data-source>                                                                                                                                                                                                              
        <properties>
            <property name="hibernate.dialect"
                    value="org.hibernatespatial.postgis.PostgisDialect"/>
        </properties>
    </persistence-unit>
</persistence>

After generating your entity beans you will recognize that all of the non-standard database types are mapped to Object and are annotated with @Lob. Replace the Object type by the actual needed spatial type, e.g. com.vividsolutions.jts.geom.Geometry, and annote them with their hibernate spatial pendant, e.g. @Type(type = "org.hibernatespatial.postgis.PGGeometryUserType"). Don't forgett to modify getters and setters accordingly.

link (0 Kommentare)   ... comment

Thema: EJB3


Freitag, 4. Juli 2008

How to create several instances of one message driven bean with different configurations

Starting with EJB 3.0 you can write Message Driven Beans using Java 1.5 annotations without having to write an XML deployment descriptor file, e.g.:

@MessageDriven(activationConfig =  {
        @ActivationConfigProperty(propertyName = "destinationType",
            propertyValue = "javax.jms.Topic"),
        @ActivationConfigProperty(propertyName = "destination",
            propertyValue = "JMS/Topic"),
        @ActivationConfigProperty(propertyName = "messageSelector",
            propertyValue = "example = 'TestValue'"),
        @ActivationConfigProperty(propertyName = "acknowledgeMode",
            propertyValue = "Auto-acknowledge"),
        @ActivationConfigProperty(propertyName = "subscriptionDurability",
            propertyValue = "Durable"),
        @ActivationConfigProperty(propertyName = "clientId",
            propertyValue = "ExampleBean"),
        @ActivationConfigProperty(propertyName = "subscriptionName",
            propertyValue = "ExampleBean")
    })
public class ExampleBean implements MessageListener {
    // ...
}

You can create several instances of the same Bean class, e.g. to have several instances for different message selectors or to listen on more than one Topic. To achieve this, move the properties out of the annotations and put them into an XML deployment descriptor.

The class file reduces to:

    @MessageDriven
    public class ExampleBean implements MessageListener {
        // ...
    }

Now create a file ejb-jar.xml (“New ► Standard Deployment Descriptor” in Netbeans). It has to end up in the META-INF/ directory in the deployment .jar file (Netbeans automatically creates ant rules for this).

The outer frame is:

<?xml version="1.0" encoding="UTF-8"?>
<ejb-jar xmlns = "http://java.sun.com/xml/ns/javaee"
         version = "3.0"
         xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation = "http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/ejb-jar_3_0.xsd">;
    <enterprise-beans>
    </enterprise-beans>
</ejb-jar>

Inside the <enterprise-beans> tag, for each Bean instance create one <message-driven> section:

<message-driven>
    <ejb-name>AnotherExampleBean</ejb-name>
    <ejb-class>package.path.to.ExampleBean</ejb-class>
    <transaction-type>Container</transaction-type>
    <activation-config>
        
    </activation-config>
</message-driven>

Gotcha

You can choose the <ejb-name> for the instances freely, but one of the instances must be called exactly like the Bean class (ExampleBean in the example).

Inside <activation-config>, for each of the properties in the annotations above (destinationType etc.) create an <activation-config-property>:

<activation-config-property>
    <activation-config-property-name>destinationType</activation-config-property-name>
    <activation-config-property-value>JMS/Topic</activation-config-property-value>
</activation-config-property>

link (0 Kommentare)   ... comment

Thema: EJB3


Donnerstag, 3. Juli 2008

EJB3: Resource injection from the deployment descriptor

Enterprise Java Beans 3 (EJB3) cut back on the extensive use of a XML deployment descriptor. One useful application of such a descriptor is to “inject” values from this configuration file into member variables of a Bean object at deployment time.

Inside the bean use the @Resource annotation on the declaration of the variable you want to set:


@Resource(name="ExampleValue")
public String exampleValue = null;

Then in the deployment descriptor, inside the section of the Bean, create an <env-entry>:

<env-entry>
    <env-entry-name>ExampleValue</env-entry-name>
    <env-entry-type>java.lang.String</env-entry-type>
    <env-entry-value>Test</env-entry-value>
    <injection-target>
        <injection-target-class>package.path.to.ExampleBean</injection-target-class>
        <injection-target-name>ExampleValue</injection-target-name>
    </injection-target>
</env-entry>

For the <env-entry-type> you can choose one of the java.lang. wrappers around Java’s primitive types or java.lang.String.

Note that the container injects the value after object construction, so it is not yet available in the constructor.

If the resource is not configured or the names mismatch, a warning will be logged, but the Bean will start anyway. So check that the value is set programmatically, too.

link (0 Kommentare)   ... comment

Thema: EJB3