Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* 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 io.atomix.resource;
import io.atomix.catalyst.util.Assert;
import io.atomix.copycat.server.Commit;
import io.atomix.copycat.server.StateMachine;
import io.atomix.copycat.server.StateMachineExecutor;
import io.atomix.copycat.server.session.ServerSession;
import io.atomix.copycat.server.session.SessionListener;
import io.atomix.resource.internal.ResourceCommand;
import io.atomix.resource.internal.ResourceEvent;
import io.atomix.resource.internal.ResourceQuery;
import java.util.*;
/**
* Base class for resource state machines.
*
* Resource implementations should extend the resource state machine for support in Atomix core.
* This state machine implements functionality necessary to multiplexing the state machine on a
* single Copycat log and provides facilities for configuring and cleaning up the resource upon
* deletion.
*
* To implement a resource state machine, simply extend this class. State machines must adhere
* to the same rules as those outlined in the Copycat documentation. See the {@link StateMachine}
* documentation for more information.
*
* {@code
* public class MapStateMachine extends ResourceStateMachine {
* private final Map
* Resource state machines should override the {@link #delete()} method to perform cleanup of
* the resource state upon deletion of the resource. Cleanup tasks might include notifying clients
* of the deletion of the resource or releasing {@link Commit commits} held by the state machine.
*
* {@code
* public class MapStateMachine extends ResourceStateMachine {
* private final Map
* Resource state machines implement Copycat's {@link SessionListener} interface to allow state
* machines to listen for changes in client sessions. When used in an Atomix cluster, sessions
* are specific to a state machine and not the entire cluster. When a client or replica opens
* a resource, a new logical session to that resource is opened and the session listener's
* {@link SessionListener#register(ServerSession)} method will be called. When a client or replica
* closes a resource, the {@link SessionListener#close(ServerSession)} method will be called.
* In other words, sessions in resource state machines represent a client's open resource instance,
* and event messages {@link ServerSession#publish(String, Object) published} to that session will
* be received by the specific client side {@link Resource} instance.
*
* {@code
* public class MapStateMachine extends ResourceStateMachine {
* private final Map map = new HashMap<>();
* private final Set listeners = new HashSet<>();
*
* public void listen(Commit commit) {
* listeners.add(commit.session());
* }
*
* public void put(Commit commit) {
* map.put(commit.operation().key(), commit.operation().value());
* for (ServerSession listener : listeners) {
* listener.publish(commit.operation().key(), commit.operation().value());
* }
* }
*
* public void close(ServerSession session) {
* listeners.remove(session);
* }
* }
* }
*