0
votes

I am working on the project to achieve stateful session using EJB and JBOSS7.1 in eclipse.

I came up with an exception while running the client code as

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)

What is the problem over here ?

Below is my code,

Interface:

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();
}

The Bean which is implementing the business logic

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");
    }
}

Client code to access and lookup the code :

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();
        }
    }

}

Is there any good way to run this project well?,then give the steps to achieve.

Why am getting exception ? how to resolve it?

2

2 Answers

1
votes

You have to instantiate the InitialContext passing the environment like this

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

This is needed to initialize the context in client applications. Please refer the documentation of InitialContext to learn more. Hope this helps!

0
votes

When you make a remote EJB invocation using JNDI, you need to pay attention to these points:

1) add to the client CLASSPATH the necessary dependecis, this is:add the remote interface class and the jboss-client.jar.

2) instantiate the InitialConetxt correctly

3) provide a correct lookup name, (this information is logged in the server when the ejb is deployed).

You have to accomplish with the JBoss 7 requeriments (are not the same that previous versions) in order to configure the Client correctly .