Friday 30 August 2013

JBoss AS 7.1.1.Final Administration. MySQL DataSource creation

Jboss AS 7.1.1 MySQL configuration

This article tries to explain how to create a new DataSource managed by the JEE-container.

The following steps must be followed in order to make available the MySql database system to persist data from beans deployed in the EBJ-Container.

Several steps are needed:

1) Download the MySql jdbc driver. In this case , the version that has been taken into account is the 5.1.26.

2) Add the new DataSource modifying the standalone.xml configuration file located in JBOSS_HOME/standalone/configuration. For doing so, add this elemental datasource element

<datasource jndi-name="java:/PruebaDS" pool-name="PruebaDS">

    <connection-url>jdbc:mysql://localhost:3306/greetingdb</connection-url>

            <driver>mysql</driver>

                    <security>

                        <user-name>root</user-name>

             </security>

 </datasource>

 Elemental explanations : the datasource is accessed by container-managed beans or external clients via JNDI. His name is “java:/PruebaDS”.

<connection-url> : It is the connection string needed if you want mysql to be your persistence database system. In this case, my database is called “greetingdb”.

<driver>: it refers to the .jar file that is installed as a JBOSS module (see later).

<security>: It specify the user of the database.

As well, the driver reference must be reflected in the previous file. 

<driver name="mysql" module="com.mysql"/>

The whole section of the file defining this new datasource is the following:

<subsystem xmlns="urn:jboss:domain:datasources:1.0">

            <datasources>

                <datasource jndi-name="java:jboss/datasources/ExampleDS" pool-name="ExampleDS" enabled="true" use-java-context="true">

                    <connection-url>jdbc:h2:mem:test;DB_CLOSE_DELAY=-1</connection-url>

                    <driver>h2</driver>

                    <security>

                        <user-name>sa</user-name>

                        <password>sa</password>

                    </security>

                </datasource>

                <datasource jndi-name="java:/PruebaDS" pool-name="PruebaDS">

                    <connection-url>jdbc:mysql://localhost:3306/greetingdb</connection-url>

                    <driver>mysql</driver>

                    <security>

                        <user-name>root</user-name>

                    </security>

                </datasource>

                <drivers>

                    <driver name="h2" module="com.h2database.h2">

                        <xa-datasource-class>org.h2.jdbcx.JdbcDataSource</xa-datasource-class>

                    </driver>

                    <driver name="mysql" module="com.mysql"/>

                </drivers>

            </datasources>

        </subsystem>

 

You can see two datasource defined with their respective drivers.

3) Now, it is the moment to install the new module in the JBOSS Application Server. It is quite simple. The steps are the following:

·         In the JBOSS_HOME/modules create the file structure /com/mysql/main (JBOSS_HOME/modules/com/mysql/main/

·         In this directory, copy the jar previously downloaded.

·         Create a file called module.xml and insert the following content:

<module xmlns="urn:jboss:module:1.0" name="com.mysql">

   <resources>

     <resource-root path="mysql-connector-java-5.1.26-bin.jar"/>

   </resources>

   <dependencies>

      <module name="javax.api"/>

      <module name="javax.transaction.api"/>

    </dependencies>

</module>

This file refers the jar file and the dependencies needed to make it available.

4) Start the JBOSS server and check if the datasource is well deployed:

 

13:44:20,984 INFO  [org.jboss.as.connector.subsystems.datasources] (MSC service thread 1-4) JBAS010400: Bound data source [java:/PruebaDS]

Some like this you might find in the logging trace of the server once it is run.

 

 

 

Thursday 29 August 2013

EJB 3.0, JBoss 7.1.1 and JNDI-based client (1)

Probably, you have suffered a lot searching for the best way of invoking a EBJ 3.1 hosted in a JBoss AS 7.1.

This article wants only to show you the steps to launch a simple “ Hello World” EJB and the way of develop an elemental EBJ client accessing via JNDI.

The plan is to create a Stateless EJB Maven-based project. In this occasion I rule out a Bean with state because this is a very delicate issue in terms of design and performance. I tend to have as first option beans without state.

Moreover, the invocation in this example is thought as a Remote one, despite the lack of performance given that there are serialization operations involved in the business request process.

JNDI will be the mechanism to resolve this issue.

Finally, I consider the JBoss Application server running as a standalone node.

These are the steps in order to create a basic bean and deploy it a Jboss instance.

1) Firstly, create a Remote-accesing bean with just one method that returns the message the client provides.

2) Desploy it in the JBoss Application server.

3) Create the client based on JNDI outlining each element is needed.

4) Execute the client.

 

1) CREATION OF A SINGLE REMOTE-ACCESED Bean (Stateless one)

Here you see the code of a simple EJB without state.

The remote interface must be defined.

package com.greeting.ejb;

import javax.ejb.Remote;

 

@Remote

public interface HelloWorldGreeting {

      String getGreeting(String msg);

}

Here it is the bean implementing the Remote interface and is defined without state.

 

package com.greeting.ejb.bean;

 

import com.greeting.ejb.HelloWorldGreeting;

import javax.ejb.Stateless;

import javax.ejb.EJB;

import com.greeting.ejb.SpanishGreetingTranslator;

@Stateless

public class HelloWorldGreetingBean implements HelloWorldGreeting {

 

      @EJB SpanishGreetingTranslator translator;

     

      public String getGreeting(String msg) {

            return translator.translate(msg);

 

      }

The project is a maven-based one, and the pom.xml I have used is the following. It is interesting because the file shows the dependencies we need to develop the bean.

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">

  <modelVersion>4.0.0</modelVersion>

  <groupId>com.greeting</groupId>

  <artifactId>greetingejb3</artifactId>

  <version>0.0.1-SNAPSHOT</version>

  <name>greetingejb3</name>

       <build>

             <plugins>

                    <plugin>

                    <artifactId>maven-compiler-plugin</artifactId> 

                           <configuration>  

                    <source>1.6 </source>

                      <target>1.6</target>  

                           </configuration>  

                    </plugin>  

            

             </plugins>

       </build>

       <repositories> 

        <repository> 

            <id>jboss-repository</id> 

            <name>JBoss Repository</name> 

            <url>https://repository.jboss.org/nexus/content/groups/public/</url> 

         </repository>

         <repository> 

            <id>Repo maiven</id> 

            <name>Maven2</name> 

            <url>http://repo1.maven.org/maven2/</url> 

         </repository> 

    </repositories> 

   <dependencies> 

      

        <dependency>

             <groupId>jboss</groupId>

                    <artifactId>jboss-ejb-api</artifactId> 

                    <version>7.1.1.Final</version> 

                    <scope>provided</scope> 

       </dependency>

        

       <dependency> 

                    <groupId>junit</groupId> 

             <artifactId>junit</artifactId> 

             <version>4.10</version> 

             <scope>test</scope> 

       </dependency> 

       <dependency>

             <groupId>org.jboss.spec.javax.ejb</groupId>

                    <artifactId>jboss-ejb-api_3.1_spec</artifactId> 

                    <version>1.0.2.Final</version> 

       </dependency>

        

  </dependencies> 

</project>

Two Annotations has been used: @Remote and @Stateless. Be aware that the bean has injected other bean managed by the container (@EJB SpanishGreetingTranslator translator; ). At this point, please, do not pay attention to this detail.

 The following step is to package and deploy this Bean in the JBoss application server. For doing both these steps:

                1) To compile the code and then, package it as JAR file. It is important that the file description called ejb-jar.xml is not needed if the we use annotations. I generate the jar file named as greetingejb3-0.0.1-SNAPSHOT.jar

                2) To deploy in JBoss the jar file to have ready the EJB called HelloWorldGreetingBean. For doing this, copy the jar file into the jboss directory: JBOSS_HOME\standalone\deployments

You can see something like this in the JBoss console.

Great!! Our Bean has been deployed properly. Time to go for our client.

2) CREATION OF THE CLIENT

To create the client several stages must be taken into account:

1) Dependencies needed to ejecute the cliente property.

In this case the following list that you can find in the JBOSS:HOME\modules directory must be include in the CLASSPATH of the client project.

..\jboss-as-7.1.1.Final\modules\org\jboss\ejb-client\main\jboss-ejb-client-1.0.5.Final.jar
..\jboss-as-7.1.1.Final\modules\org\jboss\ejb3\main\jboss-ejb3-ext-api-2.0.0.jar
..\jboss-as-7.1.1.Final\modules\org\jboss\logging\main\jboss-logging-3.1.0.GA.jar
..\jboss-as-7.1.1.Final\modules\org\jboss\xnio\main\xnio-api-3.0.3.GA.jar
..\jboss-as-7.1.1.Final\modules\org\jboss\sasl\main\jboss-sasl-1.0.0.Final.jar
..\jboss-as-7.1.1.Final\modules\org\jboss\marshalling\river\main\jboss-marshalling-river-1.3.11.GA.jar
..\jboss-as-7.1.1.Final\modules\org\jboss\marshalling\main\jboss-marshalling-1.3.11.GA.jar
..\jboss-as-7.1.1.Final\modules\javax\ejb\api\main\jboss-ejb-api_3.1_spec-1.0.1.Final.jar
..\jboss-as-7.1.1.Final\modules\org\jboss\remoting3\main\jboss-remoting-3.2.3.GA.jar
..\jboss-as-7.1.1.Final\modules\org\jboss\xnio\nio\main\xnio-nio-3.0.3.GA.jar
..\jboss-as-7.1.1.Final\modules\javax\transaction\api\main\jboss-transaction-api_1.1_spec-1.0.0.Final.jar

2) A file named jboss-ejb-client.properties must be in the CLASSPATH of the project as well, with the following content:

endpoint.name=client-endpoint

remote.connectionprovider.create.options.org.xnio.Options.SSL_ENABLED=false

 

remote.connections=default

 

remote.connection.default.host=localhost

remote.connection.default.port = 4447

remote.connection.default.connect.options.org.xnio.Options.SASL_POLICY_NOANONYMOUS=false

 

remote.connection.default.username=ejb

remote.connection.default.password=test

Be aware that the user and password must be added to the JBOSS as Application user (no described here)

Once all elements are added, here you are the client code:

import javax.naming.*;

import java.util.*;

import com.greeting.*;

import com.greeting.ejb.*;

public class GreetingClient1 {

 

      

       public static void main(String[] args) throws Exception{

            

             Hashtable jndiProperties = new Hashtable();

        jndiProperties.put(Context.URL_PKG_PREFIXES, "org.jboss.ejb.client.naming");

       

        Context context = new InitialContext(jndiProperties);

             String ejbString="ejb:/greetingejb3-0.0.1-SNAPSHOT//HelloWorldGreetingBean!com.greeting.ejb.HelloWorldGreeting";

       

        HelloWorldGreeting bean= (HelloWorldGreeting) context.lookup(ejbString);

        System.out.println (bean.getGreeting("hello"));

       

 

      }

 

}

 

The most important topic in this point is how we call the EJB via JNDI. The string used to do so has the following format:

ejb:<app-name>/<module-name>/<distinct-name>/<bean-name>!<fully-qualified-classname-of-the-remote-interface>

app-name: the EAR name. In this case is not necessary given that our artifact is a .jar file.

module-name: name of the .jar file without extension.

distinct-name: Given that out deployment has not a distinct name, this filed is not necessary.

bean-name: the name of our bean.

<f-q-c-o-t-r-i> the qualified name of the Remote interface.

Our string is the following:

ejb:/greetingejb3-0.0.1-SNAPSHOT//HelloWorldGreetingBean!com.greeting.ejb.HelloWorldGreeting";

 

Finally, execute the client and we obtain the result required.