- 浏览: 34323 次
- 性别:
- 来自: 合肥
-
博客专栏
-
-
我和Java数据库操作的那...
浏览量:9314
最新评论
-
ivanlw:
楼主用的这个snapito好神奇啊……是直接把图片链接设置成他 ...
ibookmark.me上线了! -
succinite:
使用TortoiseGit, 出现以下错误。fatal: ht ...
一个简单的JAVA后台程序框架 -
mazhiyuan:
引用很显然,之前所说的梦想,并非是真正心中所想,而只不过想“找 ...
也谈梦想 -
cevin15:
说到我心里去了。现在处于离职状态。也是对前途一片迷茫~
也谈梦想 -
lwjlaser:
lettoo 写道lwjlaser 写道这篇博客第一个示例代码 ...
我和JAVA数据库操作的那些事儿(3)
本项目已经通过git进行版本管理,checkout:http://github.com/lettoo/orion
项目要实现一个java后台程序,提供start(),stop()和status()的方法。
主要的原理是:有一个Server类运行在localhost:xxxx上,当成一个RMI service,这个Server类里有一个Service List,通过Spring注入的方式,向其中注入Service,当然,也可以通过编程方式动态往里面加Service,因为项目比较简单,直接通过Spring配置已经满足要求。RMI 客户端通过调用Server.start(),遍历Service List中的service,一个个启动起来。客户端调用stop(),则遍历Service List中的server,一个个关闭,最终再关闭服务端。status()方法只是简单的提供Server是否在运行状态。
1. Server接口
public interface IServer { void start() throws Exception; void stop() throws Exception; boolean isStarted(); }
2. Server实现类
import java.util.LinkedList; import java.util.List; public class ServerImpl implements IServer { private volatile boolean isStarted = false; private List<IService> serviceList = new LinkedList<IService>(); public void setServiceList(List<IService> serviceList) { this.serviceList = serviceList; } @Override public void start() throws Exception { if (isStarted) { return; } for (IService service : serviceList) { service.start(); } isStarted = true; } @Override public void stop() throws Exception{ for (IService service : serviceList) { service.stop(); } isStarted = false; System.exit(0); } @Override public boolean isStarted() { return isStarted; } }
3. 通过Spring RMI,分别创建两个xml,一个是server.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE beans PUBLIC "-//SPRING/DTD BEAN/EN" "http://www.springframework.org/dtd/spring-beans.dtd"> <beans> <bean id="server" class="cn.lettoo.server.ServerImpl"> <property name="serviceList"> <list> <!-- Add your service here, all service implements IService interface. --> <ref bean="jmsService" /> <ref bean="jobService" /> <ref bean="dataReaderService" /> </list> </property> </bean> <bean id="orionMonitorServer" class="org.springframework.remoting.rmi.RmiServiceExporter"> <property name="serviceInterface" value="cn.lettoo.server.IServer" /> <property name="service" ref="server" /> <property name="serviceName" value="OrionMonitorServer" /> <property name="registryPort" value="1199" /> <property name="alwaysCreateRegistry" value="true" /> </bean> </beans>
再创建一个client.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE beans PUBLIC "-//SPRING/DTD BEAN/EN" "http://www.springframework.org/dtd/spring-beans.dtd"> <beans> <bean id="serviceProxy" class="org.springframework.remoting.rmi.RmiProxyFactoryBean"> <property name="serviceUrl" value="rmi://localhost:1199/OrionMonitorServer" /> <property name="serviceInterface" value="cn.lettoo.server.IServer" /> </bean> </beans>
4. 写一个Luncher类,通过RMI来调用Server的方法
import org.apache.commons.cli.BasicParser; import org.apache.commons.cli.CommandLine; import org.apache.commons.cli.CommandLineParser; import org.apache.commons.cli.HelpFormatter; import org.apache.commons.cli.Options; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class Launcher { /** The Constant USAGE. */ private static final String USAGE = "[options]"; /** The Constant HEADER. */ private static final String HEADER = "Options:"; /** The Constant FOOTER. */ private static final String FOOTER = "For more instructions, please contact golettoo@gmail.com"; private IServer server; private static Log logger = LogFactory.getLog(Launcher.class); public static void main(String args[]) { Launcher launcher = new Launcher(); try { launcher.execute(args); } catch (Exception e) { launcher.printUsage(launcher.createOptions()); logger.error("launch error: ", e); System.exit(1); } } private void execute(String[] args) throws Exception { CommandLineParser parser = new BasicParser(); Options options = createOptions(); CommandLine commandLine = parser.parse(options, args); if (commandLine.hasOption('h')) { printUsage(options); return; } if (commandLine.hasOption('v')) { System.out.println("Version: 1.0"); return; } if (args.length == 0) { start(); } else if (commandLine.hasOption("start")) { start(); } else if (commandLine.hasOption("stop")) { stop(); } else if (commandLine.hasOption("status")) { System.out.println("Status: " + getStatus()); } else { printUsage(options); } } private void initServer() throws Exception { ApplicationContext context = new ClassPathXmlApplicationContext(new String[] { "server.xml" }); } private void initClient() throws Exception { ApplicationContext context = new ClassPathXmlApplicationContext(new String[] { "client.xml" }); this.server = (IServer) context.getBean("serviceProxy"); } private void start() throws Exception { initServer(); initClient(); server.start(); } private void stop() { try { initClient(); server.stop(); } catch (Exception e) { logger.error("Stop server error: " + e.toString()); } } private String getStatus() { try { initClient(); return server.isStarted() ? "started" : "stopped"; } catch (Exception e) { logger.error("Get server status error: " + e.toString()); } return "stopped"; } private void printUsage(Options options) { HelpFormatter helpFormatter = new HelpFormatter(); helpFormatter.printHelp(USAGE, HEADER, options, FOOTER); } private Options createOptions() { Options options = new Options(); options.addOption("h", "help", false, "print this usage information"); options.addOption("o", "option", true, "set option"); options.addOption("v", "version", false, "print version"); options.addOption("start", false, "start the application"); options.addOption("stop", false, "stop the application"); options.addOption("status", false, "print the status information"); return options; } }
Luncher类通过运行时的参数来决定是start还是stop,另外,还提供了一个帮助和版本信息的查看
现在可以通过运行Luncher -start来启动这个server了
再通过Luncher -status来看状态
通过Luncher -stop来关闭server
评论
1 楼
succinite
2011-11-19
使用TortoiseGit, 出现以下错误。
fatal: http://github.com/lettoo/orion/info/refs not found: did you run git update-server-info on the server?
fatal: http://github.com/lettoo/orion/info/refs not found: did you run git update-server-info on the server?
发表评论
-
ibookmark.me上线了!
2012-04-10 23:47 1503记得上次是2009年,接触到了python,gae,于是乎动手 ... -
使用virtualenv开发django应用
2012-01-12 11:03 1741Virtualenv是一个非常好的virtual python ... -
在Amazon EC2上试用play framework
2011-11-11 17:00 1378几个月以前,我在 ... -
一个Log生成工具小项目的实现
2011-11-01 17:32 1335这两天的主要工作是用java写一个log生成工具,用于 ... -
自定义log4j生成的log文件名
2011-11-01 13:57 2943很多时候,log4j的RollingFileAppen ... -
我和java操作数据库那些事儿(5)
2011-10-26 16:56 1618引用 半自动化武器来了:Spring JdbcTemplate ... -
我和JAVA数据库操作的那些事儿(4)
2011-10-24 16:21 1482通过前面几篇的介绍,对于JDBC的使用应该基本上够上项目开发的 ... -
我和JAVA数据库操作的那些事儿(3)
2011-10-20 15:35 2466在前面的两篇文章中,第一篇主要是讲了在jdbc编程中容 ... -
我和JAVA数据库操作的那些事儿(2)
2011-10-20 11:15 1539摘要 写道 上一篇提到的几个问题,在本篇有具体的代码。本篇后 ... -
图说事务隔离级别
2011-10-19 21:38 1395我们经常说的事务隔离级别,一般指的是SQL-92 ... -
我和JAVA数据库操作的那些事儿(1)
2011-10-19 15:26 2209摘要 我开始接触jdbc的 ... -
复习:观察者模式
2011-10-17 17:02 961观察者模式(有时又被称为发布/订阅模式)是软件设计模式 ... -
复习:代理模式
2011-10-17 15:53 706代理模式是常用的Java 设计模式,它的特征是代理类与 ... -
对chainsaw中一个简单Job Scheduler的扩展
2011-10-14 23:36 1024今天在看apache chainsaw这个项目的源代码 ... -
Spring RMI 简单实现
2011-10-14 13:47 1026好久没有写java代码了,最近工作项目上需要做一个 ... -
一道关于树的面试题
2011-10-13 15:31 914记得不久以前有道面试题,要求下面的数据结构 ... -
测试驱动开发:红、绿、重构
2011-10-12 23:01 866在读Ruby on Rails Tutorial: ... -
Spring JMS和ActiveMQ的应用
2011-10-12 22:43 2017笔者近期参与一个分析log的项目。主要流程是:读取Lo ...
相关推荐
java权限后台开发框架,采用spring + srpingsecurity + springMVC + Hibernate + freemarker + jquery 等技术实现,页面及源文件打包发布程序。 完整的功能应用,包括:后台权限、人员机构、参数代码、角色权限、...
微信商城 小程序 前后台源码 Java后台 ssm框架+MySQL 毕业设计 前后台源码
项目简介:Java开发的一个简约后台管理系统 1.需求定义:外包项目如雨后春笋,开发工期被迫压缩,为了开发人员专注开发项目业务,早点下班能陪老婆、孩子。 2.产品定位: 简约后台管理系统 3.项目特点:此项目代码...
数据库交互是 Java 后台部署 HTML 页面代码的另一个重要组成部分。在给定的代码中,我们可以看到使用了静态模型(StaticModel)来存储 HTML 页面的元数据。静态模型是一个 JavaBean 对象,用于存储 HTML 页面的元...
Java 棋牌游戏框架 WG棋牌包网项目, java服务器.支持单台游戏服务器一到两千人同时在线. 包含: 1.游戏基础框架 2.网关,路由通信模块 3.游戏机器人框架 4.大厅,房间,桌子流程 5.完善的统一棋牌控制库存框架,支持游戏...
Java网络程序:前台、后台框架示例,使用Java源码编写的前台与后台程序,第一次写的程序,可能不是很完善,敬请谅解! 此程序是用工厂模式来写的,分为独立的前台和后台!
微信小程序商城+java后台.zip小程序源码打包下载适合研究学习技术微信小程序商城+java后台.zip小程序源码打包下载适合研究学习技术 1.有可能需要安装微擎框架安装微擎小程序。 2.适合程序员学习研究用 3.适合小公司...
项目采用SpringBoot框架,可直接运行,更改WxPayConfig文件和WeixinController文件appid、mch_id、key、notify_ur等参数即可完成小程序支付和公众号支付及回调功能。包含统一下单(支付接口)即WeixinController中...
希望广学习Java的朋友关注,这是Javaweb的好看的后台管理框架,希望在拿走的同时大家关注我,我将持续为大家分享我的资源
java 后台
后台管理系统、后台管理框架、JAVA管理后台、Springboot管理后台、前后端分离管理后台、Vue3管理后台、Vue.js管理后台、Element UI管理后台.likeadmin已经搭建好前后端分离的底层,包含程序安装、登录、登出、工作...
微信点餐,带支付系统。 一个完整的微信小程序点餐项目。java后台 微信小程序实现点餐系统
毕业设计,基于java语言,ssm框架和微信小程序开发的托福资料(完整带Java后台).7z
微信小程序,小程序商城,商城,springboot框架,vue管理系统,java后台.zip
三大框架是用来开发web应用程序中使用的. Struts:基于MVC的充当了其中的试图层和控制器 Hibernate:做持久化的,对JDBC轻量级的封装,使得我们能过面向对象的操作数据库 Spring: 采用了控制反转的技术,管理Bean,降低了...
微信小程序项目实战,微信小程序课程设计,基于微信小程序开发的,含有简单代码注册,下载下来简单部署即可使用。 包含:项目源码、数据库脚本、部署说明等,该项目可以作为课程设计使用,前后端代码都在里面。 ...
java 企业官网源代码公司门户网站模板源码带后台SSM框架开发建设 源码名称: 通用性企业门户网站 适用版本: URF-8(整站) 适用站点: 企业官网、门户网站 技术支撑: 如需技术服务需另付费,请联络QQ。 部署工具:...