Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor context to support pluggable contexts and use the Netty even…
…t loop.
- Loading branch information
Showing
31 changed files
with
376 additions
and
251 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
118 changes: 118 additions & 0 deletions
118
cluster/src/main/java/net/kuujo/copycat/util/Context.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,118 @@ | |||
/* | |||
* Copyright 2015 the original author or authors. | |||
* | |||
* Licensed under the Apache License, Version 2.0 (the "License"); | |||
* you may not use this file except in compliance with the License. | |||
* You may obtain a copy of the License at | |||
* | |||
* http://www.apache.org/licenses/LICENSE-2.0 | |||
* | |||
* Unless required by applicable law or agreed to in writing, software | |||
* distributed under the License is distributed on an "AS IS" BASIS, | |||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |||
* See the License for the specific language governing permissions and | |||
* limitations under the License. | |||
*/ | |||
package net.kuujo.copycat.util; | |||
|
|||
import net.kuujo.alleycat.Alleycat; | |||
|
|||
import java.util.ServiceLoader; | |||
import java.util.concurrent.Executor; | |||
import java.util.concurrent.ScheduledFuture; | |||
import java.util.concurrent.TimeUnit; | |||
|
|||
/** | |||
* Execution context. | |||
* | |||
* @author <a href="http://github.com/kuujo">Jordan Halterman</a> | |||
*/ | |||
public abstract class Context implements Executor, AutoCloseable { | |||
private static final ContextFactory factory = ServiceLoader.load(ContextFactory.class).iterator().next(); | |||
|
|||
/** | |||
* Creates a new context. | |||
* | |||
* @param name The context name. | |||
* @param serializer The Alleycat serializer. | |||
* @return A new context. | |||
*/ | |||
public static Context createContext(String name, Alleycat serializer) { | |||
return factory.createContext(name, serializer); | |||
} | |||
|
|||
/** | |||
* Returns the current context. | |||
* | |||
* @return The current context. | |||
*/ | |||
public static Context currentContext() { | |||
Thread thread = Thread.currentThread(); | |||
return thread instanceof CopycatThread ? ((CopycatThread) thread).getContext() : null; | |||
} | |||
|
|||
private final String name; | |||
private final Alleycat serializer; | |||
|
|||
protected Context(String name, CopycatThread thread, Alleycat serializer) { | |||
if (thread == null) | |||
throw new NullPointerException("thread cannot be null"); | |||
thread.setName(name); | |||
thread.setContext(this); | |||
this.name = name; | |||
this.serializer = serializer; | |||
} | |||
|
|||
/** | |||
* Checks that the current thread is the correct context thread. | |||
*/ | |||
public void checkThread() { | |||
Thread thread = Thread.currentThread(); | |||
if (!(thread instanceof CopycatThread && ((CopycatThread) thread).getContext() == this)) { | |||
throw new IllegalStateException("not running on the correct thread"); | |||
} | |||
} | |||
|
|||
/** | |||
* Returns the context name. | |||
* | |||
* @return The context name. | |||
*/ | |||
public String name() { | |||
return name; | |||
} | |||
|
|||
/** | |||
* Returns the context serializer. | |||
* | |||
* @return The context serializer. | |||
*/ | |||
public Alleycat serializer() { | |||
return serializer; | |||
} | |||
|
|||
/** | |||
* Schedules a runnable on the context. | |||
* | |||
* @param runnable The runnable to schedule. | |||
* @param delay The delay at which to schedule the runnable. | |||
* @param unit The time unit. | |||
*/ | |||
public abstract ScheduledFuture<?> schedule(Runnable runnable, long delay, TimeUnit unit); | |||
|
|||
/** | |||
* Schedules a runnable at a fixed rate on the context. | |||
* | |||
* @param runnable The runnable to schedule. | |||
* @param delay The delay at which to schedule the runnable. | |||
* @param unit The time unit. | |||
*/ | |||
public abstract ScheduledFuture<?> scheduleAtFixedRate(Runnable runnable, long delay, long rate, TimeUnit unit); | |||
|
|||
/** | |||
* Closes the context. | |||
*/ | |||
@Override | |||
public abstract void close(); | |||
|
|||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
128 changes: 0 additions & 128 deletions
128
cluster/src/main/java/net/kuujo/copycat/util/ExecutionContext.java
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.