可以在源圖上右鍵,選Web服務--添加操作,也可以在設計圖上直接添加操作。@WebService標注表明該類是一個web服務,展現給web服務客戶端的業務方法必須使用@WebMethod標注來表示。打包部署該web應用,web服務自動會發布。可以在glassfish應用服務器上找到該web服務,直接測試或者查看服務器生成的WSDL
也可以編寫客戶端測試,新建一個普通的java項目,在項目上右鍵,選擇新建--Web服務客戶端,在彈出窗口中指定WebService項目或者WSDL url,點擊完成。在源代碼上右鍵,選擇Web服務客戶端資源--調用Web服務操作,在彈出窗口中選擇sayHi操作,點確定,測試代碼自動生成:
public interface TestRemote extends Remote { ... }就算程序裡面沒寫繼承Remote, container還是會在byte code階段插入繼承Remote的動作
@Stateful(name="SimpleStatefulBean", mappedName="ejb/SimpleStatefulBean") public class SimpleStatefulBean implements SimpleStatefulRemote { private Logger logger = Logger.getLogger("SimpleStatefulBean"); private byte[] b = new byte[100000]; { for ( int i = 0; i < b.length; i++ ) { b[i] = (byte) 100; } } public String simpleShow() { return this + ":This is simple show" + b; } @PostConstruct public void postConstruct() { logger.info("create " + this); } @PreDestroy public void preDestroy() { logger.info("destroy " + this); } @PostActivate public void postActivate() { logger.info("activate " + this); } @PrePassivate public void prePassivate() { logger.info("passivate " + this); } @Remove public void remove() { logger.info("remove " + this); } }放interceptor的lifecycle callback
@Stateless
@Interceptors(value={SimpleInterceptor.class})
public class SimpleStatelessBean implements SimpleStatelessLocal {
private Logger logger = Logger.getLogger("SimpleStatelessBean");
@Resource(name="TestQueueConnectionFactory")
private ConnectionFactory connectionFactory;
@Resource(name="jms/TestQueueDestination")
private Destination destination;
public String simpleShow() {
try {
Connection conn = connectionFactory.createConnection();
Session session = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
MessageProducer messageProducer = session.createProducer(destination);
TextMessage message = session.createTextMessage();
message.setText("This is text message");
messageProducer.send(message);
messageProducer.close();
session.close();
conn.close();
} catch (JMSException ex) {
throw new RuntimeException( ex );
}
return this + ":This is simple show";
}
}
public class SimpleInterceptor {
Logger logger = Logger.getLogger("SimpleStatefulBeanInterceptor");
@PostConstruct
public void onCreate(InvocationContext ic) {
try {
logger.info("create " + this);
ic.proceed();
} catch (Exception ex) {
Logger.getLogger(SimpleInterceptor.class.getName()).log(Level.SEVERE, null, ex);
}
}
@AroundInvoke
public Object aroundInvoke(InvocationContext ctx) throws Exception {
logger.info(ctx + " is invoked.");
return ctx.proceed();
}
@PreDestroy
public void onDestroy(InvocationContext ic) {
try {
logger.info("destroy " + this);
ic.proceed();
} catch (Exception ex) {
Logger.getLogger(SimpleInterceptor.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
stateful bean的@PrePassivate, @PostActivate也可放interceptor
@Interceptors(value={SimpleInterceptor.class})
@Stateful(name="SimpleStatefulBean", mappedName="ejb/SimpleStatefulBean")
public class SimpleStatefulBean implements SimpleStatefulRemote {
private byte[] b = new byte[100000];
{
for ( int i = 0; i < b.length; i++ ) {
b[i] = (byte) 100;
}
}
public String simpleShow() {
return this + ":This is simple show" + b;
}
@Remove
public void remove() {
Logger.getLogger("SimpleStatefulBean").info("remove " + this);
}
}
public class SimpleInterceptor {
@PostConstruct
public void onCreate(InvocationContext ic) {
try {
Logger.getLogger(SimpleInterceptor.class.getName()).info("create " + this);
ic.proceed();
} catch (Exception ex) {
Logger.getLogger(SimpleInterceptor.class.getName()).log(Level.SEVERE, null, ex);
}
}
@PostActivate
public void onActivate(InvocationContext ic) {
try {
Logger.getLogger(SimpleInterceptor.class.getName()).info("activate " + this);
ic.proceed();
} catch (Exception ex) {
Logger.getLogger(SimpleInterceptor.class.getName()).log(Level.SEVERE, null, ex);
}
}
@PrePassivate
public void onPassivate(InvocationContext ic) {
try {
Logger.getLogger(SimpleInterceptor.class.getName()).info("passivate " + this);
ic.proceed();
} catch (Exception ex) {
Logger.getLogger(SimpleInterceptor.class.getName()).log(Level.SEVERE, null, ex);
}
}
@AroundInvoke
public Object aroundInvoke(InvocationContext ctx) throws Exception {
Logger.getLogger(SimpleInterceptor.class.getName()).info(ctx + " is invoked.");
return ctx.proceed();
}
@PreDestroy
public void onDestroy(InvocationContext ic) {
try {
Logger.getLogger(SimpleInterceptor.class.getName()).info("destroy " + this);
ic.proceed();
} catch (Exception ex) {
Logger.getLogger(SimpleInterceptor.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
@Stateless(name="SimpleStatelessBean1") public class SimpleStatelessBean1 implements SimpleStatelessLocal { ... } @Stateless(name="SimpleStatelessBean2") public class SimpleStatelessBean2 implements SimpleStatelessLocal { ... } public class SimpleStatelessServlet extends HttpServlet { @EJB(beanName="SimpleStatelessBean1") private SimpleStatelessLocal simpleStatelessLocal1; @EJB(beanName="SimpleStatelessBean2") private SimpleStatelessLocal simpleStatelessLocal2; ... }