Java后臺作業執行組件:jesque

jopen 10年前發布 | 38K 次閱讀 jesque Java開發

jesque 是 Resque 的Java語言實現版。

它完全可與Ruby和Node.js的(Coffee-Resque))實現進行互操作。
resque是基于redis的后臺任務組件,能把任何類或模塊作為任務在后臺執行,且自帶前臺管理功能,方便查看執行情況。

Jesque 是一個 Maven 項目,依賴于 Jedis 來連接到 RedisJackson 來操作 JSON 和使用 SLF4J 記錄日志。
// Configuration
final Config config = new ConfigBuilder().build();

// Add a job to the queue
final Job job = new Job("TestAction", 
    new Object[]{ 1, 2.3, true, "test", Arrays.asList("inner", 4.5)});
final Client client = new ClientImpl(config);
client.enqueue("foo", job);
client.end();

// Add a job to the delayed queue
final Job job = new Job("TestAction", 
    new Object[]{ 1, 2.3, true, "test", Arrays.asList("inner", 4.5)});

final long delay = 10; // in seconds
final long future = System.currentTimeMillis() + (delay * 1000); // timestamp

final Client client = new ClientImpl(config);
client.delayedEnqueue("fooDelay", job, future);
client.end();

// Start a worker to run jobs from the queue
final Worker worker = new WorkerImpl(config, 
    Arrays.asList("foo"), new MapBasedJobFactory(map(entry("TestAction", TestAction.class))));
final Thread workerThread = new Thread(worker);
workerThread.start();

// Wait a few secs then shutdown
try { Thread.sleep((delay * 1000) + 5000); } catch (Exception e){} // Give ourselves time to process
worker.end(true);
try { workerThread.join(); } catch (Exception e){ e.printStackTrace(); }

項目主頁:http://www.baiduhome.net/lib/view/home/1417077003659

 本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!