使用Client在EJB中使用有状态bean

时间:2014-01-17 05:13:43

标签: java java-ee jboss ejb

我正在研究在eclipse中使用EJB和JBOSS7.1实现有状态会话的项目。

我在运行客户端代码

时想出了异常
javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file:  java.naming.factory.initial
at javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:645)
at javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:288)
at javax.naming.InitialContext.getURLOrDefaultInitCtx(InitialContext.java:325)
at javax.naming.InitialContext.lookup(InitialContext.java:392)
at com.sridhar.client.ShoppingCartClient.main(ShoppingCartClient.java:15)

这里有什么问题?

以下是我的代码,

接口

package com.sridhar.shoppingcart;

import java.util.HashMap;

import javax.ejb.Remote;
import javax.ejb.Remove;

@Remote
public interface ShoppingCartLocal {
    void buy(String product,int quantity);
    HashMap<String,Integer> getCartContents();
    @Remove void checkOut();
}

实施业务逻辑的Bean

package com.sridhar.shoppingcart;

import java.util.HashMap;
import java.util.Map;

import javax.ejb.Remote;
import javax.ejb.Stateful;


/**
 * Session Bean implementation class ShoppingCart
 */
@Stateful
@Remote(ShoppingCart.class)
public class ShoppingCart implements ShoppingCartLocal {

    private Map<String,Integer> cart = new HashMap<String,Integer>();
    /**
     * Default constructor. 
     */
    @Override
    public void buy(String product, int quantity) {
        // TODO Auto-generated method stub
        if(cart.containsKey(product)){
            int currentQuantity = cart.get(product);
            currentQuantity = currentQuantity+quantity;
            cart.put(product, currentQuantity); 
        }else{
            cart.put(product, quantity);
        }

    }

    @Override
    public HashMap<String, Integer> getCartContents() {
        // TODO Auto-generated method stub
        return (HashMap<String, Integer>) cart;
    }

    @Override
    public void checkOut() {
        // TODO Auto-generated method stub
        System.out.println("To be implemented");
    }
}

访问和查找代码的客户端代码:

package com.sridhar.client;

import javax.naming.InitialContext;
import javax.naming.NamingException;

import java.util.*;

import com.sridhar.shoppingcart.ShoppingCartLocal;
public class ShoppingCartClient {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        try {
            InitialContext context = new InitialContext();
            ShoppingCartLocal cart = (ShoppingCartLocal)context.lookup("ShoppingCart/Remote");
            System.out.println("Buying 1 memory stick");
            cart.buy("Memory Stick",1);
            System.out.println("Buying another memory stick");
            cart.buy("Memory stick", 1);
            System.out.println("Buying 1 laptop");
            cart.buy("Lappy", 1);
            HashMap<String,Integer>fullCart = cart.getCartContents();
            for(String product : fullCart.keySet()){
                System.out.println("full cart is:"+product+","+fullCart.get(product));
            }
            System.out.println("checkOut");
            //cart.checkOut();
        } catch (NamingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

}

有没有什么好方法可以很好地运行这个项目?,然后给出实现的步骤。

为什么会出现异常?怎么解决?

2 个答案:

答案 0 :(得分:1)

您必须实例化InitialContext environment这样的<{p}}

Hashtable env = new Hashtable();
env.put(Context.INITIAL_CONTEXT_FACTORY,CONTEXT_FACTORY_CLASS);    
Context ctx = new InitialContext(env);

这是初始化客户端应用程序中的上下文所必需的。请参阅InitialContext的documentation以了解更多信息。希望这有帮助!

答案 1 :(得分:0)

使用JNDI进行远程EJB调用时,需要注意以下几点:

1)向客户端CLASSPATH添加必要的dependecis,这是:添加远程接口类和jboss-client.jar。

2)正确实例化InitialConetxt

3)提供正确的查找名称(在部署ejb时,此信息记录在服务器中)。

您必须使用JBoss 7 requeriments(与先前版本不同)才能正确配置客户端。