设为首页 收藏本站
查看: 1341|回复: 0

[经验分享] jbuilder2006+jboss+db2开发EJB

[复制链接]

尚未签到

发表于 2016-11-15 06:55:35 | 显示全部楼层 |阅读模式
  做web程序这么久,一直把精力放在了前台的展现层,后台都没有怎么去做,除了前一段改进了一个系统的底层架构设计,优化了hibernate和log4j,更改了一些可能会导致内存泄露的地方。ejb更是一直没有用过ejb,最近看了ejb3.0介绍,感觉还不错,准备开始新一轮的修炼。
  首先到网上下载了最新的jboss4.0.4,配置jbuilder2006的server,全部默认就可以了,具体就不说了。
  先要创建一个EJBModule,然后用向导建一个session bean,名称为:HelloBean。
  可以看到生成了3个文件:Hello.java,HelloBean.java,HelloHome.java
  修改Hello接口为:
  package net.fangw;
  import javax.ejb.EJBObject;
import java.rmi.RemoteException;
import javax.ejb.CreateException;
  public interface Hello extends EJBObject {
String hello(String name) throws RemoteException,CreateException;
}
  修改HelloBean类为:
  package net.fangw;
  import javax.ejb.SessionBean;
import javax.ejb.SessionContext;
import javax.ejb.CreateException;
  public class HelloBean implements SessionBean {
SessionContext sessionContext;
public void ejbCreate() throws CreateException {
}
  public void ejbRemove() {
}
  public void ejbActivate() {
}
  public void ejbPassivate() {
}
  public void setSessionContext(SessionContext sessionContext) {
this.sessionContext = sessionContext;
}
  //业务方法
public String hello(String name) {
System.out.println("hello," + name + "!");
return "hello," + name + "!";
}
  }

  保存,编译,然后在EJBModule上点右键,把它deploy。
  在工程里新建一个jboss的server,并运行,一般是不会有什么问题的,这样就发布了。
  测试EJB:
  创建EJB测试类HelloBeanTestClient1.java:
  package net.fangw;
  import javax.naming.*;
import javax.rmi.PortableRemoteObject;
import java.lang.String;
import javax.naming.InitialContext;
import java.util.Hashtable;
import javax.ejb.*;
import java.rmi.*;
  /**
* <p>Title: ejb test</p>
*
* <p>Description: </p>
*
* <p>Copyright: Copyright (c) 2006</p>
*
* <p>Company: fangw</p>
*
* @author fangw
* @version 1.0
*/
  
public class HelloBeanTestClient1 {
private static final String ERROR_NULL_REMOTE = "Remote interface reference is null. It must be created by calling one of the Home interface methods first.";
private static final int MAX_OUTPUT_LINE_LENGTH = 100;
private boolean logging = true;
private Hello hello = null;
private HelloHome helloHome = null;
  //Construct the EJB test client
public HelloBeanTestClient1() {
initialize();
}
  public void initialize() {
long startTime = 0;
if (logging) {
log("Initializing bean access.");
startTime = System.currentTimeMillis();
}
  try {
  //get naming context
Context context = getInitialContext();
//look up jndi name
Object ref = context.lookup("HelloBean");
//look up jndi name and cast to Home interface
helloHome = (HelloHome) PortableRemoteObject.narrow(ref, HelloHome.class);
if (logging) {
log(
"Succeeded initializing bean access through Home interface.");
long endTime = System.currentTimeMillis();
log("Execution time: " + (endTime - startTime) + " ms.");
}
  } catch (Exception e) {
if (logging) {
log("Failed initializing bean access.");
}
e.printStackTrace();
}
  }
  private Context getInitialContext() throws NamingException {
Hashtable environment = new Hashtable();
  environment.put(Context.INITIAL_CONTEXT_FACTORY,
"org.jnp.interfaces.NamingContextFactory");
environment.put(Context.URL_PKG_PREFIXES,
"org.jboss.naming:org.jnp.interfaces");
environment.put(Context.PROVIDER_URL, "jnp://localhost:1099");
  return new InitialContext(environment);
}
  //----------------------------------------------------------------------------
// Methods that use Home interface methods to generate a Remote interface reference
//----------------------------------------------------------------------------
  public Hello create() {
long startTime = 0;
if (logging) {
log("Calling create()");
startTime = System.currentTimeMillis();
}
  try {
hello = helloHome.create();
if (logging) {
log("Succeeded: create()");
long endTime = System.currentTimeMillis();
log("Execution time: " + (endTime - startTime) + " ms.");
}
} catch (Exception e) {
if (logging) {
log("Failed : create()");
}
e.printStackTrace();
}
if (logging) {
log("Return value from create(): " + hello + ".");
}
return hello;
}
  //----------------------------------------------------------------------------
// Methods that use Remote interface methods to access data through the bean
//----------------------------------------------------------------------------
  public String hello(String name) {
String returnValue = "";
if (hello == null) {
System.out.println("Error in hello(): " + ERROR_NULL_REMOTE);
return returnValue;
}
long startTime = 0;
if (logging) {
log("Calling hello(" + name + ")");
startTime = System.currentTimeMillis();
}
  try {
returnValue = hello.hello(name);
if (logging) {
log("Succeeded: hello(" + name + ")");
long endTime = System.currentTimeMillis();
log("Execution time: " + (endTime - startTime) + " ms.");
}
} catch (Exception e) {
if (logging) {
log("Failed : hello(" + name + ")");
}
e.printStackTrace();
}
if (logging) {
log("Return value from hello(" + name + "): " + returnValue + ".");
}
return returnValue;
}
  public void executeRemoteCallsWithDefaultArguments() {
if (hello == null) {
System.out.println(
"Error in executeRemoteCallsWithDefaultArguments(): " +
ERROR_NULL_REMOTE);
return;
}
  try {
hello("");
} catch (Exception e) {
e.printStackTrace();
}
}
  //----------------------------------------------------------------------------
// Utility Methods
//----------------------------------------------------------------------------
  private void log(String message) {
  if (message == null) {
System.out.println("-- null");
}
if (message.length() > MAX_OUTPUT_LINE_LENGTH) {
System.out.println("-- " +
message.substring(0, MAX_OUTPUT_LINE_LENGTH) +
" ...");
} else {
System.out.println("-- " + message);
}
}
  //Main method
public static void main(String[] args) {
HelloBeanTestClient1 client = new HelloBeanTestClient1();
try {
client.create();
client.hello("fangw");
client.hello.remove();
} catch (RemoveException ex) {
ex.printStackTrace();
} catch (RemoteException ex) {
ex.printStackTrace();
}
// Use the client object to call one of the Home interface wrappers
// above, to create a Remote interface reference to the bean.
// If the return value is of the Remote interface type, you can use it
// to access the remote interface methods. You can also just use the
// client object to call the Remote interface wrappers.
}
}

  保存,编译,运行。
  如果出现:
  -- Initializing bean access.
log4j:WARN No appenders could be found for logger (org.jboss.security.SecurityAssociation).
log4j:WARN Please initialize the log4j system properly.
-- Succeeded initializing bean access through Home interface.
-- Execution time: 2113 ms.
-- Calling create()
-- Succeeded: create()
-- Execution time: 261 ms.
-- Return value from create(): HelloBean:Stateless.
-- Calling hello(fangw)
-- Succeeded: hello(fangw)
-- Execution time: 10 ms.
-- Return value from hello(fangw): hello,fangw!.

  那就ok了,可以睡觉了,今天就到这里。

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-300382-1-1.html 上篇帖子: DB2 常用SQL Quick Find 下篇帖子: db2 行列互换示例
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表