Cliente EJB 3.0 con Spring

1. Configuramos el proyecto, pom.xml

<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/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.wordpress.modprobe</groupId>
  <artifactId>hello-client</artifactId>
  <version>1.0-SNAPSHOT</version>
  <name>hello-client</name>
  <description>Hello client, hello world using spring and ejb 3.0</description>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>

  <dependencies>
  	<dependency>
	   <groupId>org.springframework</groupId>
	   <artifactId>spring</artifactId>
	   <version>2.5.6</version>
	</dependency>	
	<dependency>
	   	<groupId>org.apache.openejb</groupId>
	   	<artifactId>openejb-client</artifactId>
	   	<version>3.0</version>	   	
	</dependency> 
	<dependency>
		 <groupId>com.wordpress.modprobe</groupId>
		 <artifactId>hello-ejb</artifactId>
		 <version>0.0.1-SNAPSHOT</version>
	</dependency>

  </dependencies>

  <build>
    <plugins>
      <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>exec-maven-plugin</artifactId>
        <executions>
          <execution>
            <goals>
              <goal>java</goal>
            </goals>
          </execution>
        </executions>
        <configuration>
          <mainClass>com.wordpress.modprobe.Main</mainClass>
        </configuration>
      </plugin>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>2.3.2</version>
        <configuration>
          <source>1.6</source>
          <target>1.6</target>
          <encoding>UTF-8</encoding>
        </configuration>
      </plugin>

      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-jar-plugin</artifactId>
        <configuration>
          <archive>
            <manifest>
              <addClasspath>true</addClasspath>
              <mainClass>com.wordpress.modprobe.Main</mainClass>
            </manifest>
          </archive>
        </configuration>
      </plugin>

    </plugins>

  </build>

</project>

 

2. Escribimos el cliente

package com.wordpress.modprobe.client;

import com.wordpress.modprobe.ejb.HelloService;

public class HelloSpringClient {

	public HelloSpringClient(){}

	private HelloService service;

	public void setService(HelloService service) {
		this.service = service;
	}

	public String sayHello(String name){
		String retVal = service.hello(name);
		return retVal;
	}

}

3. Creamos el archivo de configuración para Spring

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:jee="http://www.springframework.org/schema/jee"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
         http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
         http://www.springframework.org/schema/jee
         http://www.springframework.org/schema/jee/spring-jee-2.5.xsd">

    <jee:jndi-lookup id="helloService" jndi-name="HelloServiceBeanRemote">
         <jee:environment>
             java.naming.factory.initial=org.apache.openejb.client.RemoteInitialContextFactory
             java.naming.provider.url=ejbd://localhost:4201
         </jee:environment>
    </jee:jndi-lookup>

    <bean id="client" class="com.wordpress.modprobe.client.HelloSpringClient" scope="singleton">
    	<property name="service" ref="helloService" />        		
    </bean>

</beans>

4. Crear la clase main

package com.wordpress.modprobe;

import java.io.Console;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;

import com.wordpress.modprobe.client.HelloSpringClient;

public class Main {

	public static void main(String[] args){										
		BeanFactory springFactory = new XmlBeanFactory(new ClassPathResource("application-Context.xml"));
		HelloSpringClient client = (HelloSpringClient)springFactory.getBean("client");

		System.out.println();
		System.out.print("Pon tu nombre: ");
		Console console = System.console();
		String readLine = console.readLine();

		String response = client.sayHello(readLine);

		System.out.println("EJB dice: "+response);

	}

}

5. Correr el ejecutable mvn exec:java
https://modprobe.files.wordpress.com/2010/11/hello_ejb3-0_spring_openrjb-zip.docx

Hello – world EJB 3.0

Ejemplo muy sencillo de EJB 3.0 usando un bean de sesión stateless

 1. Bajamos OpenEJB 3.0 

 2. Escribimos la interfaz remota.

package info.modprobe.ejb;

import javax.ejb.Remote;

@Remote
public interface HelloService{

	String hello(String arg);
}

 3. Escibimos el bean

package info.modprobe.ejb;

import javax.ejb.Stateless;

@Stateless
public class HelloServiceBean implements HelloService {

	public String hello(String arg) {
		return "hola "+arg;
	}

}

 4. Creamos un jar, bueno, que lo haga maven…mvn package….veamos el pom.xml

  4.0.0

  info.modprobe
  hello-ejb
  0.0.1-SNAPSHOT
  ejb

  hello-ejb JEE5 EJB
  http://modprobe.info/2010/11/28/hello-world-ejb-3-0

      javaee
      javaee-api
      5
      provided

      junit
      junit
      3.8.1
      test

     <!-- for javaee:javaee-api -->
      java.net
      java.net
      http://download.java.net/maven/2/

        org.apache.maven.plugins
        maven-compiler-plugin
        2.0.2

          1.5
          1.5

        org.apache.maven.plugins
        maven-ejb-plugin
        2.1

          3.0

    hello-ejb

 5. Deployamos el jar, agregando lo siguiente casi al final del archivo openejb.xml

/home/fal/Desktop/openejb/workspace/hello-ejb/target/hello-ejb.jar

 6.Verificamos el log…….se deb ver algo como en openejb.log

2010-01-14 19:05:15,249 - INFO  - Deployed Application(path=/home/fal/Desktop/openejb/workspace/hello-ejb/target/hello-ejb.jar)



https://modprobe.files.wordpress.com/2010/11/hello_ejb3-0_spring_openrjb-zip.docx