Create a gist now

Instantly share code, notes, and snippets.

Embed
Swing Actors using Akka
// ©2012 Viktor Klang
package akka.klang
import akka.dispatch.{ DispatcherPrerequisites, ExecutorServiceFactory, ExecutorServiceConfigurator }
import com.typesafe.config.Config
import java.util.concurrent.{ ExecutorService, AbstractExecutorService, ThreadFactory, TimeUnit }
import java.util.Collections
import javax.swing.SwingUtilities
// First we wrap invokeLater as an ExecutorService
object SwingExecutorService extends AbstractExecutorService {
def execute(command: Runnable) = SwingUtilities.invokeLater(command)
def shutdown(): Unit = ()
def shutdownNow() = Collections.emptyList[Runnable]
def isShutdown = false
def isTerminated = false
def awaitTermination(l: Long, timeUnit: TimeUnit) = true
}
// Then we create an ExecutorServiceConfigurator so that Akka can use our SwingExecutorService for the dispatchers
class SwingEventThreadExecutorServiceConfigurator(config: Config, prerequisites: DispatcherPrerequisites) extends ExecutorServiceConfigurator(config, prerequisites) {
private val f = new ExecutorServiceFactory { def createExecutorService: ExecutorService = SwingExecutorService }
def createExecutorServiceFactory(id: String, threadFactory: ThreadFactory): ExecutorServiceFactory = f
}
// Then we simply need to create a dispatcher configuration in our application.conf
swing-dispatcher {
type = "Dispatcher"
executor = "akka.klang.SwingEventThreadExecutorServiceConfigurator"
throughput = 1
}
// After that we just create the GUI Actors with a Props with the correct dispatcher set:
val frameActor = context.actorOf(Props[FrameActor].withDispatcher("swing-dispatcher"), "frame-actor")
// Done! Now all messages processed by "frameActor" will be executed by the Swing Event Dispatch Thread, enjoy!
@mucaho

This comment has been minimized.

Show comment
Hide comment
@mucaho

mucaho Feb 13, 2014

Thank you. I have extended this gist to include JavaFX actors as suggested by this stackoverflow answer. It can be found here.

mucaho commented Feb 13, 2014

Thank you. I have extended this gist to include JavaFX actors as suggested by this stackoverflow answer. It can be found here.

@drozzy

This comment has been minimized.

Show comment
Hide comment
@drozzy

drozzy Apr 15, 2014

Could you explain where the g.draw() calls go? Does the frameActor gets created inside another actor? Thanks.

drozzy commented Apr 15, 2014

Could you explain where the g.draw() calls go? Does the frameActor gets created inside another actor? Thanks.

@viktorklang

This comment has been minimized.

Show comment
Hide comment
@viktorklang

viktorklang May 30, 2014

@drozzy you can call draw inside the FrameActor, and if you want to initiate a repaint from the outside, send the FrameActor a message to do so.

Owner

viktorklang commented May 30, 2014

@drozzy you can call draw inside the FrameActor, and if you want to initiate a repaint from the outside, send the FrameActor a message to do so.

@Maatary

This comment has been minimized.

Show comment
Hide comment
@Maatary

Maatary Aug 10, 2014

Could you explain a bit better what you are doing here? The goal is to have an actor that can execute itself from within the UI Thread, To what be able to manipulate the UI, without using Platform.RunLater?

Maatary commented Aug 10, 2014

Could you explain a bit better what you are doing here? The goal is to have an actor that can execute itself from within the UI Thread, To what be able to manipulate the UI, without using Platform.RunLater?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment