Pessoal, estou estudando EJB com o JBoss 7.1. Atualmente estou tentando executar um exemplo bem parecido com esse aqui: https://docs.jboss.org/author/display/AS71/EJB+invocations+from+a+remote+client+using+JNDI. No entanto eu não estou tendo muito sucesso. Eu consegui fazer o deploy do EJB com sucesso, mas o cliente não funciona!
Segue os códigos:
Interface do EJB.
package br.com.teste;
import javax.ejb.Remote;
@Remote
public interface MatematicaRemote {
public Integer soma(Integer a, Integer b);
public Integer subtrai(Integer a, Integer b);
}
Implementação do EJB
package br.com.teste;
import javax.ejb.LocalBean;
import javax.ejb.Stateless;
import javax.ejb.TransactionManagement;
import javax.ejb.TransactionManagementType;
/**
* Session Bean implementation class Matematica
*/
@Stateless
@TransactionManagement(TransactionManagementType.BEAN)
@LocalBean
public class Matematica implements MatematicaRemote {
/**
* Default constructor.
*/
public Matematica() {
// TODO Auto-generated constructor stub
}
@Override
public Integer soma(Integer a, Integer b) {
return a + b;
}
@Override
public Integer subtrai(Integer a, Integer b) {
// TODO Auto-generated method stub
return a - b;
}
}
Log do deploy no JBoss 7.1:
13:53:53,800 Informações [org.jboss.modules] JBoss Modules version 1.1.0.CR6
13:53:54,245 INFO [org.jboss.msc] JBoss MSC version 1.0.1.GA
13:53:54,310 INFO [org.jboss.as] JBoss AS 7.1.0.CR1b "Flux Capacitor" starting
13:53:55,525 INFO [org.jboss.as] Creating http management service using socket-binding (management-http)
13:53:55,528 INFO [org.xnio] XNIO Version 3.0.0.CR7
13:53:55,548 INFO [org.xnio.nio] XNIO NIO Implementation Version 3.0.0.CR7
13:53:55,563 INFO [org.jboss.remoting] JBoss Remoting version 3.2.0.CR8
13:53:55,626 INFO [org.jboss.as.logging] JBAS011502: Removing bootstrap log handlers
13:53:55,637 INFO [org.jboss.as.security] (ServerService Thread Pool -- 44) Activating Security Subsystem
13:53:55,642 INFO [org.jboss.as.osgi] (ServerService Thread Pool -- 39) JBAS011910: Activating OSGi Subsystem
13:53:55,663 INFO [org.jboss.as.security] (MSC service thread 1-6) Picketbox version=4.0.6.Beta2
13:53:55,663 INFO [org.jboss.as.naming] (ServerService Thread Pool -- 38) JBAS011800: Activating Naming Subsystem
13:53:55,665 INFO [org.jboss.as.clustering] (ServerService Thread Pool -- 30) JBAS010300: Activating Infinispan subsystem.
13:53:55,738 INFO [org.jboss.as.webservices] (ServerService Thread Pool -- 48) JBAS015537: Activating WebServices Extension
13:53:55,740 INFO [org.jboss.as.connector.subsystems.datasources] (ServerService Thread Pool -- 26) JBAS010403: Deploying JDBC-compliant driver class org.h2.Driver (version 1.3)
13:53:55,755 INFO [org.jboss.as.connector] (MSC service thread 1-7) JBAS010408: Starting JCA Subsystem (JBoss IronJacamar 1.0.6.Final)
13:53:55,862 INFO [org.jboss.as.naming] (MSC service thread 1-7) JBAS011802: Starting Naming Service
13:53:55,863 INFO [org.jboss.as.jaxr] (MSC service thread 1-7) Binding JAXR ConnectionFactory: java:jboss/jaxr/ConnectionFactory
13:53:55,932 INFO [org.jboss.as.mail.extension] (MSC service thread 1-5) JBAS015400: Bound mail session [java:jboss/mail/Default]
13:53:57,025 INFO [org.jboss.ws.common.management.AbstractServerConfig] (MSC service thread 1-7) JBoss Web Services - Stack CXF Server 4.0.0.GA
13:53:57,366 INFO [org.jboss.as.remoting] (MSC service thread 1-5) Listening on /127.0.0.1:4447
13:53:57,450 INFO [org.apache.coyote.http11.Http11AprProtocol] (MSC service thread 1-5) Starting Coyote HTTP/1.1 on http--127.0.0.1-8080
13:53:57,485 INFO [org.jboss.as.server.deployment.scanner] (MSC service thread 1-3) JBAS015012: Started FileSystemDeploymentService for directory C:\Desenvolvimento\jboss-as-7.1.0.CR1b\standalone\deployments
13:53:57,509 INFO [org.jboss.as.remoting] (MSC service thread 1-1) Listening on /127.0.0.1:9999
13:53:57,616 INFO [org.jboss.as.connector.subsystems.datasources] (MSC service thread 1-7) JBAS010400: Bound data source [java:jboss/datasources/ExampleDS]
13:53:57,701 INFO [org.jboss.as.server.deployment] (MSC service thread 1-4) Starting deployment of "EJBTestes.ear"
13:53:57,778 INFO [org.jboss.as.server.deployment] (MSC service thread 1-4) Starting deployment of "EJBTestesEJB.jar"
13:53:57,956 INFO [org.jboss.as.ejb3.deployment.processors.EjbJndiBindingsDeploymentUnitProcessor] (MSC service thread 1-5) JNDI bindings for session bean named Matematica in deployment unit subdeployment "EJBTestesEJB.jar" of deployment "EJBTestes.ear" are as follows:
java:global/EJBTestes/EJBTestesEJB/Matematica!br.com.teste.Matematica
java:app/EJBTestesEJB/Matematica!br.com.teste.Matematica
java:module/Matematica!br.com.teste.Matematica
java:global/EJBTestes/EJBTestesEJB/Matematica!br.com.teste.MatematicaRemote
java:app/EJBTestesEJB/Matematica!br.com.teste.MatematicaRemote
java:module/Matematica!br.com.teste.MatematicaRemote
13:53:58,235 INFO [org.jboss.as] (MSC service thread 1-1) JBoss AS 7.1.0.CR1b "Flux Capacitor" started in 4680ms - Started 179 of 253 services (72 services are passive or on-demand)
13:53:58,290 INFO [org.jboss.as.server] (DeploymentScanner-threads - 2) JBAS018559: Deployed "EJBTestes.ear"
Agora vem o código J2SE que estou fazendo para consumir o EJB que já está no JBoss. É uma aplicação console bem simples que simplesmente invoca o EJB para realizar os cálculos de soma e subtração. No projeto desse cliente as únicas bibliotecas que adicionei no classpath foi as bibliotecas de Runtime do próprio JBoss 7.1. Eu estou usando o Eclipse Indigo com o plugin JBoss Tools instalado.
Segue os códigos do cliente e a exceção que ocorre quando eu tento executar:
Main:
import java.util.Hashtable;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import br.com.teste.Matematica;
import br.com.teste.MatematicaRemote;
public class Main {
public static void main(String[] args) {
try {
new Main().invoke();
} catch (NamingException e) {
e.printStackTrace();
}
}
public void invoke() throws NamingException {
// Let's lookup the remote stateless calculator
final MatematicaRemote statelessRemoteCalculator = lookupRemoteStatelessMatematica();
System.out.println("Obtained a remote stateless matematica for invocation");
// invoke on the remote calculator
int a = 204;
int b = 340;
System.out.println("Adding " + a + " and " + b + " via the remote stateless calculator deployed on the server");
int sum = statelessRemoteCalculator.soma(a, b);
System.out.println("Remote calculator returned sum = " + sum);
if (sum != a + b) {
throw new RuntimeException("Remote stateless calculator returned an incorrect sum " + sum + " ,expected sum was " + (a + b));
}
// try one more invocation, this time for subtraction
int num1 = 3434;
int num2 = 2332;
System.out.println("Subtracting " + num2 + " from " + num1 + " via the remote stateless calculator deployed on the server");
int difference = statelessRemoteCalculator.subtrai(num1, num2);
System.out.println("Remote calculator returned difference = " + difference);
if (difference != num1 - num2) {
throw new RuntimeException("Remote stateless calculator returned an incorrect difference " + difference + " ,expected difference was " + (num1 - num2));
}
}
private static MatematicaRemote lookupRemoteStatelessMatematica() throws NamingException {
final Hashtable<String, String> jndiProperties = new Hashtable<String, String>();
jndiProperties.put(Context.URL_PKG_PREFIXES, "org.jboss.ejb.client.naming");
final Context context = new InitialContext(jndiProperties);
// The app name is the application name of the deployed EJBs. This is typically the ear name
// without the .ear suffix. However, the application name could be overridden in the application.xml of the
// EJB deployment on the server.
// Since we haven't deployed the application as a .ear, the app name for us will be an empty string
final String appName = "EJBTestes";
// This is the module name of the deployed EJBs on the server. This is typically the jar name of the
// EJB deployment, without the .jar suffix, but can be overridden via the ejb-jar.xml
// In this example, we have deployed the EJBs in a jboss-as-ejb-remote-app.jar, so the module name is
// jboss-as-ejb-remote-app
final String moduleName = "jboss-as-ejb-remote-app";
// AS7 allows each deployment to have an (optional) distinct name. We haven't specified a distinct name for
// our EJB deployment, so this is an empty string
final String distinctName = "";
// The EJB name which by default is the simple class name of the bean implementation class
final String beanName = Matematica.class.getSimpleName();
// the remote view fully qualified class name
final String viewClassName = MatematicaRemote.class.getName();
// let's do the lookup
return (MatematicaRemote) context.lookup("ejb:" + appName + "/" + moduleName + "/" + distinctName + "/" + beanName + "!" + viewClassName);
}
}
Exceção que é lançada quando tento executar o código acima:
javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file: java.naming.factory.initial
at javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:662)
at javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:307)
at javax.naming.InitialContext.getURLOrDefaultInitCtx(InitialContext.java:344)
at javax.naming.InitialContext.lookup(InitialContext.java:411)
at Main.lookupRemoteStatelessMatematica(Main.java:79)
at Main.invoke(Main.java:22)
at Main.main(Main.java:14)
Bibliotecas que estão no meu classpath do cliente:
C:\Desenvolvimento\jboss-as-7.1.0.CR1b\modules\javax\activation\api\main\activation-1.1.1.jar
C:\Desenvolvimento\jboss-as-7.1.0.CR1b\modules\javax\annotation\api\main\jboss-annotations-api_1.1_spec-1.0.0.Final.jar
C:\Desenvolvimento\jboss-as-7.1.0.CR1b\modules\javax\ejb\api\main\jboss-ejb-api_3.1_spec-1.0.1.Final.jar
C:\Desenvolvimento\jboss-as-7.1.0.CR1b\modules\javax\el\api\main\jboss-el-api_2.2_spec-1.0.0.Final.jar
C:\Desenvolvimento\jboss-as-7.1.0.CR1b\modules\javax\enterprise\api\main\cdi-api-1.0-SP4.jar
C:\Desenvolvimento\jboss-as-7.1.0.CR1b\modules\javax\enterprise\deploy\api\main\jboss-jad-api_1.2_spec-1.0.0.Final.jar
C:\Desenvolvimento\jboss-as-7.1.0.CR1b\modules\javax\faces\api\main\jboss-jsf-api_2.1_spec-2.0.0.Beta1.jar
C:\Desenvolvimento\jboss-as-7.1.0.CR1b\modules\javax\inject\api\main\javax.inject-1.jar
C:\Desenvolvimento\jboss-as-7.1.0.CR1b\modules\javax\interceptor\api\main\jboss-interceptors-api_1.1_spec-1.0.0.Final.jar
C:\Desenvolvimento\jboss-as-7.1.0.CR1b\modules\javax\jms\api\main\jboss-jms-api_1.1_spec-1.0.0.Final.jar
C:\Desenvolvimento\jboss-as-7.1.0.CR1b\modules\javax\jws\api\main\jsr181-api-1.0-MR1.jar
C:\Desenvolvimento\jboss-as-7.1.0.CR1b\modules\javax\mail\api\main\mail-1.4.4.jar
C:\Desenvolvimento\jboss-as-7.1.0.CR1b\modules\javax\management\j2ee\api\main\jboss-j2eemgmt-api_1.1_spec-1.0.0.Final.jar
C:\Desenvolvimento\jboss-as-7.1.0.CR1b\modules\javax\persistence\api\main\hibernate-jpa-2.0-api-1.0.1.Final.jar
C:\Desenvolvimento\jboss-as-7.1.0.CR1b\modules\javax\resource\api\main\jboss-connector-api_1.6_spec-1.0.0.Final.jar
C:\Desenvolvimento\jboss-as-7.1.0.CR1b\modules\javax\rmi\api\main\jboss-rmi-api_1.0_spec-1.0.4.Final.jar
C:\Desenvolvimento\jboss-as-7.1.0.CR1b\modules\javax\security\auth\message\api\main\jboss-jaspi-api_1.0_spec-1.0.0.Final.jar
C:\Desenvolvimento\jboss-as-7.1.0.CR1b\modules\javax\security\jacc\api\main\jboss-jacc-api_1.4_spec-1.0.1.Final.jar
C:\Desenvolvimento\jboss-as-7.1.0.CR1b\modules\javax\servlet\api\main\jboss-servlet-api_3.0_spec-1.0.0.Final.jar
C:\Desenvolvimento\jboss-as-7.1.0.CR1b\modules\javax\servlet\jsp\api\main\jboss-jsp-api_2.2_spec-1.0.0.Final.jar
C:\Desenvolvimento\jboss-as-7.1.0.CR1b\modules\javax\servlet\jstl\api\main\jboss-jstl-api_1.2_spec-1.0.1.Final.jar
C:\Desenvolvimento\jboss-as-7.1.0.CR1b\modules\javax\transaction\api\main\jboss-transaction-api_1.1_spec-1.0.0.Final.jar
C:\Desenvolvimento\jboss-as-7.1.0.CR1b\modules\javax\validation\api\main\validation-api-1.0.0.GA.jar
C:\Desenvolvimento\jboss-as-7.1.0.CR1b\modules\javax\ws\rs\api\main\jboss-jaxrs-api_1.1_spec-1.0.0.Final.jar
C:\Desenvolvimento\jboss-as-7.1.0.CR1b\modules\javax\wsdl4j\api\main\wsdl4j-1.6.2.jar
C:\Desenvolvimento\jboss-as-7.1.0.CR1b\modules\javax\xml\bind\api\main\jboss-jaxb-api_2.2_spec-1.0.3.Final.jar
C:\Desenvolvimento\jboss-as-7.1.0.CR1b\modules\javax\xml\registry\api\main\jboss-jaxr-api_1.0_spec-1.0.0.Final.jar
C:\Desenvolvimento\jboss-as-7.1.0.CR1b\modules\javax\xml\rpc\api\main\jboss-jaxrpc-api_1.1_spec-1.0.0.Final.jar
C:\Desenvolvimento\jboss-as-7.1.0.CR1b\modules\javax\xml\soap\api\main\jboss-saaj-api_1.3_spec-1.0.1.Final.jar
C:\Desenvolvimento\jboss-as-7.1.0.CR1b\modules\javax\xml\ws\api\main\jboss-jaxws-api_2.2_spec-2.0.0.Final.jar
C:\Desenvolvimento\jboss-as-7.1.0.CR1b\modules\org\hibernate\validator\main\hibernate-validator-4.2.0.Final.jar
C:\Desenvolvimento\jboss-as-7.1.0.CR1b\modules\org\picketbox\main\picketbox-4.0.6.Beta2.jar
C:\Desenvolvimento\jboss-as-7.1.0.CR1b\modules\org\picketbox\main\picketbox-commons-1.0.0.CR1.jar
C:\Desenvolvimento\jboss-as-7.1.0.CR1b\modules\org\picketbox\main\picketbox-infinispan-4.0.6.Beta2.jar
C:\Desenvolvimento\jboss-as-7.1.0.CR1b\modules\org\jboss\as\controller-client\main\jboss-as-controller-client-7.1.0.CR1b.jar
C:\Desenvolvimento\jboss-as-7.1.0.CR1b\modules\org\jboss\dmr\main\jboss-dmr-1.1.1.Final.jar
C:\Desenvolvimento\jboss-as-7.1.0.CR1b\modules\org\jboss\logging\main\jboss-logging-3.1.0.CR2.jar
C:\Desenvolvimento\jboss-as-7.1.0.CR1b\modules\org\jboss\resteasy\resteasy-jaxb-provider\main\resteasy-jaxb-provider-2.3.0.GA-jandex.jar
C:\Desenvolvimento\jboss-as-7.1.0.CR1b\modules\org\jboss\resteasy\resteasy-jaxb-provider\main\resteasy-jaxb-provider-2.3.0.GA.jar
C:\Desenvolvimento\jboss-as-7.1.0.CR1b\modules\org\jboss\resteasy\resteasy-jaxrs\main\async-http-servlet-3.0-2.3.0.GA-jandex.jar
C:\Desenvolvimento\jboss-as-7.1.0.CR1b\modules\org\jboss\resteasy\resteasy-jaxrs\main\async-http-servlet-3.0-2.3.0.GA.jar
C:\Desenvolvimento\jboss-as-7.1.0.CR1b\modules\org\jboss\resteasy\resteasy-jaxrs\main\resteasy-jaxrs-2.3.0.GA-jandex.jar
C:\Desenvolvimento\jboss-as-7.1.0.CR1b\modules\org\jboss\resteasy\resteasy-jaxrs\main\resteasy-jaxrs-2.3.0.GA.jar
C:\Desenvolvimento\jboss-as-7.1.0.CR1b\modules\org\jboss\resteasy\resteasy-multipart-provider\main\resteasy-multipart-provider-2.3.0.GA-jandex.jar
C:\Desenvolvimento\jboss-as-7.1.0.CR1b\modules\org\jboss\resteasy\resteasy-multipart-provider\main\resteasy-multipart-provider-2.3.0.GA.jar
Esse problema ocorre usando a JDK7 ou 6.