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 ...
随机推荐
- [网络]_[0基础]_[使用putty备份远程数据]
场景: 1. putty是windows上訪问linux服务的免费client之中的一个.用它来ssh到远程server备份数据是常见的做法(在没做好自己主动备份机制前), 通过putty界面尽管也不 ...
- 深入探索C++对象模型-语义
有三种情况,这将是一个object的内容,以及一class object早期值: class X { ... }; X x; X xx = x; // 情况1,赋值对象 e ...
- Java Drp项目实战——Drp知多少
是什么 Drp是Distribution Resource Planning的缩写,意思是分销资源计划.它是用来管理企业的执行于Internet上的分销网络的系统,是以商业流程优化为基础,它的核心是销 ...
- Ubuntu 13.04 用户安装 gnome 3.8 桌面
昨天我试用了一把 Ubuntu gnome 13.04,如果你看了那一片文章:Ubuntu Gnome 13.04 体验截图.对 Ubuntu gnome 13.04 并不是采用的gnome 3.8 ...
- 玩转html5(二)----用canvas结合脚本在画布上画简单的图(html5又一强大功能)
在html5中可以使用canvas标签在画布上画图,先直接上代码,这篇文章先简单介绍一下canvas的使用方法,简单画几个圆,矩形,三角形,写字. 在代码中均给出了注释,在这里特别强调的一点是:使用c ...
- android AlarmManager采用
Android的闹钟实现机制非常easy, 仅仅须要调用AlarmManager.Set()方法将闹钟设置提交给系统,当闹钟时间到后,系统会依照我们的设定发送指定的广播消息.我们写一个广播去接收消息做 ...
- 剖析html对标准标签和自定义标签闭合与不闭合渲染问题
昨天在修改去年写的系统的时候无意中看到了当时写的一个利用标准标签未闭合在单元格内把整个单元格颜色渲染成红色的效果,如下: 当时的问题是从后台返回来的是个int整数而%是写在页面上的如图 这 时候就出现 ...
- 如何插上U盘 自动复制内容
U盘插入自动复制,有效对付那些不肯给PPT的老师,还有一定几率拿到期末考试卷子···有图有真相!业界良心的好东西!!现在看还来得及!!! 代码优化了一下,把不是很重要的都删掉了,这次使用时看不出任何现 ...
- 读书时间《JavaScript高级程序设计》五:DOM
DOM(文档对象模型)是针对HTML文档的一个API,描绘了一个层次化的节点树,可以添加.移除.修改页面的某一部分. 一个简单的文档结构 <!DOCTYPE html> <html& ...
- hdu3182 状态压缩水题
题意是这种 给你n个汉堡 每一个汉堡有它的价值 做每一个汉堡都得花费相应的能量 如今告诉你最大能量 让你求获得的最大的价值(有些汉堡必须有还有一些汉堡做好为前提) 给你的 ...