Java:安排作业按时间间隔运行

最近,我花了一些时间围绕Neo4j版本之间的滚动升级构建了一组测试,作为其中的一部分,我想记录升级发生时的群集状态。

测试的主线程会等待升级完成,因此我想每隔几秒钟登录另一个线程。 Alistair向我指出了ScheduledExecutorService ,该服务效果很好。

我结束了一个大致如下的测试:

public class MyUpgradeTest {@Testpublic void shouldUpgradeFromOneVersionToAnother() throws InterruptedException{ScheduledExecutorService scheduledExecutorService = Executors.newSingleThreadScheduledExecutor();scheduledExecutorService.scheduleAtFixedRate( new LogAllTheThings(), 0, 1, TimeUnit.SECONDS );Thread.sleep(10000);// do upgrade of clusterscheduledExecutorService.shutdown();}static class LogAllTheThings implements Runnable{@Overridepublic void run(){Date time = new Date( System.currentTimeMillis() );try{Map<String, Object> masterProperties = selectedProperties( client(), URI.create( "http://localhost:7474/" ) );System.out.println( String.format( "%s: %s", time, masterProperties ) );}catch ( Exception ignored ){ignored.printStackTrace();}}private static Client client(){DefaultClientConfig defaultClientConfig = new DefaultClientConfig();defaultClientConfig.getClasses().add( JacksonJsonProvider.class );return Client.create( defaultClientConfig );}public static Map<String, Object> selectedProperties( Client client, URI uri ){Map<String, Object> jmxProperties = new HashMap<String, Object>();ArrayNode transactionsProperties = jmxBean( client, uri, "org.neo4j/instance%3Dkernel%230%2Cname%3DTransactions" );addProperty( jmxProperties, transactionsProperties, "LastCommittedTxId" );ArrayNode kernelProperties = jmxBean( client, uri, "org.neo4j/instance%3Dkernel%230%2Cname%3DKernel" );addProperty( jmxProperties, kernelProperties, "KernelVersion" );ArrayNode haProperties = jmxBean( client, uri, "org.neo4j/instance%3Dkernel%230%2Cname%3DHigh+Availability" );addProperty( jmxProperties, haProperties, "Role" );addProperty( jmxProperties, haProperties, "InstanceId" );return jmxProperties;}private static void addProperty( Map<String, Object> jmxProperties, ArrayNode properties, String propertyName ){jmxProperties.put( propertyName, getProperty( properties, propertyName ) );}private static String getProperty( ArrayNode properties, String propertyName ){for ( JsonNode property : properties ){if ( property.get( "name" ).asText().equals( propertyName ) ){return property.get( "value" ).asText();}}throw new RuntimeException( "Could not find requested property: " + propertyName );}private static ArrayNode jmxBean( Client client, URI uri, String beanExtension ){ClientResponse clientResponse = client.resource( uri + "db/manage/server/jmx/domain/" + beanExtension ).accept( MediaType.APPLICATION_JSON ).get( ClientResponse.class );JsonNode transactionsBean = clientResponse.getEntity( JsonNode.class );return (ArrayNode) transactionsBean.get( 0 ).get( "attributes" );}}
}

LogAllTheThings每秒被调用一次,它记录Neo4j服务器作为JMX属性公开的KernelVersion,InstanceId,LastCommittedTxId和Role。

如果我们对本地Neo4j集群运行它,我们将看到以下内容:

Sun Nov 17 22:31:55 GMT 2013: {KernelVersion=Neo4j - Graph Database Kernel 2.0.0-M06, InstanceId=1, LastCommittedTxId=18, Role=master}
Sun Nov 17 22:31:56 GMT 2013: {KernelVersion=Neo4j - Graph Database Kernel 2.0.0-M06, InstanceId=1, LastCommittedTxId=18, Role=master}
Sun Nov 17 22:31:57 GMT 2013: {KernelVersion=Neo4j - Graph Database Kernel 2.0.0-M06, InstanceId=1, LastCommittedTxId=18, Role=master}
Sun Nov 17 22:31:58 GMT 2013: {KernelVersion=Neo4j - Graph Database Kernel 2.0.0-M06, InstanceId=1, LastCommittedTxId=18, Role=master}
Sun Nov 17 22:31:59 GMT 2013: {KernelVersion=Neo4j - Graph Database Kernel 2.0.0-M06, InstanceId=1, LastCommittedTxId=18, Role=master}
...
removed for brevity

下一步是同时获取集群所有成员的属性,然后我们可以引入另一个ExecutorService ,该线程的线程池为3,以便它将同时评估(至少接近)每台计算机:

static class LogAllTheThings implements Runnable{private ExecutorService executorService = Executors.newFixedThreadPool( 3 );@Overridepublic void run(){List<URI> machines = new ArrayList<>(  );machines.add(URI.create( "http://localhost:7474/" ));machines.add(URI.create( "http://localhost:7484/" ));machines.add(URI.create( "http://localhost:7494/" ));Map<URI, Future<Map<String, Object>>> futureJmxProperties = new HashMap<>(  );for ( final URI machine : machines ){Future<Map<String, Object>> futureProperties = executorService.submit( new Callable<Map<String, Object>>(){@Overridepublic Map<String, Object> call() throws Exception{try{return selectedProperties( client(), machine );}catch ( Exception ignored ){ignored.printStackTrace();return new HashMap<>();}}} );futureJmxProperties.put( machine, futureProperties );}Date time = new Date( System.currentTimeMillis() );System.out.println( time );for ( Map.Entry<URI, Future<Map<String, Object>>> uriFutureEntry : futureJmxProperties.entrySet() ){try{System.out.println( "==> " + uriFutureEntry.getValue().get() );}catch ( Exception ignored ){}}}// other methods the same as above}

我们将每个作业提交给ExecutorService,并返回一个Future ,并将其存储在地图中,然后再检索其结果。 如果运行该命令,将看到以下输出:

Sun Nov 17 22:49:58 GMT 2013
==> {KernelVersion=Neo4j - Graph Database Kernel 2.0.0-M06, InstanceId=1, LastCommittedTxId=18, Role=master}
==> {KernelVersion=Neo4j - Graph Database Kernel 2.0.0-M06, InstanceId=2, LastCommittedTxId=18, Role=slave}
==> {KernelVersion=Neo4j - Graph Database Kernel 2.0.0-M06, InstanceId=3, LastCommittedTxId=18, Role=slave}
Sun Nov 17 22:49:59 GMT 2013
==> {KernelVersion=Neo4j - Graph Database Kernel 2.0.0-M06, InstanceId=1, LastCommittedTxId=18, Role=master}
==> {KernelVersion=Neo4j - Graph Database Kernel 2.0.0-M06, InstanceId=2, LastCommittedTxId=18, Role=slave}
==> {KernelVersion=Neo4j - Graph Database Kernel 2.0.0-M06, InstanceId=3, LastCommittedTxId=18, Role=slave}
Sun Nov 17 22:50:00 GMT 2013
==> {KernelVersion=Neo4j - Graph Database Kernel 2.0.0-M06, InstanceId=1, LastCommittedTxId=18, Role=master}
==> {KernelVersion=Neo4j - Graph Database Kernel 2.0.0-M06, InstanceId=2, LastCommittedTxId=18, Role=slave}
==> {KernelVersion=Neo4j - Graph Database Kernel 2.0.0-M06, InstanceId=3, LastCommittedTxId=18, Role=slave}...
removed for brevity

总体而言,这种方法效果很好,尽管我总是愿意学习更好的方法!

参考: Java:安排我们的JCG合作伙伴 Mark Needham在Mark Needham Blog博客上按时间间隔运行作业 。

翻译自: https://www.javacodegeeks.com/2013/11/java-schedule-a-job-to-run-on-a-time-interval.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/366170.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

epic怎么添加本地游戏_游戏日报:原神公测无法上架多家安卓渠道;Epic投资布局游戏UGC平台...

// 热点标签/// NOCITCE华为|小米|OPPO|米哈游|腾讯|莉莉丝B站|阅文集团|Epic|万国觉醒|灰烬战线FGO|怪物弹珠|勇者斗恶龙|金币大师阴阳师|和平精英|王者荣耀|三国志战略版1.华为、小米、OPPO等多家安卓渠道的《原神》版主发布公告称&#xff0c;因开发者要求/未与米哈游达成一…

php 出错处理,PHP 错误处理机制

在日常的项目开发过程中&#xff0c;总是会出现一些我们意想不到的异常错误&#xff0c;如果我们对此没有进行相对完善的处理&#xff0c;那么程序看上去也很不专业&#xff0c;也很可能就会成为别人攻击系统的有效信息&#xff1b;有些错误异常会终止脚本执行&#xff0c;这个…

根据location地址,在导航栏高亮显示当前页面

获取当前页面的地址栏。与导航栏中所有<a> 标签的href属性 进行比较。如果相等则高亮显示 此<a>标签。 注意点&#xff1a;a 标签的href 属性在浏览器解析时 是绝对路径。 a 标签的href 如果是锚点&#xff0c;则 pathname &#xff0c; href都与 location的 pathn…

解决关于 ionic3 启动白屏 控制台错误提示:Uncaught SyntaxError Use of const in strict mode.

今天将项目从ionic2 升级为ionic3 &#xff0c;ionic serve 运行在网页上无任何错误。 但是将项目打包成为android apk 却一直卡在启动页面 白屏&#xff0c;进不去的情况。后来在android studio 控制台看到这个错误提示&#xff1a;Uncaught SyntaxError Use of const in str…

参数与超参数

作为机器学习攻城狮&#xff08;咳咳&#xff1a;调参员&#xff09;&#xff0c;参数和超参数是最最基础的常识。 1、参数&#xff08;模型根据数据可以自动学习出的变量&#xff09; 参数指的是模型内部的配置变量(configuration variable)&#xff0c;可通过数据来估计其取值…

使用structure101分析软件包的依赖关系

稳定应用程序的一个关键是结构良好的代码库。 我们知道我们应该建立尽可能多的黑匣子&#xff0c;因为一旦完成一个黑匣子&#xff0c;我们就不必再考虑它的内部了。 您只需要使用您或其他团队成员通过明确定义的界面编写的代码即可。 这使您可以专注于要添加的下一个功能。 当…

excel高级筛选怎么用_神!Excel高级筛选原来如此好用

转自EXCE不加班这两天的宏教程都提到了高级筛选这个功能&#xff0c;不过只是用到最基本的用法。高级筛选其实是一个很好用的功能&#xff0c;今天卢子来全面讲解。1.按关键词筛选高级筛选最重要的就是条件区域&#xff0c;资产名称四门书柜&#xff0c;日期2017/1/1&#xff0…

iis7.5配置php环境,iis7.5安装配置php环境 - iis

前言iis7.5是安装在win7、win8里的web服务器&#xff0c;win2003、win2000的web服务器使用的是iis6.0&#xff0c;由于win7、win8系统相比win2003、win2000有了改新革面的不同&#xff0c;因此对于iis7.5的配置也必需捣鼓一翻才能熟悉。不过...前言iis7.5是安装在win7、win8里的…

微信小程序 网络请求之设置合法域名

设置域名 登录微信公众号后台小程序中 设置→开发设置→服务器设置 必须设置域名&#xff0c;微信小程序才能进行网络通讯&#xff0c;不然会报错 如果设置好了合法域名&#xff0c;开发工具还提示不在合法域名列表中&#xff0c;因为在微信会有一段时间的缓存&#xff0c;等…

money (dp)

牛客网暑假训练第二场D题&#xff1a; 链接&#xff1a;https://www.nowcoder.com/acm/contest/140/D来源&#xff1a;牛客网 题目描述 White Cloud has built n stores numbered from 1 to n. White Rabbit wants to visit these stores in the order from 1 to n. The store …

简而言之Java.io:22个案例研究

这篇文章试图涵盖java.io中的一整套操作。 与与此主题相关的其他书籍和博客相比&#xff0c;我的动机是通过案例研究展示“操作方法”。 作为一名Java的学生&#xff0c;我意识到学习一种新的程序语言的最有效方法是通过示例&#xff1a;复制并粘贴一段代码&#xff0c;运行它以…

php gps 坐标,php 计算gps坐标 距离

在计算机或GPS上经纬度经常用度、分、秒和度.度、分.分、秒.秒的混合方式进行表示&#xff0c;度、分、秒间的进 制是60进制&#xff0c;度.度、分.分、秒.秒的进制是100进制&#xff0c;换算时一定要注意。可以近似地认为每个纬度之间的距离是不变的111KM,每分间 1.85KM&#…

博客园如何使用MarkDown

如何使用博客园下的markdown&#xff1a;https://www.cnblogs.com/ulrica/p/8933549.html 博客园的 MarkDown 代码样式如何设置https://www.cnblogs.com/zhongxia/p/26b4b061f2a47518681bcdd4ff89c344.html 博客园 Markdown 编辑器指南http://www.cnblogs.com/qiaogaojian/p/61…

jQuery -- 光阴似箭(五):AJAX 方法

jQuery -- 知识点回顾篇&#xff08;五&#xff09;&#xff1a;AJAX 方法 1. $.ajax 方法&#xff1a;用于执行 AJAX&#xff08;异步 HTTP&#xff09;请求。 <!DOCTYPE html> <html> <head> <meta http-equiv"Content-Type" content"t…

ggplot2设置坐标轴范围_R语言数据可视化| ggplot2中会“分身术”的facet_wrap()与facet_grid()...

【R语言】高维数据可视化| ggplot2中会“分身术”的facet_wrap()与facet_grid()姐妹花​mp.weixin.qq.comfacet_grid()形成由行和列面化变量定义的面板矩阵。当有两个离散变量&#xff0c;并且这些变量的所有组合存在于数据中时&#xff0c;它是最有用的。如果只有一个具有多个…

使用Google GSON:额外的赠品:第一部分

介绍 这是以前的Google GSON入门的后续文章&#xff0c;其中显示了有关使用Google Gson的入门资料。 本文显示了GSON库的一些其他优点。 由于有很多关于这些额外功能的文章要写&#xff0c;所以我将长篇文章分成2个系列&#xff0c;因此&#xff0c;这是其中一部分&#xff0c…

HDU 2181 哈密顿绕行世界问题 (dfs)

哈密顿绕行世界问题 Crawling in process... Crawling failed Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Submit Status Description 一个规则的实心十二面体&#xff0c;它的 20个顶点标出世界著名的20个城市&#xff0c;你从一个城…

php resque 计划任务,PHP-RESQUE - 实现重试

因为PHP-Resque 的重试部分需要自己写&#xff0c;网上又没啥轮子&#xff0c;而且resque也已经很久不更新了&#xff0c;所以自己研究下resque的源码&#xff0c;然后也借鉴了Laravel的队列重试机制&#xff0c;实现了PHP-Resque的重试机制。Resque地址设计思路1.这里需要阅读…

RabbitMQ集群、镜像部署配置

1 RABBITMQ简介及安装 RabbitMQ是一个开源的AMQP实现&#xff0c;服务器端用Erlang语言编写&#xff0c;支持多种客户端&#xff0c;如&#xff1a;Python、Ruby、.NET、Java、JMS、C、PHP、ActionScript、XMPP、STOMP等&#xff0c;支持AJAX。用于在分布式系统中存储转发消息…

C语言、c++实现超好玩植物大战僵尸(完整版附源码)

实现这个游戏需要Easy_X main.cpp //开发日志 //1导入素材 //2实现最开始的游戏场景 //3实现游戏顶部的工具栏 //4实现工具栏里面的游戏卡牌 #define WIN_WIDTH 900 #define WIN_HEIGHT 600 //定义植物类型 enum { WAN_DOU, XIANG_RI_KUI, ZHI_WU_COUNT }; #include<stdio.…