我是EJB新手。我尝试了java_for_web_with_servlets_jsp_and_ejb book中的一个例子。以下代码创建了一个名为Adder的会话,它添加了两个整数:
Adder.java:
package com.brainysoftware.ejb;
import java.rmi.RemoteException;
import javax.ejb.EJBObject;
public interface Adder extends EJBObject{
public int add(int a,int b) throws RemoteException;
}
AdderBean.java
package com.brainysoftware.ejb;
import java.rmi.RemoteException;
import javax.ejb.EJBException;
import javax.ejb.SessionBean;
import javax.ejb.SessionContext;
public class AdderBean implements SessionBean{
public int add(int a, int b){
System.out.println("From AdderBean");
return (a+b);
}
@Override
public void ejbActivate() throws EJBException, RemoteException {
// TODO Auto-generated method stub
}
@Override
public void ejbPassivate() throws EJBException, RemoteException {
// TODO Auto-generated method stub
}
@Override
public void ejbRemove() throws EJBException, RemoteException {
// TODO Auto-generated method stub
}
@Override
public void setSessionContext(SessionContext arg0) throws EJBException,
RemoteException {
// TODO Auto-generated method stub
}
}
AdderHome.java
package com.brainysoftware.ejb;
import java.rmi.RemoteException;
import javax.ejb.CreateException;
import javax.ejb.EJBHome;
public interface AdderHome extends EJBHome{
Adder create() throws RemoteException, CreateException;
}
部署描述符如下:
<?xml version="1.0" encoding="UTF-8"?>
<ejb-jar version="3.1" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/ejb-jar_3_1.xsd">
<description>Your first EJB application</description>
<display-name>Adder Application</display-name>
<enterprise-beans>
<session>
<ejb-name>Adder</ejb-name>
<home>com.brainysoftware.ejb.AdderHome</home>
<remote>com.brainysoftware.ejb.Adder</remote>
<ejb-class>com.brainysoftware.ejb.AdderBean</ejb-class>
<session-type>Stateless</session-type>
<transaction-type>Bean</transaction-type>
</session>
</enterprise-beans>
</ejb-jar>
现在,对于客户端,我创建了一个动态Web项目,该项目具有使用Adder bean的类 BeanClient.java :
import java.rmi.RemoteException; import java.util.Properties; import javax.ejb.CreateException; import javax.naming.Context; import javax.naming.InitialContext; import javax.naming.NamingException; import com.brainysoftware.ejb.Adder; import com.brainysoftware.ejb.AdderHome; import javax.rmi.PortableRemoteObject;
公共类BeanClient {
public static void main(String [] args){
属性props = new Properties();
props.put(Context.INITIAL_CONTEXT_FACTORY,&#34; org.jnp.interfaces.NamingContextFactory&#34;);
props.put(Context.PROVIDER_URL,&#34; localhost:1099&#34;);
尝试{
InitialContext jnic = new InitialContext(props);
System.out.println(&#34;获取上下文&#34;);
Object ref = jnic.lookup(&#34; Adder&#34;);
System.out.println(&#34;得到参考&#34;);
AdderHome home =(AdderHome)PortableRemoteObject.narrow(ref,AdderHome.class);
Adder adder = home.create();
System.out.println(&#34;添加2和5:&#34; + adder.add(2,5));
} catch(NamingException e){
的System.out.println(e.toString());
} catch(CreateException e){
的System.out.println(e.toString());
} catch(RemoteException e){
的System.out.println(e.toString());
}
}
}
在eclipse中执行此类时,我收到以下错误:
获取上下文 javax.naming.CommunicationException:无法获取任何这些URL的连接:localhost:1099并且发现失败并显示错误:javax.naming.CommunicationException:接收超时[根异常是java.net.SocketTimeoutException:接收超时] [Root异常是javax.naming.CommunicationException:无法连接到服务器localhost:1099 [根异常是javax.naming.ServiceUnavailableException:无法连接到服务器localhost:1099 [根异常是java.net.ConnectException:连接被拒绝:连接]] ]
你可以帮我解决这个问题吗?
答案 0 :(得分:0)
查询和ejb firstbale:
你必须有一个接口及其实现。
你必须注释你的界面(@Local ou @Remote) 实现必须注释@statless ou @statful 您必须添加项目jndi.properties。 您的ejb项目必须部署在您的服务器上才能轻松查找。 我在这里有一个jboss的例子: https://www.youtube.com/watch?v=mBwgHuBg96g
我希望我的回答很有用,