Einstieg in J2EE: Remote auf Bean zugreifen

tomcom

Mitglied
Hallo liebe Commuinity,

ich bin Neueinsteiger bei J2EE und möchte erst einmal die Grundlagen zum Arbeiten mit einem Applicationserver verstehen. Dazu habe ich einen JBoss 7 AS aufgesetzt und entwickle mit Eclipse Statless Session Beans.

Zunächst habe ich ein Tutorial durchgerabeitet, das eine Bean mit dem Klassennamen MyBean hat. Dazu gehören die Interfaces MyBeanLocal und MyBeanRemote , so wie ein allgemeines Interface IMyBean:
Java:
import java.io.Serializable;

public interface IMyBean extends Serializable {    
    public void doSomething();
}
-------------------------------------------------------------
import javax.ejb.Local;
@Local
public interface MyBeanLocal extends IMyBean {}
-------------------------------------------------------------
import javax.eimport javax.ejb.Remote;
@Remote
public interface MyBeanRemote extends IMyBean{}
-------------------------------------------------------------
import javax.ejb.Stateless;

@Stateless  
public class MyBean implements MyBeanLocal, MyBeanRemote {
    private static final long serialVersionUID = 9184424076718418234L;

    public void doSomething() {
        System.out.println("Hello World!");         
    }
}

Das ganze wird an den JBoss übermittelt, indem ich in Eclipse für das Projekt Run as -> Run on Server ausführe.

In der Serverkonsole erscheint daraufhin:
Code:
19:33:31,360 INFO  [org.jboss.as.server.deployment] (MSC service thread 1-5) Starting deployment of "MyBeansnull"
19:33:31,365 INFO  [org.jboss.as.jpa] (MSC service thread 1-7) added javax.persistence.api dependency to MyBeansnull
19:33:31,372 INFO  [org.jboss.as.ejb3.deployment.processors.EjbJndiBindingsDeploymentUnitProcessor] (MSC service thread 1-6) JNDI bindings for session bean named MyBean in deployment unit deployment "MyBeansnull" are as follows:

	java:global/MyBeansnull/MyBean!MyBeanLocal
	java:app/MyBeansnull/MyBean!MyBeanLocal
	java:module/MyBean!MyBeanLocal
	java:global/MyBeansnull/MyBean!MyBeanRemote
	java:app/MyBeansnull/MyBean!MyBeanRemote
	java:module/MyBean!MyBeanRemote

19:33:31,395 INFO  [org.jboss.as.server.controller] (DeploymentScanner-threads - 2) Deployed "MyBeansnull"

So weit, so gut.
Nun muss ich also nur noch per Client-Programm die Bean nutzen:
Java:
import java.util.Properties;
public class MyBeanClient {
    public static void main(String[] args) {
        try {
            Properties props = new Properties();
            props.setProperty("java.naming.factory.initial","org.jboss.as.naming.InitialContextFactory");
            props.setProperty("java.naming.factory.url.pkgs", "org.jboss.naming");
            props.setProperty("java.naming.provider.url", "127.0.0.1:1099");         
            
            InitialContext ctx = new InitialContext(props);            
            MyBeanRemote bean = (MyBeanRemote) ctx.lookup("MyBean/remote");
            
            bean.doSomething();
        } catch (NamingException e) {
            e.printStackTrace();
        } 
    }
}

Allerdings kommt beim Ausführen folgende Fehlermeldung:
Code:
javax.naming.NameNotFoundException: Name 'MyBean' not found in context ''
	at org.jboss.as.naming.util.NamingUtils.nameNotFoundException(NamingUtils.java:111)
	at org.jboss.as.naming.InMemoryNamingStore$NodeTraversingVisitor.visit(InMemoryNamingStore.java:368)
	at org.jboss.as.naming.InMemoryNamingStore$ContextNode.accept(InMemoryNamingStore.java:307)
	at org.jboss.as.naming.InMemoryNamingStore.lookup(InMemoryNamingStore.java:162)
	at org.jboss.as.naming.NamingContext.lookup(NamingContext.java:173)
	at org.jboss.as.naming.InitialContext.lookup(InitialContext.java:47)
	at org.jboss.as.naming.NamingContext.lookup(NamingContext.java:209)
	at javax.naming.InitialContext.lookup(InitialContext.java:409)
	at MyBeanClient.main(MyBeanClient.java:13)
Hmm, in der Server-Konsole stand ja unter anderem: java:module/MyBean!MyBeanRemote.
also änderte ich das Programm dementsprechend ab:
Java:
import java.util.Properties;
public class MyBeanClient {
    public static void main(String[] args) {
        try {
            /* .... */
            InitialContext ctx = new InitialContext(props);            
            MyBeanRemote bean = (MyBeanRemote) ctx.lookup("java:module/MyBean!MyBeanRemote");
            /* .... */
        } 
    }
}
Aber es ging wieder nicht 🙁
Code:
javax.naming.NameNotFoundException: java:module/MyBean!MyBeanRemote
	at org.jboss.as.naming.InitialContext.lookup(InitialContext.java:51)
	at org.jboss.as.naming.NamingContext.lookup(NamingContext.java:209)
	at javax.naming.InitialContext.lookup(InitialContext.java:409)
	at MyBeanClient.main(MyBeanClient.java:13)
Auffällig ist auch, das die Bean auf der JBoss-Administrator-Konsole (http://127.0.0.1:9990) unter Manage Deployments zu sehen ist (Warum wird snull angehängt?):
Code:
Name          | Runtime Name   | Enabled
MyBeansnull   | MyBeansnull    |  yes
Aber unter EJB -> Sessionbeans ist keine Bean aufgelistet.


Kann mir jemand weiter helfen, das ich endlich Remote ein Hello World ausgeben kann.

Gruß Thomas
 
Zuletzt bearbeitet von einem Moderator:
Derzeit gibt es noch einen Bug im JBoss 7 AS Full - ich weiß nicht, ob dieser auch im Web Profile auftritt. Dazu solltest Du mal bei den JBoss Leuten suchen, da gibt es ein paar Bugs zu.

Allerdings sollte es seit letzter Woche die erste Alpha zu 7.1 geben. Vielleicht läuft es damit.
 
Im Web-Profil funktioniert der Zugriff von WAR auf Beans, oder EAR nach EAR, wenn alles im JBoss liegt, aber nicht der Zugriff aus einer anderen VM auf die Beans. Ich hatte zuerst auch an einen Bug gedacht (wie viele andere auch).
 
Und so schnell wie der startet, verzeiht man Probleme am Anfang und wartet auf die 7.1 Final... 😀
Ich habe mir die Berta Version von 7.1 runtergeladen, ähhm Beta meinte ich natürlich.
Mal schauen, ob es damit läuft.
Ansonsten warte ich auf die Final-Version (oder Versuche es mit dem 6er-JbossAS).
Mit der 7.1-Beta-Version hab ich z.Zt. allerdings ein kleines Problem mit der Steuerung über Eclipse/Jbosstools. Aber das ist ein anderes Problem, für das ich auch einen anderen Thread aufmachen werde.

Vielen Dank für alle Antworten.
 

Neue Themen


Zurück
Oben