6,786
社区成员
发帖
与我相关
我的任务
分享run:
[java] Saving customer to node1 = localhost:1099
[java] Exception in thread "main" javax.naming.NameNotFoundException: Entit
yTestBean not bound
[java] at org.jnp.server.NamingServer.getBinding(NamingServer.java:771)
[java] at org.jnp.server.NamingServer.getBinding(NamingServer.java:779)
[java] at org.jnp.server.NamingServer.getObject(NamingServer.java:785)
[java] at org.jnp.server.NamingServer.lookup(NamingServer.java:396)
[java] at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
[java] at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAcces
sorImpl.java:39)
[java] at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMet
hodAccessorImpl.java:25)
[java] at java.lang.reflect.Method.invoke(Method.java:597)
[java] at sun.rmi.server.UnicastServerRef.dispatch(UnicastServerRef.jav
a:305)
[java] at sun.rmi.transport.Transport$1.run(Transport.java:159)
[java] at java.security.AccessController.doPrivileged(Native Method)
[java] at sun.rmi.transport.Transport.serviceCall(Transport.java:155)
[java] at sun.rmi.transport.tcp.TCPTransport.handleMessages(TCPTranspor
t.java:535)
[java] at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run0(TCP
Transport.java:790)
[java] at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(TCPT
ransport.java:649)package org.jboss.tutorial.cachedentity.bean;
import java.io.Serializable;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue; import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.PostLoad;
import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;
/**
*
* @author <a href="mailto:kabir.khan@jboss.org">Kabir Khan</a>
* @version $Revision$
*/
@Entity
@Cache (usage=CacheConcurrencyStrategy.TRANSACTIONAL)
public class Contact implements Serializable
{
Long id;
String name;
String tlf;
Customer customer;
public Contact()
{
}
@Id @GeneratedValue(strategy=GenerationType.IDENTITY)
public Long getId()
{
return id;
}
public void setId(Long long1)
{
id = long1;
}
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
public String getTlf()
{
return tlf;
}
public void setTlf(String tlf)
{
this.tlf = tlf;
}
@ManyToOne
@JoinColumn(name="CUST_ID")
public Customer getCustomer()
{
return customer;
}
public void setCustomer(Customer customer)
{
this.customer = customer;
}
}
package org.jboss.tutorial.cachedentity.bean;
import java.util.Set;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue; import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import javax.persistence.PostLoad;
import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;
/**
* Company customer
*
* @author Emmanuel Bernard
* @author Kabir Khan
*/
@Entity
@Cache (usage=CacheConcurrencyStrategy.TRANSACTIONAL)
public class Customer implements java.io.Serializable
{
Long id;
String name;
private Set<Contact> contacts;
public Customer()
{
}
@Id @GeneratedValue(strategy=GenerationType.IDENTITY)
public Long getId()
{
return id;
}
public void setId(Long long1)
{
id = long1;
}
public String getName()
{
return name;
}
public void setName(String string)
{
name = string;
}
@Cache (usage=CacheConcurrencyStrategy.TRANSACTIONAL)
@OneToMany(mappedBy="customer", fetch=FetchType.EAGER, cascade=CascadeType.ALL)
public Set<Contact> getContacts()
{
return contacts;
}
public void setContacts(Set<Contact> contacts)
{
this.contacts = contacts;
}
}package org.jboss.tutorial.cachedentity.bean;
/**
* Comment
*
* @author <a href="mailto:bill@jboss.org">Bill Burke</a>
* @version $Revision$
*/
public interface EntityTest
{
Customer createCustomer();
Customer findByCustomerId(Long id);
}package org.jboss.tutorial.cachedentity.bean;
import java.util.HashSet;
import java.util.Set;
import javax.ejb.Remote;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import org.jboss.logging.Logger;
/**
* Comment
*
* @author <a href="mailto:bill@jboss.org">Bill Burke</a>
* @version $Revision$
*/
@Stateless
@Remote(EntityTest.class)
public class EntityTestBean implements EntityTest
{
@PersistenceContext
private EntityManager manager;
/**
* Logger
*/
private Logger logger = Logger.getLogger(EntityTestBean.class);
public Customer createCustomer()
{
Customer customer = new Customer();
customer.setName("JBoss");
Set<Contact> contacts = new HashSet<Contact>();
Contact kabir = new Contact();
kabir.setCustomer(customer);
kabir.setName("Kabir");
kabir.setTlf("1111");
contacts.add(kabir);
Contact bill = new Contact();
bill.setCustomer(customer);
bill.setName("Bill");
bill.setTlf("2222");
contacts.add(bill);
customer.setContacts(contacts);
manager.persist(customer);
logger.info("Created customer named " + customer.getName() + " with " + customer.getContacts().size() + " contacts");
return customer;
}
public Customer findByCustomerId(Long id)
{
logger.info("Find customer with id = " + id);
Customer customer = manager.find(Customer.class, id);
logger.info("Customer with id = " + id + " found");
return customer;
}
}
package org.jboss.tutorial.cachedentity.client;
import java.util.Properties;
import java.util.Set;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import org.jboss.tutorial.cachedentity.bean.Contact;
import org.jboss.tutorial.cachedentity.bean.Customer;
import org.jboss.tutorial.cachedentity.bean.EntityTest;
/**
*
* @author <a href="mailto:kabir.khan@jboss.org">Kabir Khan</a>
* @version $Revision$
*/
public class CachedEntityRun
{
public static void main(String[] args)throws NamingException
{
if (args.length != 2)
{
throw new RuntimeException("You need to pass in two parameters of the type ipaddress:port");
}
Properties prop1 = new Properties();
prop1.put("java.naming.factory.initial", "org.jnp.interfaces.NamingContextFactory");
prop1.put("java.naming.factory.url.pkgs", "org.jboss.naming:org.jnp.interfaces");
prop1.put("java.naming.provider.url", "jnp://" + args[0]);
Properties prop2 = new Properties();
prop2.put("java.naming.factory.initial", "org.jnp.interfaces.NamingContextFactory");
prop2.put("java.naming.factory.url.pkgs", "org.jboss.naming:org.jnp.interfaces");
prop2.put("java.naming.provider.url", "jnp://" + args[1]);
System.out.println("Saving customer to node1 = " + args[0]);
InitialContext ctx1 = new InitialContext(prop1);
EntityTest tester1 = (EntityTest)ctx1.lookup("EntityTestBean/remote");
Customer customer = tester1.createCustomer();
customer = tester1.findByCustomerId(customer.getId());
System.out.println("Looking for customer on node2 = " + args[1] + " (should be available in cache)");
InitialContext ctx2 = new InitialContext(prop2);
EntityTest tester2 = (EntityTest)ctx2.lookup("EntityTestBean/remote");
Set<Contact> contacts = customer.getContacts();
customer = tester2.findByCustomerId(customer.getId());
if (customer == null)
{
throw new RuntimeException("Customer was not found in node2 = " + args[1]);
}
System.out.println("Found customer on node2 (cache). Customer details follow:");
System.out.println("Customer: id=" + customer.getId() + "; name=" + customer.getName());
for (Contact contact : contacts)
{
System.out.println("\tContact: id=" + contact.getId() + "; name=" + contact.getName());
}
}
}
<?xml version="1.0"?>
<!-- ======================================================================= -->
<!-- JBoss build file -->
<!-- ======================================================================= -->
<project name="JBoss" default="ejbjar" basedir=".">
<property environment="env"/>
<property name="src.dir" value="${basedir}/src"/>
<property name="jboss.home" value="${env.JBOSS_HOME}"/>
<property name="jboss.server.config" value="all"/>
<property name="build.dir" value="${basedir}/build"/>
<property name="build.classes.dir" value="${build.dir}/classes"/>
<property name="build.artifact" value="jboss-ejb3-tutorial-cachedentity.jar"/>
<!-- Build classpath -->
<path id="classpath">
<!-- So that we can get jndi.properties for InitialContext -->
<pathelement location="${basedir}"/>
<!-- Only the jbossall-client.jar should ideally be sufficient -->
<fileset dir="${jboss.home}/client">
<include name="**/jbossall-client.jar"/>
</fileset>
<!-- Hibernate core classes -->
<fileset dir="${jboss.home}/common/lib">
<include name="hibernate-core.jar"/>
<include name="ejb3-persistence.jar"/>
</fileset>
<fileset dir="${jboss.home}/server/${jboss.server.config}/lib">
<include name="**/*.jar"/>
</fileset>
<pathelement location="${build.classes.dir}"/>
</path>
<property name="build.classpath" refid="classpath"/>
<!-- =================================================================== -->
<!-- Prepares the build directory -->
<!-- =================================================================== -->
<target name="prepare">
<mkdir dir="${build.dir}"/>
<mkdir dir="${build.classes.dir}"/>
</target>
<!-- =================================================================== -->
<!-- Compiles the source code -->
<!-- =================================================================== -->
<target name="compile" depends="prepare">
<javac srcdir="${src.dir}"
destdir="${build.classes.dir}"
debug="on"
deprecation="on"
optimize="off"
includes="**">
<classpath refid="classpath"/>
</javac>
</target>
<target name="ejbjar" depends="compile">
<jar jarfile="build/${build.artifact}">
<fileset dir="${build.classes.dir}">
<include name="**/*.class"/>
</fileset>
<fileset dir=".">
<include name="META-INF/*.*"/>
</fileset>
</jar>
<copy file="build/${build.artifact}" todir="${jboss.home}/server/${jboss.server.config}/deploy"/>
</target>
<target name="run" depends="ejbjar">
<java classname="org.jboss.tutorial.cachedentity.client.CachedEntityRun" fork="yes" dir=".">
<!-- node 1 -->
<arg value="localhost:1099"/>
<!--<arg value="192.168.1.1:1099"/>-->
<!-- node 2 -->
<arg value="localhost:1099"/>
<!--<arg value="192.168.1.2:1099"/>-->
<classpath refid="classpath"/>
</java>
</target>
<!-- =================================================================== -->
<!-- Cleans up generated stuff -->
<!-- =================================================================== -->
<target name="clean.db">
<delete dir="${jboss.home}/server/${jboss.server.config}/data/hypersonic"/>
</target>
<target name="clean">
<delete dir="${build.dir}"/>
<delete file="${jboss.home}/server/${jboss.server.config}/deploy/${build.artifact}"/>
</target>
</project>