Homework for LDN event Workshop: Clojure.
Follow Immutant Tutorials and install Leiningen and Immutant.
- Create web handler for /?counter=some-name
- Create queue to enqueue some-name
- Create cache
- Read from queue and increment cache(some-name)
- Create job to print stats table every 5 sec.
$ curl localhost:8080/?counter=Gopher
Recorded 'Gopher' into the stats table
$ curl localhost:8080/?counter=Gopher
Recorded 'Gopher' into the stats table
$ curl localhost:8080/?counter=Gopher
Recorded 'Gopher' into the stats table
$ curl localhost:8080/?counter=Web
Recorded 'Web' into the stats table
$ curl localhost:8080/?counter=Web
Recorded 'Web' into the stats table
$ curl localhost:8080/?counter=Web
Recorded 'Web' into the stats table
$ curl localhost:8080/?counter=Web
Recorded 'Web' into the stats table
$ curl localhost:8080/?counter=Web
Recorded 'Web' into the stats table
$
21:21:43,250 INFO [stdout] (Thread-81 (HornetQ-client-global-threads-14918829)) Gopher 0 -> 1
21:21:43,704 INFO [stdout] (Thread-79 (HornetQ-client-global-threads-14918829)) Gopher 1 -> 2
21:21:44,404 INFO [stdout] (Thread-79 (HornetQ-client-global-threads-14918829)) Gopher 2 -> 3
21:21:45,563 INFO [stdout] (JobScheduler$cloji.clj_Worker-2) === Stats ===
21:21:45,563 INFO [stdout] (JobScheduler$cloji.clj_Worker-2) Gopher - 3
21:21:46,532 INFO [stdout] (Thread-79 (HornetQ-client-global-threads-14918829)) Web 0 -> 1
21:21:46,918 INFO [stdout] (Thread-81 (HornetQ-client-global-threads-14918829)) Web 1 -> 2
21:21:47,811 INFO [stdout] (Thread-81 (HornetQ-client-global-threads-14918829)) Web 2 -> 3
21:21:48,183 INFO [stdout] (Thread-81 (HornetQ-client-global-threads-14918829)) Web 3 -> 4
21:21:48,627 INFO [stdout] (Thread-80 (HornetQ-client-global-threads-14918829)) Web 4 -> 5
21:21:50,562 INFO [stdout] (JobScheduler$cloji.clj_Worker-3) === Stats ===
21:21:50,563 INFO [stdout] (JobScheduler$cloji.clj_Worker-3) Gopher - 3
21:21:50,563 INFO [stdout] (JobScheduler$cloji.clj_Worker-3) Web - 5
Full solution is 33 non-blank code lines, see below.
Thats includes enterprise grade clustering, messaging, caching, scheduling, race-free counters update.
21:21:16,651 INFO [org.jboss.as.server.deployment] (MSC service thread 1-1) JBAS015876: Starting deployment of "cloji.clj" (runtime-name: "cloji.clj")
21:21:19,053 INFO [org.jboss.web] (ServerService Thread Pool -- 68) JBAS018210: Register web context:
21:21:23,082 INFO [immutant.cache.core] (pool-8-thread-8) Configuring cache cache0
21:21:23,090 INFO [org.jboss.as.clustering.infinispan] (pool-8-thread-8) JBAS010282: Stopped cache0 cache from polyglot container
21:21:23,094 INFO [org.infinispan.jmx.CacheJmxRegistration] (pool-8-thread-8) ISPN000031: MBeans were successfully registered to the platform MBean server.
21:21:23,094 INFO [org.jboss.as.clustering.infinispan] (pool-8-thread-8) JBAS010281: Started cache0 cache from polyglot container
21:21:25,251 INFO [immutant.web] (pool-8-thread-8) Registering ring handler at sub-context path: /
21:21:25,462 INFO [org.hornetq.core.server] (pool-15-thread-1) HQ221005: trying to deploy queue jms.queue.queue0
21:21:25,466 INFO [org.jboss.as.messaging] (pool-15-thread-1) JBAS011601: Bound messaging object to jndi name java:/queue0
21:21:25,523 INFO [immutant.jobs] (pool-8-thread-8) Scheduling job job0 with {:every 5000}
21:21:25,527 INFO [org.quartz.impl.StdSchedulerFactory] (MSC service thread 1-3) Using default implementation for ThreadExecutor
21:21:25,529 INFO [immutant.runtime] (pool-8-thread-8) Initialized cloji from immutant.init
21:21:25,560 INFO [org.quartz.core.SchedulerSignalerImpl] (MSC service thread 1-3) Initialized Scheduler Signaller of type: class org.quartz.core.SchedulerSignalerImpl
21:21:25,561 INFO [org.quartz.core.QuartzScheduler] (MSC service thread 1-3) Quartz Scheduler v.2.1.5 created.
21:21:25,561 INFO [org.quartz.simpl.RAMJobStore] (MSC service thread 1-3) RAMJobStore initialized.
21:21:25,561 INFO [org.quartz.core.QuartzScheduler] (MSC service thread 1-3) Scheduler meta-data: Quartz Scheduler (v2.1.5) 'JobScheduler$cloji.clj' with instanceId 'NON_CLUSTERED'
Scheduler class: 'org.quartz.core.QuartzScheduler' - running locally.
NOT STARTED.
Currently in standby mode.
Number of jobs executed: 0
Using thread pool 'org.quartz.simpl.SimpleThreadPool' - with 3 threads.
Using job-store 'org.quartz.simpl.RAMJobStore' - which does not support persistence. and is not clustered.
21:21:25,561 INFO [org.quartz.impl.StdSchedulerFactory] (MSC service thread 1-3) Quartz scheduler 'JobScheduler$cloji.clj' initialized from an externally provided properties instance.
21:21:25,561 INFO [org.quartz.impl.StdSchedulerFactory] (MSC service thread 1-3) Quartz scheduler version: 2.1.5
21:21:25,561 INFO [org.quartz.core.QuartzScheduler] (MSC service thread 1-3) JobFactory set to: org.immutant.jobs.JobScheduler@6b55ca
21:21:25,562 INFO [org.quartz.core.QuartzScheduler] (MSC service thread 1-3) Scheduler JobScheduler$cloji.clj_$_NON_CLUSTERED started.
21:21:25,581 INFO [org.jboss.as.server] (DeploymentScanner-threads - 2) JBAS018559: Deployed "cloji.clj" (runtime-name : "cloji.clj")
21:21:43,250 INFO [stdout] (Thread-81 (HornetQ-client-global-threads-14918829)) Gopher 0 -> 1
21:21:43,704 INFO [stdout] (Thread-79 (HornetQ-client-global-threads-14918829)) Gopher 1 -> 2
21:21:44,404 INFO [stdout] (Thread-79 (HornetQ-client-global-threads-14918829)) Gopher 2 -> 3
21:21:45,563 INFO [stdout] (JobScheduler$cloji.clj_Worker-2) === Stats ===
21:21:45,563 INFO [stdout] (JobScheduler$cloji.clj_Worker-2) Gopher - 3
21:21:46,532 INFO [stdout] (Thread-79 (HornetQ-client-global-threads-14918829)) Web 0 -> 1
21:21:46,918 INFO [stdout] (Thread-81 (HornetQ-client-global-threads-14918829)) Web 1 -> 2
21:21:47,811 INFO [stdout] (Thread-81 (HornetQ-client-global-threads-14918829)) Web 2 -> 3
21:21:48,183 INFO [stdout] (Thread-81 (HornetQ-client-global-threads-14918829)) Web 3 -> 4
21:21:48,627 INFO [stdout] (Thread-80 (HornetQ-client-global-threads-14918829)) Web 4 -> 5
21:21:50,562 INFO [stdout] (JobScheduler$cloji.clj_Worker-3) === Stats ===
21:21:50,563 INFO [stdout] (JobScheduler$cloji.clj_Worker-3) Gopher - 3
21:21:50,563 INFO [stdout] (JobScheduler$cloji.clj_Worker-3) Web - 5