更新时间:2022-10-04 21:05:07
相关软件:
1、JBoss(jboss-as-7.1.1.Final):http://jbossas.jboss.org/downloads
2、Intellij IDEA 13.02
3、JDK 1.7
4、Tomcat 6
1、新建一个项目,即作业空间:
package com.ejb; import javax.ejb.Remote; /** * Created by gao on 16-3-27. */ @Remote public interface HelloWorld { public String sayHello(String world); }
4、创建一个无状态Bean,右击src,New -> Stateless Session Bean。
输入名称为HelloWorld。
实现HelloWorld接口,完成编程:
package com.ejb; import javax.ejb.Stateless; /** * Created by gao on 16-3-27. */ @Stateless(name = "HelloWorldEJB") public class HelloWorldBean implements HelloWorld { public HelloWorldBean() { } @Override public String sayHello(String world) { return "Hello " + world + "!"; } }
5、部署JBoss,选择JBoss -> Local
弹出Run/Debug Configuration,输入Name为ejbtest,然后选择Deployment:
package com.ejb; import javax.ejb.Remote; /** * Created by gao on 16-3-28. */ @Remote public interface HelloWorld { public String sayHello(String world); }
4)新建一个测试类
import com.ejb.HelloWorld; import javax.naming.Context; import javax.naming.InitialContext; import javax.naming.NamingException; import java.util.Hashtable; public class HelloWorldClient { public static void main(String[] args) throws NamingException { // Properties props = new Properties(); // props.setProperty("java.naming.factory.initial", // "org.jnp.interfaces.NamingContextFactory"); // props.setProperty("java.naming.provider.url", "localhost:1099"); // props.setProperty("java.naming.factory.url.pkgs", "org.jboss.naming"); // try { // InitialContext ctx = new InitialContext(props); // HelloWorldRemote helloworld = (HelloWorldRemote) ctx // .lookup("HelloWorld/remote"); // System.out.println(helloworld.sayHello("World")); // System.out.println(helloworld.sayHello("China")); // } catch (NamingException e) { // e.printStackTrace(); // } HelloWorld helloWorld = lookupRemoteStatelessEjbBean(); System.out.println(helloWorld); String s = helloWorld.sayHello("world"); System.out.println(s); } private static HelloWorld lookupRemoteStatelessEjbBean() throws NamingException { final Hashtable jndiProperties = new Hashtable(); jndiProperties.put(Context.URL_PKG_PREFIXES, "org.jboss.ejb.client.naming"); final Context context = new InitialContext(jndiProperties); final String appName = ""; final String moduleName = "EJBTest_war_exploded"; final String distinctName = ""; final String beanName = "HelloWorldEJB"; final String viewClassName = HelloWorld.class.getName(); final String namespace = "ejb:" + appName + "/" + moduleName + "/" + distinctName + "/" + beanName + "!" + viewClassName; System.out.println(namespace); return (HelloWorld) context.lookup(namespace); /* return (RemoteCounter) context.lookup("ejb:" + appName + "/" + moduleName + "/" + distinctName + "/" + beanName + "!" + viewClassName + "?stateful"); */ } }
endpoint.name=client-endpoint remote.connectionprovider.create.options.org.xnio.Options.SSL_ENABLED=false remote.connections=default remote.connection.default.host=localhost remote.connection.default.port = 4447 remote.connection.default.connect.options.org.xnio.Options.SASL_POLICY_NOANONYMOUS=false remote.connection.default.username=yang remote.connection.default.password=123456
6)项目目录: