File: HelloServiceBean.java
import javax.ejb.Stateless;
@Stateless
public class HelloServiceBean implements HelloServiceLocal, HelloServiceRemote {
public String sayHello(String name) {
return "Hello1, " + name;
}
}
File: HelloServiceLocal.java
import javax.ejb.Local;
@Local
public interface HelloServiceLocal {
public String sayHello(String name);
}
File: HelloServiceRemote.java
import javax.ejb.Remote;
@Remote
public interface HelloServiceRemote{
public String sayHello(String name);
}
File: jndi.properties
java.naming.factory.initial=org.jnp.interfaces.NamingContextFactory
java.naming.factory.url.pkgs=org.jboss.naming:org.jnp.interfaces
java.naming.provider.url=localhost:1099
File: Main.java
import java.util.*;
import javax.naming.*;
public class Main{
public static void main(String[] a) throws Exception{
/* get a initial context. By default the settings in the file
* jndi.properties are used.
* You can explicitly set up properties instead of using the file.
*
Properties properties = new Properties();
properties.put("java.naming.factory.initial","org.jnp.interfaces.NamingContextFactory");
properties.put("java.naming.factory.url.pkgs","=org.jboss.naming:org.jnp.interfaces");
properties.put("java.naming.provider.url","localhost:1099");
*/
String name = "java2s";
HelloServiceRemote service = null;
//Context compEnv = (Context) new InitialContext().lookup("java:comp/env");
//service = (HelloService)new InitialContext().lookup("java:comp/env/ejb/HelloService");
service = (HelloServiceRemote)new InitialContext().lookup("HelloServiceBean/remote");
System.out.println(service.sayHello(name));
}
}
|