ejb3 小结6

5 篇文章 0 订阅

1 ejb的依赖注入
   @Stateless
@Remote ({Injection.class})
public class InjectionBean implements Injection {
    @EJB (beanName="HelloWorldBean")
    HelloWorld helloworld;
  
    public String SayHello() { 
        return helloworld.SayHello("注入者");
    }

  这里注入了另一个bean helloworldbean了,还可以用
mappedName="HellWorldBean/remote
来指定JNDI
 

2 定时服务
    例子:
   

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

@Stateless
@Remote ({TimerService.class})
public class TimerServiceBean implements TimerService {
   
    private int count = 1;
    private @Resource SessionContext ctx;
   
    public void scheduleTimer(long milliseconds){
        count = 1;
        ctx.getTimerService().createTimer(new Date(new Date().getTime() + milliseconds),milliseconds, "大家好,这是我的第一个定时器");
    }

    @Timeout
    public void timeoutHandler(Timer timer)
    {
       System.out.println("---------------------");
       System.out.println("定时器事件发生,传进的参数为: " + timer.getInfo());
       System.out.println("---------------------");

       if (count>=5){
           timer.cancel();//如果定时器触发5次,便终止定时器          
       }
       count++;
    }
}
通过依赖注入@Resource SessionContext ctx,我们获得SessionContext对象,调用ctx.getTimerService().createTimer
(Date arg0, long arg1, Serializable arg2)方法创建定时器,三个参数的含义如下:
Date arg0 定时器启动时间,如果传入时间小于现在时间,定时器会立刻启动。
long arg1 间隔多长时间后再次触发定时事件。单位:毫秒
Serializable arg2 你需要传给定时器的参数,该参数必须实现Serializable接口。
当定时器创建完成后,我们还需声明定时器方法。定时器方法的声明很简单,只需在方法上面加入@Timeout 注
释,另外定时器方法必须遵守如下格式:
void XXX(Timer timer)
在定时事件发生时,此方法将被执行。

   在客户端调用时,可以这样
    try {
InitialContext ctx = new InitialContext(props);
TimerService timer = (TimerService) ctx.lookup("TimerServiceBean/remote");
timer.scheduleTimer((long)3000);

}

 

3 日期型的注释
    
    @Temporal(value=TemporalType.DATE)
    public Date getBirthday() {
        return birthday;
    }

   @Temporal默认为timestamp映射,有三个可以选TemporalType.DATE,TemporalType.time,TemporalType.timestamp

 

4 @lob
   如果是大的LOB,最好用延迟加载了
   @Lob
    @Basic(fetch=FetchType.Lazy)
  ......

 

5 命名参数查询
    Query query=em.createQuery("select p from Person P where p.personid=:Id");

     query.setParameter("Id",new Integer(1));

 

6 查询部分属性
   Query query = em.createQuery("select p.personid, p.name from Person p order by p.personid desc ");
        //集合中的元素不再是Person,而是一个Object[]对象数组
        List result = query.getResultList();
              if (result!=null){
            Iterator iterator = result.iterator();           
            while( iterator.hasNext() ){
                //取每一行
                Object[] row = ( Object[]) iterator.next();
                //数组中的第一个值是personid
                int personid = Integer.parseInt(row[0].toString());
                String PersonName = row[1].toString();
                out.append("personid="+ personid+ "; Person Name="+PersonName+ "<BR>");   
            }
        }

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值