日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > java >内容正文

java

java运行时间间隔_Java:安排作业按时间间隔运行

發布時間:2023/12/3 java 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java运行时间间隔_Java:安排作业按时间间隔运行 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

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

java運行時間間隔

總結

以上是生活随笔為你收集整理的java运行时间间隔_Java:安排作业按时间间隔运行的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。