EJB开发

 大家一至认为EJB是比较重量级组件,没有spring+Hibernate+struts2来的轻量,但是工作中,在大型企业级应用过程中,使用轻量级的开源框架有时,或多或少会有一些问题,比如:集群环境下并发,事务,分布式。等问题是比交头疼的一件事,为了解决这些问题,我们会到处找相应的开源产品或者自己实现(一般不可能),最近,在工作中发现,项目越来越大,在200人以上人的开发规模是:项目管理、模块划分、版本控制,异构系统交互发面;暴露了大量的问题,由于,我们的开发模式是比较的传统的瀑布模式:一般流程如下:分析需求----->需求评审------》设计-----》设计评审-----》写伪代码-------》开发--------》测试(压力测试和单元测试)--------》评审测试报告--------》发布申请--------》发布。最初为了进入迅速电子商务的行业项目的架构不是很好,比如,模块划分,项目的规划等问题没有考虑周全,现在发现,每天的访问量独立IP占到40W,加上没过一个时间就会有活动,所以开发新功能是难免的,那问题来了,我们花了大量的时间在优化网站的性能、数据库的性能,但是,在优化的过程中发现,模块

的紧耦合导致,有些优化方案无能为力,所有,最近,老大们在想,我们现有的机构已经不符合我们的业务发展速度了,因为我们花了大量的人力和物力,效果不是很明显,所以,决定要从新开发 ,由于,我们决定发展的方向是云计算,所以,为了适应中模式,我个人认为,我们技术选型,EJB3.0是肯定了,因为EJB3.0的编程模型和EJB1.X 和EJB2.X相比,有了很好的改善,个人认为:我们的架构会趋向模块化,系统化,服务化,这真是云计算SOA体系的核心,对于业务BEAN,我们会采用分布式部署:当然也是独立化,比如:我们会按照一定的业务某块,单独在服务器上只部署EJB,在另外机器上部署其他项目,今天晚了,明天继续。。。

1、ant脚本

 

<?xml version="1.0" encoding="UTF-8"?>
<project name="ejb学习" default="begin" basedir=".">
	<property name="build.dir" value="${basedir}/build" />
	<property name="build.classes.dir" value="${build.dir}/classes" />
	<target name="begin" depends="jar,deploy" description="开始">
		<echo message="开始了--------------"></echo>
	 <echo message="结束了--------------"></echo>
	</target>
	<!--初始化-->
	<target name="init" description="初始化" depends="clean">
		<mkdir dir="${build.dir}" />
		<mkdir dir="${build.classes.dir}" />
		<mkdir dir="${basedir}/dist" />
	</target>
	<!--编译-->
	<target name="compile" description="编译">
		<javac srcdir="${basedir}/src" destdir="${build.classes.dir}">
			<classpath>
				<pathelement path="${basedir}/lib/javaee.jar" />
				<pathelement path="${basedir}/lib/jboss-annotations-ejb3.jar" />
			</classpath>
		</javac>
	</target>
	<!--打成JAR包-->
	<target name="jar" description="打包成jar" depends="init,compile">
		<jar destfile="${basedir}/dist/ejb.jar">
			<fileset dir="${build.classes.dir}">
				<include name="**/*.class" />
			</fileset>
			<metainf dir="${basedir}/src/META-INF">
				<include name="**" />
			</metainf>
		</jar>
		<jar destfile="${basedir}/dist/ejbclient.jar">
			<fileset dir="${build.classes.dir}">
				<include name="**/ejb/*.class" />
			</fileset>
		</jar>
		<jar destfile="${basedir}/dist/ejbentity.jar">
			<fileset dir="${build.classes.dir}">
				<include name="**/entity/*.class" />
			</fileset>
		</jar>
	</target>
	<!--清理-->
	<target name="clean" description="清理描述">
		<delete dir="${build.dir}" />
		<delete dir="${basedir}/dist" />
		<delete file="J:\ejb3.0\ejbstudy\server\jboss-4.2.0.GA\server\default\deploy\ejb.jar"/>
	</target>
	<!--部署EJB-->
	<fileset dir="${basedir}/dist" includes="ejb.jar" id="source.fileset" />
	<target name="deploy"  description="部署EJB">
			<copy todir="J:\ejb3.0\ejbstudy\server\jboss-4.2.0.GA\server\default\deploy\">
				<fileset refid="source.fileset" />
			</copy>
		</target>
</project>

 2、第一个EJB

 

package com.dongwenhua.ejb;

public interface HelloWorld {

	public String sayHello(String name);
}

 

package com.dongwenhua.impl;

import javax.ejb.Remote;
import javax.ejb.Stateless;

import com.dongwenhua.ejb.HelloWorld;
@Stateless
@Remote(HelloWorld.class)
public class HelloWorldBean implements HelloWorld {

	public String sayHello(String name) {
		
		return name+":说你好吗,这是我第一个EJB哦!";
	}

}
 
	@Test
	public void test01() throws NamingException {
		InitialContext ctx = new InitialContext();
		HelloWorld hello;
		try {
			hello = (HelloWorld) ctx.lookup("HelloWorldBean/remote");
			String say = hello.sayHello("dongwenhua");
			System.out.println(say);
		} catch (NamingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

 jndi.properties

 

java.naming.factory.initial=org.jnp.interfaces.NamingContextFactory
java.naming.provider.url=localhost
java.naming.factory.url.pkgs=org.jboss.naming:org.jnp.interfaces

 3、定时服务

package com.dongwenhua.ejb;

public interface TimeService {

	public void scheduleTimer(long milliseconds);
}
  
package com.dongwenhua.impl;

import java.util.Date;

import javax.annotation.Resource;
import javax.ejb.Remote;
import javax.ejb.SessionContext;
import javax.ejb.Stateless;
import javax.ejb.Timeout;
import javax.ejb.Timer;

import com.dongwenhua.ejb.TimeService;
@Stateless
@Remote(TimeService.class)
public class TimeServiceBean implements TimeService {

	private int count=1;
	@Resource
	private SessionContext ctx;
	public void scheduleTimer(long milliseconds) {
		count=1;
		ctx.getTimerService().createTimer(new Date(new Date().getTime()+milliseconds), milliseconds,"大家好这是我的第一个定时器!");
		
	}
	@Timeout
	public void timeoutHandle(Timer timer){
		System.out.println("------------");
		System.out.println("定时器事件发生,传进的参数为:"+timer.getInfo());
		System.out.println("------------");
		if(count>=5){
			timer.cancel();
		}
		count++;
	}
}

 EJB注入

package com.dongwenhua.ejb;

public interface Injection {

	public String sayHello(String name);
}
  
package com.dongwenhua.impl;

import javax.ejb.EJB;
import javax.ejb.Remote;
import javax.ejb.Stateless;

import com.dongwenhua.ejb.HelloWorld;
import com.dongwenhua.ejb.Injection;

@Stateless
@Remote(Injection.class)
public class InjectionBean implements Injection {
	@EJB(name="HelloWorldBean")
	private HelloWorld helloWorld;
	public String sayHello(String name) {
		return this.helloWorld.sayHello(name+"@ejb");
	}

}

 拦击器

package com.dongwenhua.ejb;

public interface InterceptorOption {

	public String sayHello(String name);
	
	public String getName();
}
  
package com.dongwenhua.impl;

import javax.interceptor.AroundInvoke;
import javax.interceptor.InvocationContext;

public class HelloInterceptor {
	@AroundInvoke
	public Object log(InvocationContext ctx) throws Exception{
		System.out.println("HelloInterceptor beign......");
		long start = System.currentTimeMillis();
		if(ctx.getMethod().getName().equals("sayHello")){
			System.out.println("sayHello is invoke......");
		}
		if(ctx.getMethod().getName().equals("getName")){
			System.out.println("getName is invoke......");
		}
		long time = System.currentTimeMillis()-start;
		System.out.println("总共用了:"+time);
		return ctx.proceed();
		
	}
}
 
package com.dongwenhua.impl;

import javax.ejb.Remote;
import javax.ejb.Stateless;
import javax.interceptor.AroundInvoke;
import javax.interceptor.Interceptors;
import javax.interceptor.InvocationContext;

import org.jboss.annotation.ejb.RemoteBinding;

import com.dongwenhua.ejb.InterceptorOption;
@Stateless
@Remote(InterceptorOption.class)
@RemoteBinding(jndiBinding="interceptor/InterceptorBean")
//@Interceptors(HelloInterceptor.class)
public class InterceptorOptionBean implements InterceptorOption {

	public String getName() {
		// TODO Auto-generated method stub
		return "我很好!";
	}

	public String sayHello(String name) {
		// TODO Auto-generated method stub
		return name+":你好吗?";
	}
	@AroundInvoke
	public Object log(InvocationContext ctx) throws Exception{
		System.out.println("HelloInterceptor beign......");
		long start = System.currentTimeMillis();
		if(ctx.getMethod().getName().equals("sayHello")){
			System.out.println("sayHello is invoke......");
		}
		if(ctx.getMethod().getName().equals("getName")){
			System.out.println("getName is invoke......");
		}
		long time = System.currentTimeMillis()-start;
		System.out.println("总共用了:"+time);
		return ctx.proceed();
		
	}
}

 4、自定义JNDI名称

package com.dongwenhua.ejb;

public interface JndiBuildingRemoteOption {

	public String sayHello(String name);
}
package com.dongwenhua.ejb;

public interface JndiBuildingLocalOption {

	public String sayHello(String name);
}
  
package com.dongwenhua.impl;

import javax.ejb.Local;
import javax.ejb.Remote;
import javax.ejb.Stateless;

import org.jboss.annotation.ejb.LocalBinding;
import org.jboss.annotation.ejb.RemoteBinding;

import com.dongwenhua.ejb.JndiBuildingLocalOption;
import com.dongwenhua.ejb.JndiBuildingRemoteOption;
@Stateless
@Remote(JndiBuildingRemoteOption.class)
@RemoteBinding(jndiBinding="ejb/RemoteOption")
@Local(JndiBuildingLocalOption.class)
@LocalBinding(jndiBinding="ejb/LocalOption")
public class JndiBuildingOptionBean implements JndiBuildingLocalOption,JndiBuildingRemoteOption {

	public String sayHello(String name) {
		
		return name+":改变JNDI的名字!";
	}

}

 5、EJB生命周期

package com.dongwenhua.ejb;

public interface LifeCycle {

	public String sayHello(String name);
	
	public void stopSession();
}
  
package com.dongwenhua.impl;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.ejb.Init;
import javax.ejb.PostActivate;
import javax.ejb.Remote;
import javax.ejb.Remove;
import javax.ejb.Stateful;

import org.jboss.annotation.ejb.RemoteBinding;

import com.dongwenhua.ejb.LifeCycle;
@Stateful
@Remote(LifeCycle.class)
@RemoteBinding(jndiBinding="life/RemoteOption")
public class LifeCycleBean implements LifeCycle {

	public String sayHello(String name) {
		try {
			Thread.sleep(1000*30);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "这个是BENA生命周期的例子!";
	}

	@Init
	public void init(){
		System.out.println("init is begin....");
	}
	
	@PostConstruct
	public void construct(){
		System.out.println("construct");
	}
	@PreDestroy
	public void exit(){
		System.out.println("exit");
	}
	@PostActivate
	public void active(){
		System.out.println("active");
	}
	@Remove
	public void stopSession() {
		System.out.println("stopSession");

	}

}

 

 

 3

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值