我是个EJB3新手,还望各位大侠多多帮助。
现在的文件是这样的
example.session.stateful下边有4个文件Count.java,CountBean.java,CountCallbacks.java,CountClient.java
在META-INF下边有一个ejb-jar.xml,具体代码如下
/*********************************************************/
/*                       Count.java                     **/
/*********************************************************/package example.session.stateful;/**
* The business interface - a plain Java interface with only
* business methods.
*/
public interface Count {
/**
* Increments the counter by 1
*/
public int count();
/**
* Sets the counter to val
* @param val
*/
public void set(int val);
/**
* removes the counter
*/
public void remove();
}/*********************************************************/
/*                       CountBean.java                 **/
/*********************************************************/package example.session.stateful;import javax.ejb.*;
import javax.interceptor.Interceptors;
/**
 * A Stateful Session Bean Class that shows the basics of how to write a
 * stateful session bean.
 * 
 * This Bean is initialized to some integer value. It has a business method
 * which increments the value.
 * 
 * The annotations below declare that:
 * <ul>
 * <li>this is a Stateful Session Bean
 * <li>the bean’s remote business interface is <code>Count</code>
 * <li>any lifecycle callbacks go to the class <code>CountCallbacks</code>
 * </ul>
 */
@Stateful
@Remote(Count.class)
@Interceptors(CountCallbacks.class)
public class CountBean implements Count {
/** The current counter is our conversational state. */
private int val; /**
 * The count() business method.
 */
public int count() {
System.out.println("count()");
return ++val;
} /**
 * The set() business method.
 */
public void set(int val) {
this.val = val;
System.out.println("set()");
} /**
 * The remove method is annotated so that the container knows it can remove
 * the bean after this method returns.
 */
@Remove
public void remove() {
System.out.println("remove()");
}
}/*********************************************************/
/*                      CountCallbacks.java             **/
/*********************************************************/
package example.session.stateful;import javax.ejb.PostActivate;
import javax.ejb.PrePassivate;
import javax.interceptor.InvocationContext;
import javax.annotation.PostConstruct; 
import javax.annotation.PreDestroy;
/**
* This class is a lifecycle callback interceptor for the Count
* bean. The callback methods simply print a message when
* invoked by the container.
*/
public class CountCallbacks {
/**
* Called by the container after construction
*/
@PostConstruct
public void construct(InvocationContext ctx) {
System.out.println("cb:construct()");
}
/**
* Called by the container after activation
*/
@PostActivate
public void activate(InvocationContext ctx) {
System.out.println("cb:activate()");
}
/**
* Called by the container before passivation
*/
@PrePassivate
public void passivate(InvocationContext ctx) {
System.out.println("cb:passivate()");
}
/**
* Called by the container before destruction
*/
@PreDestroy
public void destroy(InvocationContext ctx) {
System.out.println("cb:destroy()");
}
}META-INF/ejb-jar.xml
<?xml version="1.0" encoding="UTF-8" ?>
<ejb-jar xmlns="http://java.sun.com/xml/ns/javaee" version="3.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/ejb-jar_3_0.xsd">
<description>Stateful Session Bean Example</description>
<display-name>Stateful Session Bean Example</display-name>
<enterprise-beans>
<session>
<ejb-name>CountBean</ejb-name>
<business-remote>
example.session.stateful.Count
</business-remote>
<ejb-class>
example.session.stateful.CountBean
</ejb-class>
<session-type>Stateful</session-type>
<transaction-type>Container</transaction-type>
</session>
</enterprise-beans>
<interceptors>
<interceptor>
<interceptor-class>
example.session.stateful.CountCallbacks
</interceptor-class>
<post-construct>
<lifecycle-callback-method>
construct
</lifecycle-callback-method>
</post-construct>
<post-activate>
<lifecycle-callback-method>
activate
</lifecycle-callback-method>
</post-activate>
<pre-passivate>
<lifecycle-callback-method>
passivate
</lifecycle-callback-method>
</pre-passivate>
</interceptor>
</interceptors>
<assembly-descriptor>
<interceptor-binding>
<ejb-name>CountBean</ejb-name>
<interceptor-class>
example.session.stateful.CountCallbacks
</interceptor-class>
</interceptor-binding>
</assembly-descriptor>
</ejb-jar>
我用的是Oracle workshop for weblogic IDE,我run-server把这个ejb部署到服务器上,然后启动下边的客户端代码
/*********************************************************/
/*                      CountClient.java               **/
/*********************************************************/package example.session.stateful;import java.util.Hashtable;
import java.util.Properties;import javax.naming.*;/**
 * This class is a simple client for a stateful session bean.
 * 
 * To illustrate how passivation works, configure your EJB server to allow only
 * 2 stateful session beans in memory. (Consult your vendor documentation for
 * details on how to do this.) We create 3 beans in this example to see how and
 * when beans are passivated.
 */
public class CountClient {
public static final int noOfClients = 3; public static void main(String[] args) {
try {
/* Get a reference to the bean */
Properties properties = new Properties();
properties.put(Context.INITIAL_CONTEXT_FACTORY,
"weblogic.jndi.WLInitialContextFactory");
properties.put(Context.PROVIDER_URL, "t3://127.0.0.1:7001"); Context ctx = new InitialContext(properties);
/* An array to hold the Count beans */
Count count[] = new Count[noOfClients];
int countVal = 0;
/* Create and count() on each member of array */
System.out.println("Instantiating beans...");
for (int i = 0; i < noOfClients; i++) {
count[i] = (Count) ctx.lookup(Count.class.getName());
/* initialize each bean to the current count value */
count[i].set(countVal);
/* Add 1 and print */
countVal = count[i].count();
System.out.println(countVal);
/* Sleep for 1/2 second */
Thread.sleep(100);
}
/*
 * Let’s call count() on each bean to make sure the beans were
 * passivated and activated properly.
 */
System.out.println("Calling count() on beans...");
for (int i = 0; i < noOfClients; i++) {
/* Add 1 and print */
countVal = count[i].count();
System.out.println(countVal);
/* let the container dispose of the bean */
count[i].remove();
/* Sleep */
Thread.sleep(50);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}报错:Exception in thread "Main Thread" java.lang.NoClassDefFoundError: weblogic/kernel/KernelStatus
at weblogic.jndi.Environment.<clinit>(Environment.java:78)
at weblogic.jndi.WLInitialContextFactory.getInitialContext(WLInitialContextFactory.java:117)
at javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:667)
at javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:288)
at javax.naming.InitialContext.init(InitialContext.java:223)
at javax.naming.InitialContext.<init>(InitialContext.java:198)
at example.session.stateful.CountClient.main(CountClient.java:31)