接口
package com;public interface IHello{    public String hello();}
无状态的bean
package com.impl;
import javax.ejb.Remote;
import javax.ejb.Stateless;
import com.IHello;
@Remote({IHello.class})
@Stateless
public class Hello implements IHello{     @Override
    public String hello()    {       System.out.println("hello world!");       return "HEL!!";    } }
客户端package com.client;import java.rmi.RMISecurityManager;
import java.util.Properties;import javax.naming.InitialContext;
import javax.naming.NamingException;import com.IHello;public class FirstTest {
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:8888"); InitialContext ctx = new InitialContext(props);
IHello hello = (IHello) ctx.lookup("Hello/remote"); hello.hello(); }}
这个是《ejb3.0实例教程》上的例子照着敲得但是还是出现了异常
异常信息
Exception in thread "main" javax.naming.CommunicationException: Could not obtain connection to any of these urls: localhost:8888 and discovery failed with error: javax.naming.CommunicationException: Receive timed out [Root exception is java.net.SocketTimeoutException: Receive timed out]
at org.jnp.interfaces.NamingContext.discoverServer(NamingContext.java:1690)
at org.jnp.interfaces.NamingContext.checkRef(NamingContext.java:1761)
at org.jnp.interfaces.NamingContext.lookup(NamingContext.java:695)
at org.jnp.interfaces.NamingContext.lookup(NamingContext.java:688)
at javax.naming.InitialContext.lookup(InitialContext.java:392)
at com.client.FirstTest.main(FirstTest.java:21)
Caused by: java.net.SocketTimeoutException: Receive timed out
at java.net.PlainDatagramSocketImpl.receive0(Native Method)
at java.net.PlainDatagramSocketImpl.receive(PlainDatagramSocketImpl.java:136)
at java.net.DatagramSocket.receive(DatagramSocket.java:712)
at org.jnp.interfaces.NamingContext.discoverServer(NamingContext.java:1659)
... 5 more
 [Root exception is javax.naming.CommunicationException: Failed to retrieve stub from server localhost/127.0.0.1:8888 [Root exception is java.io.EOFException]]
at org.jnp.interfaces.NamingContext.checkRef(NamingContext.java:1780)
at org.jnp.interfaces.NamingContext.lookup(NamingContext.java:695)
at org.jnp.interfaces.NamingContext.lookup(NamingContext.java:688)
at javax.naming.InitialContext.lookup(InitialContext.java:392)
at com.client.FirstTest.main(FirstTest.java:21)
Caused by: javax.naming.CommunicationException: Failed to retrieve stub from server localhost/127.0.0.1:8888 [Root exception is java.io.EOFException]
at org.jnp.interfaces.NamingContext.getServer(NamingContext.java:329)
at org.jnp.interfaces.NamingContext.checkRef(NamingContext.java:1746)
... 4 more
Caused by: java.io.EOFException
at java.io.ObjectInputStream$PeekInputStream.readFully(ObjectInputStream.java:2281)
at java.io.ObjectInputStream$BlockDataInputStream.readShort(ObjectInputStream.java:2750)
at java.io.ObjectInputStream.readStreamHeader(ObjectInputStream.java:780)
at java.io.ObjectInputStream.<init>(ObjectInputStream.java:280)
at org.jnp.interfaces.NamingContext.getServer(NamingContext.java:314)
... 5 more

解决方案 »

  1.   

    学习中,我EJB没用过,LZ加油!
      

  2.   

    at com.client.FirstTest.main(FirstTest.java:21)
    这句的问题。超时?
      

  3.   

    程序连不上jboss长时间得不到响应坑定会抱着个异常,我的问题是Could not obtain connection to any of these urls: localhost:8888 and discovery failed这个
      

  4.   

    自己在访问一下localhost:8888。在jndi list view下看是否能够找到你的hello/remote
      

  5.   

    props.setProperty("java.naming.provider.url", "localhost:8888");这个你改过了吗,
    如果没有改过,jboss as 6 默认是1099的 ,你可以看看
    %jboss_home%\server\default\conf\bindingservice.beans\META-INF\bindings-jboss-beans.xml里面

       <bean name="StandardBindings" class="java.util.HashSet">
          <constructor>
             <parameter class="java.util.Collection">
                <set elementClass="org.jboss.services.binding.ServiceBindingMetadata">
                <!-- ********************* conf/jboss-service.xml ****************** -->            <!-- Naming Service -->
                <bean class="org.jboss.services.binding.ServiceBindingMetadata">
                   <property name="serviceName">jboss:service=Naming</property>
                   <property name="bindingName">Port</property>
                   <property name="port">1099</property>
                   <property name="description">The listening socket for the Naming service</property>
                </bean>是什么,props.setProperty("java.naming.provider.url", "localhost:8888");端口就是什么