jueves, 15 de mayo de 2014

Implementing an EJB as Web Service using jax-ws

Creando un EJB como un Web Service utilizando JAX-WS

Requerimientos:
  • JBoss 5.0.0.GA 
  • librerias JBoss WS native.

Vamos a copiar las siguientes librerias desde c:\server\jboss-5.0.0.GA\client\ y a c:\server\jboss-5.0.0.GA\lib\endorsed\
  • jbossws-native-jaxrpc.jar
  • jbossws-native-jaxws-ext.jar
  • jbossws-native-saaj.jar
  • jbossws-native-jaxws.jar

Luego, creamos el EJB y le agregamos la anotación @WebService

@WebService
@Stateless
public class BancoServiceImpl implements BancoService {
private final Logger logger = LoggerFactory.getLogger(this.getClass());
private final long SALDO_ESTATICO=1000000;
/* (non-Javadoc)
* @see com.gemalto.inlakech.spring.ejb.template.example.BancoService#consultarSaldo(java.lang.String)
*/
@Override
public long consultarSaldo(String cuenta){
logger.info("Entering to method with paraemeters[cuenta:"+cuenta+"]");
//TODO here business logic to get REAL balance
logger.info("The current balance for cuenta["+cuenta+"] is ["+SALDO_ESTATICO+"]");
return SALDO_ESTATICO;
}
}

@Remote
public interface BancoService {

public long consultarSaldo(String cuenta);
}

POM.XML

<dependencies>

<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.7</version>
<scope>test</scope>
</dependency>

<dependency>
   <groupId>org.mockito</groupId>
   <artifactId>mockito-all</artifactId>
   <version>1.9.5</version>
</dependency>
       
<dependency>
<groupId>org.mockejb</groupId>
<artifactId>mockejb</artifactId>
<version>0.6-beta2</version>
</dependency>

<dependency>
    <groupId>jboss</groupId>
    <artifactId>jboss-ejb-api</artifactId>
    <version>4.2.0.GA</version>
    <scope>provided</scope>
  </dependency>

<dependency>
<groupId>org.apache.openejb</groupId>
<artifactId>openejb-client</artifactId>
<version>4.0.0</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>${spring.version}</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>${spring.version}</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring.version}</version>
<scope>test</scope>
</dependency>

    <!-- slf4j dependencies for Logging -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${slf4j.version}</version>
</dependency>

<!-- logback dependencies -->
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>${logback.version}</version>
</dependency>

<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-core</artifactId>
<version>${logback.version}</version>
</dependency>

</dependencies>

Note: Existen dependencias de este POM que no son necesarias.

Y por ultimo, creamos un JAR utilizando mvn clean package y lo copiamos en c:\server\jboss-5.0.0.GA\server\default\deploy\

Para ver que el WS esta deployado correctamente, vamos a:

http://localhost:8099/jbossws/services


Para acceder al WSDL, vamos a:

http://localhost:8099/ejb-banco-service/BancoServiceImpl?wsdl

Utilizando el SOAP UI:



Creando un POJO como un Web Service utilizando JAX-WS

Deploying your ejb as webservice is not your only option: you can deploy a POJO as web service as well. In this case you just need to tag@WebService in a plain java class.

Pero para esto, vamos a necesitar armar un WAR y decir que el POJO sera un servlet



When you choose EJB over a POJO for a web service?

JAX-WS 2.0 allows both regular Java classes and stateless EJBs to be exposed as web services. If you
were using J2EE 1.4, you’re probably wondering why you’d use a stateless EJB as a web service. A
look at the code for a POJO and for EJB 3 web services reveals that there are hardly any differences,
with the exception that the EJB 3 web service will have a few extra annotations. A Java class web
service is packaged in a web module whereas an EJB web service is packaged in an EJB-JAR.
Both a Java web service and an EJB web service support dependency injection and lifecycle
methods such as @PostConstruct and @PreDestroy, but you get a few extra benefits from
using EJB 3 web services.

Leer: http://www.mastertheboss.com/jboss-web-services/jboss-web-services-part-1

No hay comentarios:

Publicar un comentario