-
1. Re: JGroups delay
pferraro Jan 9, 2015 11:37 AM (in response to yfeng1998)While that article does a good job explaining the purpose of the CommandDispatcher abstraction, it does a poor job demonstrating how to use it.
The key issue the author fails to note is that Commands can only be sent, but more importantly, *only be received*, for the lifetime of the CommandDispatcher.
Consequently, this code:
public void execute() { try (CommandDispatcher<String> dispatcher = this.factory.createCommandDispatcher("id1", "Garbage collection in progress....")) { dispatcher.executeOnCluster(new GarbageCollectorCommand()); } }
will rarely execute on a remote node, since that node will only execute the command if its corresponding CommandDispatcher (i.e. with the same cluster and identifier) is active.
In summary, the CommandDispatcher is not a temporary object, but must remain active for as long as you wish it to execute commands.
To facilitate this, a CommandDispatcher is typically created and used from a @Startup @Singleton, e.g.
@Startup @Singleton public class MySingleton { @Resource("java:jboss/clustering/dispatcher/default") private CommandDispatcherFactory factory; private CommandDispatcher<MyCommandContext> dispatcher; private MyCommandContext context = ...; @PostConstruct public void init() { this.dispatcher = this.factory.createCommandDispatcher("foo", this.context); } @PreDestroy public void destroy() { this.dispatcher.close(); } // ... }
There's a better example of CommandDispatcher usage in the clustering testsuite:
-
2. Re: JGroups delay
yfeng1998 Jan 9, 2015 2:07 PM (in response to pferraro)Thanks Paul! It is of great help and I will look into your example.