Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move local member task queue/connection to separate classes.
- Loading branch information
Showing
5 changed files
with
157 additions
and
91 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
87 changes: 87 additions & 0 deletions
87
coordination/src/main/java/io/atomix/coordination/LocalGroupConnection.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 | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
/* | ||
* Copyright 2016 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 io.atomix.coordination; | ||
|
||
import io.atomix.catalyst.transport.Address; | ||
import io.atomix.catalyst.util.Listener; | ||
|
||
import java.util.Map; | ||
import java.util.concurrent.ConcurrentHashMap; | ||
import java.util.function.Consumer; | ||
|
||
/** | ||
* Local group connection. | ||
* | ||
* @author <a href="http://github.com/kuujo>Jordan Halterman</a> | ||
*/ | ||
public class LocalGroupConnection extends GroupConnection { | ||
private final Map<String, MessageListenerHolder> messageListeners = new ConcurrentHashMap<>(); | ||
|
||
public LocalGroupConnection(String memberId, Address address, GroupConnectionManager connections) { | ||
super(memberId, address, connections); | ||
} | ||
|
||
/** | ||
* Registers a consumer for messages sent to the local member. | ||
* <p> | ||
* The provided message consumer will be called when a message sent to the local member | ||
* is received for the given {@code topic}. | ||
* | ||
* @param topic The message topic. | ||
* @param consumer The message consumer. | ||
* @param <T> The message type. | ||
* @return The message listener. | ||
*/ | ||
@SuppressWarnings("unchecked") | ||
public <T> Listener<GroupMessage<T>> onMessage(String topic, Consumer<GroupMessage<T>> consumer) { | ||
MessageListenerHolder listener = new MessageListenerHolder(consumer); | ||
messageListeners.put(topic, listener); | ||
return listener; | ||
} | ||
|
||
/** | ||
* Handles a message to the member. | ||
*/ | ||
void handleMessage(GroupMessage message) { | ||
MessageListenerHolder listener = messageListeners.get(message.topic()); | ||
if (listener != null) { | ||
listener.accept(message); | ||
} | ||
} | ||
|
||
/** | ||
* Listener holder. | ||
*/ | ||
@SuppressWarnings("unchecked") | ||
private class MessageListenerHolder implements Listener { | ||
private final Consumer consumer; | ||
|
||
private MessageListenerHolder(Consumer consumer) { | ||
this.consumer = consumer; | ||
} | ||
|
||
@Override | ||
public void accept(Object message) { | ||
consumer.accept(message); | ||
} | ||
|
||
@Override | ||
public void close() { | ||
messageListeners.remove(this); | ||
} | ||
} | ||
|
||
} |
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
55 changes: 55 additions & 0 deletions
55
coordination/src/main/java/io/atomix/coordination/LocalGroupTaskQueue.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 | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
/* | ||
* Copyright 2016 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 io.atomix.coordination; | ||
|
||
import io.atomix.catalyst.util.Listener; | ||
import io.atomix.catalyst.util.Listeners; | ||
|
||
import java.util.function.Consumer; | ||
|
||
/** | ||
* Local group task queue. | ||
* | ||
* @author <a href="http://github.com/kuujo>Jordan Halterman</a> | ||
*/ | ||
public class LocalGroupTaskQueue extends GroupTaskQueue { | ||
private final Listeners<GroupTask<Object>> taskListeners = new Listeners<>(); | ||
|
||
public LocalGroupTaskQueue(String memberId, DistributedGroup group) { | ||
super(memberId, group); | ||
} | ||
|
||
/** | ||
* Registers a consumer for tasks send to the local member. | ||
* | ||
* @param consumer The task consumer. | ||
* @param <T> The task type. | ||
* @return The task listener. | ||
*/ | ||
@SuppressWarnings("unchecked") | ||
public <T> Listener<GroupTask<T>> onTask(Consumer<GroupTask<T>> consumer) { | ||
return (Listener) taskListeners.add((Consumer) consumer); | ||
} | ||
|
||
/** | ||
* Handles a task. | ||
*/ | ||
@SuppressWarnings("unchecked") | ||
void handleTask(GroupTask task) { | ||
taskListeners.accept(task); | ||
} | ||
|
||
} |
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