I am trying to create a Entity bean to connect with a MySql databse using remote client but its giving error,using Jboss eap 7.1,Eclipse Juno to connect with Mysql databse 5.6,below are the source codes:
Entity Class:
package com.ibytecode.entities;
import java.io.Serializable;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Column;
@Entity(name = "project")
public class Project implements Serializable {
private static final long serialVersionUID = 1L;
public Project() {
super();
}
@Id
private int id;
private String name;
private String post;
@Column(name = "dept_no")
private int deptNo;
public int getid() {
return id;
}
public void setid(int id) {
this.id = id;
}
public String getname() {
return name;
}
public void setname(String name) {
this.name = name;
}
public String getpost() {
return post;
}
public void setpost(String post) {
this.post = post;
}
@Override
public String toString() {
return "Project [id=" + id + ", name="
+ name + ", post="
+ post + "]";
}
}
Remote Interface:
package com.ibytecode.business;
import java.util.List;
import javax.ejb.Remote;
import com.ibytecode.entities.Project;
@Remote
public interface IProject {
void saveProject(Project project);
Project findProject(Project project);
List<Project> retrieveAllProjects();
}
Session Bean:
package com.ibytecode.businesslogic;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import com.ibytecode.business.IProject;
import com.ibytecode.entities.Project;
@Stateless
public class ProjectBean implements IProject {
@PersistenceContext(unitName = "JPADB")
private EntityManager entityManager;
public ProjectBean() { }
@Override
public void saveProject(Project project) {
entityManager.persist(project);
}
@Override
public Project findProject(Project project) {
Project p = entityManager.find(Project.class,
project.getid());
return p;
}
@Override
public List<Project> retrieveAllProjects() {
String q = "SELECT id from " + Project.class.getName() + " entity";
Query query = entityManager.createQuery(q);
List<Project> projects = query.getResultList();
return projects;
}
}
ClientUtility:
package com.ibytecode.clientutility;
import java.util.Properties;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
public class JNDILookupClass {
/*
* location of JBoss JNDI Service provider the client will use. It should be
* URL string.
*/
private static final String PROVIDER_URL = "jnp://localhost:1099";
/*
* specifying the list of package prefixes to use when loading in URL
* context factories. colon separated
*/
private static final String JNP_INTERFACES = "org.jboss.naming:org.jnp.interfaces";
/*
* Factory that creates initial context objects. fully qualified class name.
*/
private static final String INITIAL_CONTEXT_FACTORY = "org.jnp.interfaces.NamingContextFactory";
private static Context initialContext;
public static Context getInitialContext() throws NamingException {
if (initialContext == null) {
// Properties extends HashTable
Properties prop = new Properties();
//prop.put(Context.INITIAL_CONTEXT_FACTORY, INITIAL_CONTEXT_FACTORY);
prop
.put("remote.connectionprovider.create.options.org.xnio.Options.SSL_ENABLED",
"false");
prop.put("remote.connections", "default");
prop.put("remote.connection.default.host", "localhost");
prop.put("remote.connection.default.port", "4447");
prop.put("remote.connection.default.connect.options.org.xnio.Options.SASL_POLICY_NOANONYMOUS",
"false");
prop.put(Context.URL_PKG_PREFIXES, JNP_INTERFACES);
prop.put(Context.PROVIDER_URL, PROVIDER_URL);
initialContext = new InitialContext(prop);
}
return initialContext;
}
}
Client Class:
package com.ibytecode.client;
import java.util.List;
import java.util.Properties;
import javax.naming.Context;
import javax.naming.NamingException;
import org.jboss.ejb.client.ContextSelector;
import org.jboss.ejb.client.EJBClientConfiguration;
import org.jboss.ejb.client.EJBClientContext;
import org.jboss.ejb.client.PropertiesBasedEJBClientConfiguration;
import org.jboss.ejb.client.remoting.ConfigBasedEJBClientContextSelector;
import com.ibytecode.business.IProject;
import com.ibytecode.businesslogic.ProjectBean;
import com.ibytecode.clientutility.JNDILookupClass;
import com.ibytecode.entities.Project;
public class EJBApplicationClient {
private static final String LOOKUP_STRING = "java:jboss/exported/FirstJPAProject1/ProjectBean!com.ibytecode.business.IProject";
public static void main(String[] args) {
final EJBClientConfiguration clientConfiguration = new PropertiesBasedEJBClientConfiguration(
createClientConfigurationProperties());
final ContextSelector<EJBClientContext> contextSelector = new ConfigBasedEJBClientContextSelector(
clientConfiguration);
// set the selector for use
EJBClientContext.setSelector(contextSelector);
IProject bean = doLookup();
Project p1 = new Project();
p1.setname("Banking App");
p1.setpost("Town City");
Project p2 = new Project();
p2.setname("Office Automation");
p2.setpost("Downtown");
// 3. Call business logic
// Saving new Projects
bean.saveProject(p1);
bean.saveProject(p2);
// Find a Project
p1.setid(1);
Project p3 = bean.findProject(p1);
System.out.println(p3);
// Retrieve all projects
System.out.println("List of Projects:");
List<Project> projects = bean.retrieveAllProjects();
for (Project project : projects)
System.out.println(project);
}
private static Properties createClientConfigurationProperties() {
final Properties properties = new Properties();
properties.put("remote.connectionprovider.create.options.org.xnio.Options.SSL_ENABLED","false");
properties.put("remote.connections", "default");
properties.put("remote.connection.default.host","localhost");
properties.put("remote.connection.default.port","4447");
properties.put("remote.connection.default.connect.options.org.xnio.Options.SASL_POLICY_NOANONYMOUS","false");
//properties.put("remote.lookupclass", "IProject");
// properties.put("remote.connection.default.connect.options.org.xnio.Options.SASL_DISALLOWED_MECHANISMS","JBOSS-LOCAL-USER");
// properties.put("remote.connection.default.connect.options.org.xnio.Options.SASL_POLICY_NOPLAINTEXT",
// "false");
return properties;
}
private static IProject doLookup() {
Context context = null;
IProject bean = null;
try {
// 1. Obtaining Context
context = JNDILookupClass.getInitialContext();
String lookupName = getLookupName();
// 2. Lookup and cast
bean = (IProject) context.lookup(lookupName);
} catch (NamingException e) {
e.printStackTrace();
}
return bean;
}
private static String getLookupName() {
/*
* The app name is the EAR name of the deployed EJB without .ear suffix.
* Since we haven't deployed the application as a .ear, the app name for
* us will be an empty string
*/
String appName = "";
// The module name is the JAR name of the deployed EJB without the .jar
// suffix.
String moduleName = "FirstJPAProject1";
/*
* AS7 allows each deployment to have an (optional) distinct name. This
* can be an empty string if distinct name is not specified.
*/
String distinctName = "";
// The EJB bean implementation class name
String beanName = ProjectBean.class.getSimpleName();
// Fully qualified remote interface name
final String interfaceName = IProject.class.getName();
// Create a look up string name
String name = "ejb:" + appName + "/" + moduleName + "/" + distinctName
+ "/" + beanName + "!" + interfaceName;
return name;
}
}
And the error i am getting is:
Aug 22, 2013 10:39:06 AM org.xnio.Xnio <clinit>
INFO: XNIO Version 3.0.3.GA
Aug 22, 2013 10:39:06 AM org.xnio.nio.NioXnio <clinit>
INFO: XNIO NIO Implementation Version 3.0.3.GA
Aug 22, 2013 10:39:06 AM org.jboss.remoting3.EndpointImpl <clinit>
INFO: JBoss Remoting version 3.2.3.GA
Aug 22, 2013 10:39:06 AM org.jboss.ejb.client.remoting.VersionReceiver handleMessage
INFO: Received server version 1 and marshalling strategies [river]
Aug 22, 2013 10:39:06 AM org.jboss.ejb.client.remoting.RemotingConnectionEJBReceiver associate
INFO: Successful version handshake completed for receiver context EJBReceiverContext{clientContext=org.jboss.ejb.client.EJBClientContext@84a74, receiver=Remoting connection EJB receiver [connection=Remoting connection <1a88328>,channel=jboss.ejb,nodename=inp-cc122858-2]} on channel Channel ID e600faf2 (outbound) of Remoting connection 0175650e to localhost/127.0.0.1:4447
Aug 22, 2013 10:39:06 AM org.jboss.ejb.client.remoting.ChannelAssociation$ResponseReceiver handleMessage
WARN: Unsupported message received with header 0xffffffff
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:662)
at javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:307)
at javax.naming.InitialContext.getURLOrDefaultInitCtx(InitialContext.java:344)
at javax.naming.InitialContext.lookup(InitialContext.java:411)
at com.ibytecode.client.EJBApplicationClient.doLookup(EJBApplicationClient.java:83)
at com.ibytecode.client.EJBApplicationClient.main(EJBApplicationClient.java:32)
Exception in thread "main" java.lang.NullPointerException
at com.ibytecode.client.EJBApplicationClient.main(EJBApplicationClient.java:44)
Comments