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 ...
随机推荐
- 跨平台移动框架iMAG开发入门
iMAG是一个非常简洁高效的移动跨平台开发框架,开发一次能够同一时候兼容Android和iOS平台,有点儿Web开发基础就能非常快上手.当前移动端跨平台开发的框架有非常多,但用iMAG另一个优点,就是 ...
- leetCode Min Stack解决共享
原标题:https://oj.leetcode.com/problems/min-stack/ Design a stack that supports push, pop, top, and ret ...
- hadoop版本号变迁
近期在研究hadoop时,发现hadoop的版本号非常混乱.原来的版本号都说0.X.X開始,后来发现有1.X.X和2.X.X 这样就不太清楚了. 0.20.2版本号曾经(不含该版本号)的配置文件都在d ...
- 工作经常使用的SQL整理,实战篇(二)
原文:工作经常使用的SQL整理,实战篇(二) 工作经常使用的SQL整理,实战篇,地址一览: 工作经常使用的SQL整理,实战篇(一) 工作经常使用的SQL整理,实战篇(二) 工作经常使用的SQL整理,实 ...
- go - 内置基础类型
Go 语言中包括以下内置基础类型: 布尔型:bool 整型:int int64 int32 int16 int8 uint8(byte) uint16 uint32 uint64 uint 浮点型:f ...
- 【Android进阶】在开发中Application到底用来做什么
首先,在一个Android程序中,有且只有一个Application对象,在程序开始跑起来的时候,首先执行Application的onCreate方法,这是一个Android应用的入口,在开发中,我们 ...
- linux下"="号与"=="号
<鸟哥的linux私房菜>基础学习篇P382中说,在bash中"="与"=="是同样的,都代表推断是否相等,仅仅只是因为其它语言一般写法使用&quo ...
- Codeforces 9A-Die Roll(意甲冠军)
A. Die Roll time limit per test 1 second memory limit per test 64 megabytes input standard input out ...
- 区分内边距与外边距padding和margin
以两个并排显示的div为例说明. 现在两个div都有背景颜色, 右边的div中有几行p, 若是想要使两个div之间有间隔, 即两块带了颜色区域之间产生空隙, 则给div的css中外边距margin赋值 ...
- 从头开始编写项目Makefile(八):型号规则
[版权声明:转载请保留源:blog.csdn.net/gentleliu.Mail:shallnew at 163 dot com] 上创建的文件夹谈及.没有生产目标文件到相应的文件夹,在这里 ...