This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2021-10-31
Channels
- # aleph (8)
- # announcements (11)
- # aws (1)
- # babashka (7)
- # beginners (104)
- # calva (52)
- # clara (1)
- # clj-kondo (28)
- # cljdoc (8)
- # cljsrn (2)
- # clojure (20)
- # clojure-europe (8)
- # clojure-uk (1)
- # clojurescript (26)
- # core-typed (3)
- # datomic (6)
- # holy-lambda (1)
- # jobs (1)
- # jobs-discuss (14)
- # malli (7)
- # pathom (31)
- # polylith (19)
- # re-frame (8)
- # reitit (1)
- # releases (1)
- # shadow-cljs (5)
- # tools-build (92)
Is there a way to wrap a Java object and override a single (public) method, without reimplementing every single method in the object's class?
cool, thanks
A slightly more tedious way is that you can re-implement all methods in your proxy and just use proxy-super
to call the parent implementation
There was a discussion about it lately: https://clojurians.slack.com/archives/C03S1KBA2/p1633952449070600
I'm not sure if this belongs in #beginners or here, but I'm guessing here. What is the clojure way of handling multiple "jobs"? For example, I have an application which has a few jobs/threads which do not rely on each other, but might need to send some data to other jobs/threads.
If its the most basic case - you want to fire off a job and let it run to completion and there aren’t that many of them - just submit the job onto a thread pool
(.submit (ForkJoinPool/commonPool)
(reify Runnable
(run [_]
(println "Doing thing"))))
and if you need to communicate data to annother process, have those processes share a reference to a queue
ArrayBlockingQueue is good. chan
from core.async can be a bit more powerful and (i think) lightweight. There are other options too
(import java.util.concurrent.ForkJoinPool)
=> java.util.concurrent.ForkJoinPool
(require '[clojure.core.async :as async])
=> nil
(let [shared-chan (async/chan)]
(.submit (ForkJoinPool/commonPool)
(reify Runnable
(run [_]
(println "Worker 1: Waiting for one second")
(Thread/sleep 1000)
(println "Worker 1: Giving data to the other worker")
(async/>!! shared-chan {:data :stuff})
(Thread/sleep 2000)
(println "Worker 1: Doing more work"))))
(.submit (ForkJoinPool/commonPool)
(reify Runnable
(run [_]
(println "Worker 2: Waiting for data")
(println "Worker 2: Got Data: " (async/<!! shared-chan))))))
(require '[promesa.core :as promesa])
(let [shared-promise (promesa/deferred)]
(.submit (ForkJoinPool/commonPool)
(reify Runnable
(run [_]
(println "Worker 1: Waiting for one second")
(Thread/sleep 1000)
(println "Worker 1: Giving data to the other worker")
(promesa/resolve! shared-promise {:data :stuff})
(Thread/sleep 2000)
(println "Worker 1: Doing more work"))))
(.submit (ForkJoinPool/commonPool)
(reify Runnable
(run [_]
(println "Worker 2: Waiting for data")
(println "Worker 2: Got Data: " @shared-promise)))))
For communication between processes, you can also use a sort of internal pub/sub mechanism. This will prevent processes needing to know about and directly reference others
@nielsdewaal sounds like that might be a good use case for core.async
I’d disagree - or at the very least i’d specify which parts of core.async - go blocks aren’t really useful for something like this, channels would be