Skip to content

Commit

Permalink
Use WildFly's built-in singleton support.
Browse files Browse the repository at this point in the history
This greatly simplifies our own singleton implementation, and makes it
more reliable, in theory.
  • Loading branch information
tobias committed Oct 9, 2015
1 parent 6dbd527 commit 47b0274
Show file tree
Hide file tree
Showing 19 changed files with 304 additions and 460 deletions.
Expand Up @@ -23,17 +23,12 @@ public boolean isMaster() {
}

@Override
public void setClusterChangeCallback(ClusterChangeCallback callback) {
public void whenMasterAcquired(Runnable r) {

}

@Override
public void connect() {

}

@Override
public void disconnect() {
public void whenMasterLost(Runnable r) {

}

Expand Down
Expand Up @@ -17,13 +17,10 @@
package org.projectodd.wunderboss.ec;

public interface ClusterParticipant {

boolean isMaster();

void setClusterChangeCallback(ClusterChangeCallback callback);

void connect();
void whenMasterAcquired(Runnable r);

void disconnect();
void whenMasterLost(Runnable r);
}

Expand Up @@ -41,7 +41,7 @@ public void setAction(final Runnable action) {
@Override
public void run() {
if (!isRunning) {
thread = new Thread(wrappedAction(action), "daemon-thread[" + name + "]");
thread = new Thread(wrappedAction(action), "wunderboss-daemon-thread[" + name + "]");
thread.start();
isRunning = true;
}
Expand All @@ -64,15 +64,14 @@ public void run() {

if (error != null) {
isStarted = false;
clusterParticipant.disconnect();
errorCallback.notify(name, error);
}
}
};
}

/**
* This runnable will be called when the daemon context stops, but only if the daemon is actually running.
* This runnable will be called when the daemon context stops, but only if the daemon has ran.
*/
@Override
public void setStopCallback(StopCallback c) {
Expand All @@ -94,8 +93,7 @@ public boolean isStarted() {

@Override
public void start() {
if (!isStarted()) {
this.clusterParticipant.connect();
if (!isRunning()) {
this.isStarted = true;
run();
}
Expand All @@ -118,7 +116,6 @@ public synchronized void stop() throws TimeoutException, InterruptedException {
}
}

this.clusterParticipant.disconnect();
this.isStarted = false;
}
}
Expand Down
Expand Up @@ -16,7 +16,7 @@

package org.projectodd.wunderboss.ec;

public abstract class ConcreteExecutionContext implements ExecutionContext, ClusterChangeCallback {
public abstract class ConcreteExecutionContext implements ExecutionContext {

public ConcreteExecutionContext(final String name,
final ClusterParticipant clusterParticipant,
Expand All @@ -25,7 +25,25 @@ public ConcreteExecutionContext(final String name,
this.clusterParticipant = clusterParticipant;
this.singleton = singleton;

clusterParticipant.setClusterChangeCallback(this);
clusterParticipant.whenMasterAcquired(new Runnable() {
@Override
public void run() {
ConcreteExecutionContext.this.start();
}
});

clusterParticipant.whenMasterLost(new Runnable() {
@Override
public void run() {
try {
ConcreteExecutionContext.this.stop();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
});


}

@Override
Expand All @@ -41,15 +59,6 @@ public void run() {
}
}

@Override
public void clusterChanged(boolean wasMaster, boolean isMaster) {
if (this.singleton &&
!wasMaster &&
isMaster) {
run();
}
}

@Override
public void start() {
this.isRunning = true;
Expand Down
Expand Up @@ -23,9 +23,11 @@ public class DaemonContextProvider extends ExecutionContextProvider implements C

@Override
public DaemonContext create(final String name, final Options options) {
final boolean singleton = options.getBoolean(DaemonContext.CreateOption.SINGLETON);

return new ConcreteDaemonContext(name,
clusterParticipant(name),
options.getBoolean(DaemonContext.CreateOption.SINGLETON),
clusterParticipant(name, singleton),
singleton,
options.getLong(DaemonContext.CreateOption.STOP_TIMEOUT));
}
}
Expand Up @@ -17,7 +17,7 @@
package org.projectodd.wunderboss.ec;

public abstract class ExecutionContextProvider {
protected ClusterParticipant clusterParticipant(final String name) {
protected ClusterParticipant clusterParticipant(final String name, final boolean singleton) {
return AlwaysMasterClusterParticipant.INSTANCE;
}
}
Expand Up @@ -20,11 +20,8 @@ public class ImmediateContext extends ConcreteExecutionContext {

public ImmediateContext(String name,ClusterParticipant participant, boolean singleton) {
super(name, participant, singleton);
// we don't care about cluster changes, since we only want to check
// when run is invoked directly
this.clusterParticipant.whenMasterAcquired(null);
}

@Override
public void clusterChanged(boolean wasMaster, boolean isMaster) {
//don't care
}

}
Expand Up @@ -22,8 +22,10 @@
public class ImmediateContextProvider extends ExecutionContextProvider implements ComponentProvider<ImmediateContext> {
@Override
public ImmediateContext create(String name, Options options) {
final boolean singleton = options.getBoolean(DaemonContext.CreateOption.SINGLETON);

return new ImmediateContext(name,
clusterParticipant(name),
options.getBoolean(ExecutionContext.CreateOption.SINGLETON));
clusterParticipant(name, singleton),
singleton);
}
}
45 changes: 21 additions & 24 deletions modules/wildfly/pom.xml
Expand Up @@ -24,24 +24,35 @@

<dependencies>
<dependency>
<groupId>org.projectodd.wunderboss</groupId>
<artifactId>wunderboss-core</artifactId>
<version>${project.version}</version>
<groupId>org.jboss.msc</groupId>
<artifactId>jboss-msc</artifactId>
<version>${version.jboss-msc}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.jgroups</groupId>
<artifactId>jgroups</artifactId>
<version>${version.jgroups}</version>
<groupId>org.jboss.modules</groupId>
<artifactId>jboss-modules</artifactId>
<version>${version.jboss.modules}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.jboss.msc</groupId>
<artifactId>jboss-msc</artifactId>
<version>${version.jboss-msc}</version>
<groupId>org.wildfly</groupId>
<artifactId>wildfly-messaging</artifactId>
<version>${version.wildfly}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.wildfly</groupId>
<artifactId>wildfly-clustering-singleton</artifactId>
<version>${version.wildfly}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.projectodd.wunderboss</groupId>
<artifactId>wunderboss-core</artifactId>
<version>${project.version}</version>
<scope>provided</scope>
</dependency>

<dependency>
<groupId>org.projectodd.wunderboss</groupId>
<artifactId>wunderboss-messaging-hornetq</artifactId>
Expand Down Expand Up @@ -92,20 +103,6 @@
<version>${project.version}</version>
<scope>provided</scope>
</dependency>

<dependency>
<groupId>org.jboss.modules</groupId>
<artifactId>jboss-modules</artifactId>
<version>${version.jboss.modules}</version>
<scope>provided</scope>
</dependency>

<dependency>
<groupId>org.wildfly</groupId>
<artifactId>wildfly-messaging</artifactId>
<version>${version.wildfly}</version>
<scope>provided</scope>
</dependency>
</dependencies>

</project>

This file was deleted.

0 comments on commit 47b0274

Please sign in to comment.