JavaEE(8) - 本地和远程调用的有状态以及无状态Session EJB
1. 使用NetBeans开发Session Bean
#1. 创建项目:File-->New Project-->Java EE-->EJB Module
#2. 在项目中创建Session Bean: 右击项目-->New-->Session Bean-->SessionType(Stateless/Stateful)-->Create Interface(Remote/Local)
2. 开发远程调用的无状态Session Bean
#1. 开发EJB(Net Beans创建EJB Module, 项目名称:Hello)
Hello.java
package org.crazyit.service; import javax.ejb.*; @Remote
public interface Hello {
public String hello(String name);
}
HelloBean.java
package org.crazyit.service; import javax.ejb.*; @Stateless(mappedName = "Hello")
public class HelloBean implements Hello { public String hello(String name) {
return "Hello " + name + ", current time is " + new java.util.Date();
}
}
#2. 客户端调用EJB (Net Beans创建Java Project: EJBClient)
hello.java
package org.crazyit.service; import javax.ejb.*; @Remote
public interface Hello {
public String hello(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();
Hello hello = (Hello) ctx.lookup("Hello#org.crazyit.service.Hello");
//调用WebLogic容器中EJB的业务方法
System.out.println(hello.hello("Ben"));
} //工具方法,用来获取WebLogic中JNDI服务的Context
private Context getInitialContext() {
// 参加(4)
} public static void main(String[] args) throws Exception {
EjbClient client = new EjbClient();
client.test();
}
}
3. 开发本地调用的无状态Session Bean(Net Beans创建Enterprise Application, 项目名称:CatServiceEAR)
#1. CatServiceEAR-ejb
Cat.java
package org.crazyit.business;
public class Cat {
private String name;
private int age;
//无参数的构造器
public Cat() {
}
//初始化全部属性的构造器
public Cat(String name, int age) {
this.name = name;
this.age = age;
}
//name属性的setter和getter方法
public void setName(String name) {
this.name = name;
}
public String getName() {
return this.name;
}
//age属性的setter和getter方法
public void setAge(int age) {
this.age = age;
}
public int getAge() {
return this.age;
}
}
Person.java
package org.crazyit.business;
public class Person {
private Integer id;
private String name;
//无参数的构造器
public Person() {
}
//初始化全部属性的构造器
public Person(Integer id, String name) {
this.id = id;
this.name = name;
}
//id属性的setter和getter方法
public void setId(Integer id) {
this.id = id;
}
public Integer getId() {
return this.id;
}
//name属性的setter和getter方法
public void setName(String name) {
this.name = name;
}
public String getName() {
return this.name;
}
public boolean equals(Object target) {
if (this == target) {
return true;
}
if (target.getClass() == Person.class) {
Person p = (Person) target;
if (p.getId() == this.getId()) {
return true;
}
}
return false;
}
public int hashCode() {
return this.getId();
}
}
CatService.java
package org.crazyit.service; import javax.ejb.*;
import org.crazyit.business.*; @Local
public interface CatService {
Cat[] getCats(Person owner);
}
CatServiceBean.java
package org.crazyit.service; import java.util.*;
import javax.ejb.*; import org.crazyit.business.*; @Stateless(mappedName = "CatService")
public class CatServiceBean implements CatService { static Map<Person, Cat[]> catsInfo; static {
catsInfo = new HashMap<Person, Cat[]>();
catsInfo.put(new Person(1, "孙悟空"), new Cat[]{
new Cat("Kitty", 2),
new Cat("Garfield", 4),});
catsInfo.put(new Person(2, "猪八戒"), new Cat[]{
new Cat("Tom", 2),
new Cat("机器猫", 4),});
} public Cat[] getCats(Person owner) {
return catsInfo.get(owner);
}
}
#2. CatServiceEAR-war
index.jsp
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@page import="org.crazyit.service.*, org.crazyit.business.*" %>
<%@page import="javax.naming.*" %> <%
InitialContext ctx = new InitialContext();
CatService catService = (CatService)ctx.lookup("CatService#org.crazyit.service.CatService"); Cat[] cats = catService.getCats(new Person(1, "Ben"));
for (int i = 0; i < cats.length; i++){
out.println("The age of " + cats[i].getName() + " is: " + cats[i].getAge() + "<br/>");
}
%>
4. Annotation与部署描述文件(Net Beans创建EJB Module, 项目名称:CatServiceXML)
CatService.jar
package org.crazyit.service;
import org.crazyit.business.*;
public interface CatService {
Cat[] getCats(Person owner);
}
CatServiceBean.java
package org.crazyit.service;
import java.util.*;
import org.crazyit.business.*;
public class CatServiceBean implements CatService {
static Map<Person, Cat[]> catsInfo;
static {
catsInfo = new HashMap<Person, Cat[]>();
catsInfo.put(new Person(1, "孙悟空"), new Cat[]{
new Cat("Kitty", 2),
new Cat("Garfield", 4),});
catsInfo.put(new Person(2, "猪八戒"), new Cat[]{
new Cat("Tom", 2),
new Cat("机器猫", 4),});
}
public Cat[] getCats(Person owner) {
return catsInfo.get(owner);
}
}
Source Packages/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>catService</ejb-name>
<mapped-name>catService</mapped-name>
<business-local>org.crazyit.service.CatService</business-local>
<ejb-class>org.crazyit.service.CatServiceBean</ejb-class>
<session-type>Stateless</session-type>
</session>
</enterprise-beans>
</ejb-jar>
5. 开发远程调用的有状态Session Bean
#1. 开发EJB(Net Beans创建EJB Module, 项目名称:ShopService)
ShopService.java
package org.crazyit.service; import javax.ejb.*;
import java.util.*; @Remote
public interface ShopService {
void addItem(String item);
Map<String, Integer> showDetail();
}
ShopServiceBean.java
package org.crazyit.service; import java.util.*;
import javax.ejb.*; @Stateful(mappedName = "ShopService")
public class ShopServiceBean implements ShopService { private Map<String, Integer> buyInfo = new HashMap<String, Integer>(); public void addItem(String item) {
//该物品已经购买过
if (buyInfo.containsKey(item)) {
buyInfo.put(item, buyInfo.get(item) + 1);
}
else {
buyInfo.put(item, 1);
}
} public Map<String, Integer> showDetail() {
return buyInfo;
}
}
#2. 开发Web应用(Net Beans创建Java Web Project 项目名称:ShopServiceTest)
shop.jsp
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>选择物品购买</title>
</head>
<body>
<form method="post" action="processBuy.jsp">
书籍:<input type="checkbox" name="item" value="book"><br/>
电脑:<input type="checkbox" name="item" value="computer"><br/>
汽车:<input type="checkbox" name="item" value="car"><br/>
<input type="submit" value="购买">
</form>
</body>
</html>
processBuy.jsp
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@ page import="java.util.*,org.crazyit.service.*,javax.naming.*"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>购买的物品列表</title>
</head>
<body>
<%
ShopService ss = (ShopService) session.getAttribute("ss");
if (ss == null) {
Context ctx = new InitialContext();
//通过JNDI查找EJB的引用
Object stub = ctx.lookup("ShopService#org.crazyit.service.ShopService");
ss = (ShopService) stub;
session.setAttribute("ss", ss);
} String[] buys = request.getParameterValues("item"); for (String item : buys) {
ss.addItem(item);
}
%>
您所购买的物品:<br/>
<%
Map<String, Integer> buyInfo = ss.showDetail(); for (String item : buyInfo.keySet()) {
out.println(item + "的数量为:" + buyInfo.get(item) + "<br />");
}
%>
<hr/>
<a href="shop.jsp">再次购买</a>
</body>
</html>
JavaEE(8) - 本地和远程调用的有状态以及无状态Session EJB的更多相关文章
- storm drpc分布式本地和远程调用模式讲解
一.drpc 的介绍 1.rpc RPC(Remote Procedure Call)—远程过程调用,它是一种通过网络从远程计算机程序上请求服务,而不需要了解底层网络技术的协议. 2.drpc drp ...
- java RMI 远程调用
1.背景 在学习代理模式的过程中接触到了远程调用,jdk有自己的RMI实现,所以这边自己实现了RMI远程调用,并记录下心得. 感受最深的是RMI和现在的微服务有点相似,都是通过"注册中心&q ...
- openoffice excel word 转换pdf 支持本地调用和远程调用
OpenOffice.org 是一套跨平台的办公室软件套件,能在Windows.Linux.MacOS X (X11)和 Solaris 等操作系统上执行.它与各个主要的办公室软件套件兼容.OpenO ...
- 远程调用cmd更新本地jar
最近遇到一个项目需求需要实现远程更新,但是本地项目无法更新自己,这让博主很是头疼,既然自己无法更新自己的话,那就自建新的项目,通过本地项目来调用新项目接口来更新本地项目. 代码如下: /** * 重启 ...
- JAVAEE——BOS物流项目08:配置代理对象远程调用crm服务、查看定区中包含的分区、查看定区关联的客户
1 学习计划 1.定区关联客户 n 完善CRM服务中的客户查询方法 n 在BOS项目中配置代理对象远程调用crm服务 n 调整定区关联客户页面 n 实现定区关联客户 2.查看定区中包含的分区 n 页面 ...
- 【Java EE 学习 78 中】【数据采集系统第十天】【Spring远程调用】
一.远程调用概述 1.远程调用的定义 在一个程序中就像调用本地中的方法一样调用另外一个远程程序中的方法,但是整个过程对本地完全透明,这就是远程调用.spring已经能够非常成熟的完成该项功能了. 2. ...
- 架构师之路-在Dubbo中开发REST风格的远程调用
架构师之路:从无到有搭建中小型互联网公司后台服务架构与运维架构 http://www.roncoo.com/course/view/ae1dbb70496349d3a8899b6c68f7d10b 概 ...
- 【Rest】在Dubbo中开发REST风格的远程调用(RESTful Remoting)
目录 概述 REST的优点 应用场景 快速入门 标准Java REST API:JAX-RS简介 REST服务提供端详解 HTTP POST/GET的实现 Annotation放在接口类还是实现类 J ...
- Spring集成RMI实现远程调用
前提: 1.开发工具: jdk tomcat ecplise,开发工具的使用本篇不做介绍. 2.需具备以下知识:javase servelt web rmi spring maven 一.关于RMI ...
随机推荐
- 局域网连接SQL Server数据库配置
首先要保证两台机器位于同一局域网内,然后打开配置工具→SQL Server配置管理器进行配置.将MSSQLSERVER的协议的TCP/IP的(IP1.IP2)TCPport改为1433,已启用改为是. ...
- 看PHP在内部迭代的动作
以下我们来了解怎样实现一个自己定义的迭代器,然后再開始慢慢理解迭代器的内部工作原理.先来看一个官方的样例: <? php class myIterator implements Iterator ...
- 【原创】shadowebdict开发日记:基于linux的简明英汉字典(三)
全系列目录: [原创]shadowebdict开发日记:基于linux的简明英汉字典(一) [原创]shadowebdict开发日记:基于linux的简明英汉字典(二) [原创]shadowebdic ...
- SharePoint综合Excel数据与Excel Web Access Web部分
SharePoint综合Excel数据与Excel Web Access Web部分 Excel Web Access Web零件SharePoint于Excel以电子形式提交数据. 1. 打开Exc ...
- 使用GDAL图书馆RPC校正问题
很快就会GDAL库更新1.11版本号之后,在发现之前写RPC像方误差修正模型校准结果特别大(在更新结果之前的版本号和PCI结果一致).所以初步推断是GDAL库的bug,经过各个參数改动发现原来是指定的 ...
- Android学习路径(十)如何将Action Bar堆放在布局
默认情况下,action bar出如今activity窗体的顶部,稍微降低了activity布局的总空间.假设你想隐藏或者显示action bar,在这堂用户体验的课程中,你能够通过调用hide() ...
- Phone Number 2010年山东省第一届ACM大学生程序设计竞赛
Phone Number Time Limit: 1000MS Memory limit: 65536K 题目描述 We know that if a phone number A is anothe ...
- 乐在其中设计模式(C#) - 模板方法模式(Template Method Pattern)
原文:乐在其中设计模式(C#) - 模板方法模式(Template Method Pattern) [索引页][源码下载] 乐在其中设计模式(C#) - 模板方法模式(Template Method ...
- Android网络图片显示在ImageView 上面
在写这篇博文的时候,我參与了一个项目的开发,里面涉及了非常多网络调用相关的问题,我记得我在刚刚開始做android项目的时候,以前就遇到这个问题,当时在网上搜索了一下,发现了一篇博文,如今与大家分享一 ...
- YT新人之巅峰大决战04
Problem Description Eddy's interest is very extensive, recently he is interested in prime number. Ed ...