Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 582
  • Last Modified:

ejb wildfly example

Hi,

I am trying ejb stateless bean example on wildfly 10.

my code as follows
package com.gp;

import javax.ejb.LocalBean;
import javax.ejb.Stateless;

/**
 * Session Bean implementation class GreetBean
 */
@Stateless
@LocalBean
public class GreetBean implements GreetBeanRemote {

    /**
     * Default constructor. 
     */
    public GreetBean() {
        // TODO Auto-generated constructor stub
    }

    @Override
    public void greet(String name) {
	// TODO Auto-generated method stub
	System.out.println("hello dear"+name);
	
    }

}

Open in new window

package com.gp;

import javax.ejb.Remote;

@Remote
public interface GreetBeanRemote {
void greet(String name);
}

Open in new window


package com.gp;

import java.util.Hashtable;

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

public class GreetClient {

    public static void main(String[] args) {
	// TODO Auto-generated method stub

	//String host = "192.168.56.1";// localhost 127.0.0.1
	String host = "127.0.0.1";// localhost 127.0.0.1
	String port = "8180";
	Hashtable<Object, Object> props = new Hashtable<>();
	props.put(Context.INITIAL_CONTEXT_FACTORY, "org.jboss.naming.remote.client.InitialContext");
	props.put(Context.URL_PKG_PREFIXES, "org.jboss.ejb.client.naming");

	props.put("jboss.naming.client.ejb.context", false);
	props.put("org.jboss.ejb.client.scoped.context", true);

	props.put("endpoint.name", "client-endpoint");
	props.put("remote.connectionprovider.create.options.org.xnio.Options.SSL_Enables", true);

	props.put("remote.connections", "default");
	props.put("remote.connection.default.connect.options.org.xnio.Options.SASL_POLICY_NOANANYMOUS", false);

	props.put(Context.PROVIDER_URL, "http-remoting://" + host + ":" + port);
	props.put("remote.connection.default.host", host);
	props.put("remote.connection.default.port", port);

	String beanName = GreetBean.class.getSimpleName();
	String interfaceName = GreetBeanRemote.class.getName();
	String moduleName = "GreetEJBProjectStateless";

	String lookUpName = "ejb:/" + moduleName + "/" + beanName + "!" + interfaceName;

	System.out.println("lookUpName is---->" + lookUpName);

	Context context;
	try {
	    context = new InitialContext(props);

	    GreetBeanRemote bean = (GreetBeanRemote) context.lookup(lookUpName);
	    bean.greet("gp");
	} catch (NamingException e) {
	    // TODO Auto-generated catch block
	    e.printStackTrace();
	}

	/*
	 * java:global/GreetEJBProjectStaless/GreetBean!com.gp.GreetBean
	 * java:app/GreetEJBProjectStaless/GreetBean!com.gp.GreetBean
	 * java:module/GreetBean!com.gp.GreetBean
	 * java:global/GreetEJBProjectStaless/GreetBean!com.gp.GreetBeanRemote
	 * java:app/GreetEJBProjectStaless/GreetBean!com.gp.GreetBeanRemote
	 * java:module/GreetBean!com.gp.GreetBeanRemote
	 * java:jboss/exported/GreetEJBProjectStaless/GreetBean!com.gp.
	 * GreetBeanRemote
	 */

    }

}

Open in new window

ejb-jar.xml is

<?xml version="1.0" encoding="UTF-8"?>
<ejb-jar xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/ejb-jar_3_2.xsd" version="3.2">
  <display-name>GreetEJBProjectStaless</display-name>
</ejb-jar>

Open in new window


structure is as attached


i am getting below error
lookUpName is---->ejb:/GreetEJBProjectStateless/GreetBean!com.gp.GreetBeanRemote
javax.naming.NoInitialContextException: Cannot instantiate class: org.jboss.naming.remote.client.InitialContext [Root exception is java.lang.ClassNotFoundException: org.jboss.naming.remote.client.InitialContext]
      at javax.naming.spi.NamingManager.getInitialContext(Unknown Source)
      at javax.naming.InitialContext.getDefaultInitCtx(Unknown Source)
      at javax.naming.InitialContext.init(Unknown Source)
      at javax.naming.InitialContext.<init>(Unknown Source)
      at com.gp.GreetClient.main(GreetClient.java:44)
Caused by: java.lang.ClassNotFoundException: org.jboss.naming.remote.client.InitialContext
      at java.net.URLClassLoader.findClass(Unknown Source)
      at java.lang.ClassLoader.loadClass(Unknown Source)
      at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
      at java.lang.ClassLoader.loadClass(Unknown Source)
      at java.lang.Class.forName0(Native Method)
      at java.lang.Class.forName(Unknown Source)
      at com.sun.naming.internal.VersionHelper12.loadClass(Unknown Source)
      at com.sun.naming.internal.VersionHelper12.loadClass(Unknown Source)
      ... 5 more
please advise
strEjb2.png
0
gudii9
Asked:
gudii9
1 Solution
 
gudii9Author Commented:
0

Featured Post

Become an Android App Developer

Ready to kick start your career in 2018? Learn how to build an Android app in January’s Course of the Month and open the door to new opportunities.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now