JavaEE(10) - Session EJB的依赖注入、引用及任务调度

时间:2023-01-19 21:31:41

1. EJB依赖注入

#1. EJB开发(Net Beans创建EJB Module, 项目名称:CallHello)

Hello.java

package org.crazyit.service;

import javax.ejb.*;

@Local
public interface Hello {
    public String hello(String name);
}

HelloBean.java

package org.crazyit.service;

import javax.ejb.*;

@Stateless(name = "Hello")
public class HelloBean implements Hello {
    public String hello(String name) {
        return name + ",您好,现在时间是" + new java.util.Date();
    }
}

CallHello.java

package org.crazyit.service;

import javax.ejb.*;

@Remote
public interface CallHello {
    String callHello(String name);
}

CallHelloBean.java

package org.crazyit.service;

import javax.ejb.*;

@Stateless(mappedName = "CallHello")
public class CallHelloBean implements CallHello {

    @EJB(beanName = "Hello")
    private Hello hello;

    public String callHello(String name) {
        final String prefix = "调用Hello EJB成功,返回值:";
        String result = hello.hello("yeeku");
        System.out.println(prefix + result);
        return prefix + result;
    }
}

#2. 客户端调用EJB (Net Beans创建Java Project: CallHelloTest)

CallHello.java

package org.crazyit.service;

public interface CallHello {
    String callHello(String name);
}

EjbClient.java

package lee;

import javax.rmi.*;
import javax.naming.*;
import java.util.Properties;

import org.crazyit.service.*;

public class EjbClient {

    public void test() throws NamingException {
        //获取WebLogic中JNDI服务的Context
        Context ctx = getInitialContext();
        CallHello ch = (CallHello) ctx.lookup("CallHello#org.crazyit.service.CallHello");
        System.out.println(ch.callHello("hehe!"));
    }

    //工具方法,用来获取WebLogic中JNDI服务的Context
    private Context getInitialContext() {
        // 参见(4)
    }

    public static void main(String[] args) throws Exception {
        EjbClient client = new EjbClient();
        client.test();
    }
}

2. 资源依赖注入

#1. EJB开发(Net Beans创建EJB Module, 项目名称:CMT2)

Cmt2.java

package org.crazyit.service;

import javax.ejb.*;

@Remote
public interface Cmt2 {
    void insert();
}

Cmt2Bean.java

package org.crazyit.service;

import java.util.*;
import javax.ejb.*;
import javax.annotation.*;
import javax.naming.*;

import java.sql.*;
import javax.sql.*;

@Stateless(mappedName = "Cmt2")
@TransactionManagement(TransactionManagementType.CONTAINER)
@TransactionAttribute(TransactionAttributeType.REQUIRED)
public class Cmt2Bean implements Cmt2 {

    @Resource(mappedName="javaee")
    private DataSource ds = null;
    @Resource
    private SessionContext sessCtx;

    public void insert() {
        try {
            Connection conn = ds.getConnection();
            Statement stmt = conn.createStatement();
            int result = stmt.executeUpdate("insert into user_table values(null, '新用户' , 'bbb')");
            System.out.println(result);
            stmt.close();
            conn.close();
        } 
        catch (SQLException ex) {
            ex.printStackTrace();
            sessCtx.setRollbackOnly();
        }
    }
}

#2. 客户端调用EJB (Net Beans创建Java Project: CMT2Test)

Cmt2.java

package org.crazyit.service;

public interface Cmt2 {
    void insert();
}

EjbClient.java

package lee;

import javax.naming.*;
import java.util.Properties;

import org.crazyit.service.*;

public class EjbClient {

    public void test() throws NamingException {
        //获取WebLogic中JNDI服务的Context
        Context ctx = getInitialContext();
        Cmt2 cmt2 = (Cmt2) ctx.lookup("Cmt2#org.crazyit.service.Cmt2");
        cmt2.insert();
    }

    //工具方法,用来获取WebLogic中JNDI服务的Context
    private Context getInitialContext() {
        // 参见(4)
    }

    public static void main(String[] args) throws Exception {
        EjbClient client = new EjbClient();
        client.test();
    }
}

3. 配置EJB引用

#1. EJB开发(Net Beans创建EJB Module, 项目名称:EJBRef)

Hello.jav(参见10.1)

HelloBean.java(参见10.1)

EJBRef.java

package org.crazyit.service;

import javax.ejb.*;

@Remote
public interface EJBRef {
    String callHello(String name);
}

EJBRefBean.java

package org.crazyit.service;

import javax.ejb.*;

@Stateless(mappedName = "EJBRef", name = "EJBRef")
public class EJBRefBean implements EJBRef {

    @EJB(name = "ejb3/Hello")
    private Hello hello;

    public String callHello(String name) {
        final String prefix = "调用Hello EJB成功,返回值:";
        String result = hello.hello("yeeku");
        System.out.println(prefix + result);
        return prefix + result;
    }
}

META-INF/ejb-jar.xml

<?xml version="1.0" encoding="UTF-8"?>
<ejb-jar xmlns="http://java.sun.com/xml/ns/javaee"
    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"
    version="3.0">
    <enterprise-beans>
        <session>
            <ejb-name>EJBRef</ejb-name>
            <ejb-local-ref>
                <ejb-ref-name>ejb3/Hello</ejb-ref-name>
                <ejb-ref-type>local</ejb-ref-type>
                <local>org.crazyit.service.Hello</local>
            </ejb-local-ref>
        </session>
    </enterprise-beans>
</ejb-jar>

#2. EJB开发(Net Beans创建EJB Module, 项目名称:EJBRef2)

Hello.jav(参见#1)

HelloBean.java(参见#1)

EJBRef.java(参见#1)

EJBRefBean.java

package org.crazyit.service;

import javax.ejb.*;

@Stateless(mappedName="EJBRef")
/* 通过@EJB配置一个EJB引用 */
@EJB(name="ejb3/Hello" , beanInterface=Hello.class)
public class EJBRefBean implements EJBRef {
    //通过依赖注入来注入指定EJB
    @EJB(name="ejb3/Hello")
    private Hello hello;
        
    public String callHello(String name) {
        final String prefix = "调用Hello EJB成功,返回值:";
        String result = hello.hello("yeeku");
        System.out.println(prefix + result);
        return prefix + result;
    }
}

#3. 客户端调用EJB (Net Beans创建Java Project: EJBRefTest)

EJBRef.java

package org.crazyit.service;

public interface EJBRef {
    String callHello(String name);
}

EjbClient.java

package lee;

import javax.naming.*;
import java.util.Properties;

import org.crazyit.service.*;

public class EjbClient {

    public void test() throws NamingException {
        //获取WebLogic中JNDI服务的Context
        Context ctx = getInitialContext();
        EJBRef ch = (EJBRef) ctx.lookup("EJBRef#org.crazyit.service.EJBRef");
        System.out.println(ch.callHello("hehe!"));
    }

    //工具方法,用来获取WebLogic中JNDI服务的Context
    private Context getInitialContext() {
        // 参见(4)
    }

    public static void main(String[] args) throws Exception {
        EjbClient client = new EjbClient();
        client.test();
    }
}

4. 使用计时器进行任务调度

#1. EJB开发(Net Beans创建EJB Module, 项目名称:TimerEJB)

TimerEJB.java

package org.crazyit.service;

import javax.ejb.*;

@Remote
public interface TimerEJB {
    void setTime(java.util.Date init, long interval);
    void check(Timer timer);
}

TimerEJBBean.java

package org.crazyit.service;

import javax.ejb.*;
import javax.annotation.*;

@Stateless(mappedName = "TimerEJB")
public class TimerEJBBean implements TimerEJB {

    //定义设置定时器的方法
    @Resource
    TimerService timerService;

    public void setTime(java.util.Date init, long interval) {
        //启动一个每隔interval时间就会启动定时器
        timerService.createTimer(init, interval, "新的定时器!");
    }

    //定义定时执行的方法
    @Timeout
    public void check(Timer timer) {
        System.out.println("定时器信息:" + timer.getInfo());
        System.out.println("模拟系统检查!");
    }
}

#2. 客户端调用EJB (Net Beans创建Java Project: TimerEJBClient)

TimerEJB.java

package org.crazyit.service;

import java.util.Timer;

public interface TimerEJB {
    void setTime(java.util.Date init, long interval);
    void check(Timer timer);
}

EJBClient.java

package lee;

import javax.naming.*;
import java.util.Properties;

import org.crazyit.service.*;

public class EjbClient {

    public void test() throws NamingException {
        //获取WebLogic中JNDI服务的Context
        Context ctx = getInitialContext();
        TimerEJB te = (TimerEJB) ctx.lookup("TimerEJB#org.crazyit.service.TimerEJB");
        //调用EJB中设置定时器的方法
        te.setTime(new java.util.Date(), 1500);
        System.out.println("客户端让定时器每个1.5s触发一次");
    }

    //工具方法,用来获取WebLogic中JNDI服务的Context

    private Context getInitialContext() {
        // 参见(4)
    }

    public static void main(String[] args) throws Exception {
        EjbClient client = new EjbClient();
        client.test();
    }
}