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 (c) 2008-2015, Hazelcast, Inc. All Rights Reserved.
*
* 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 com.hazelcast.client.spi.impl;
import com.hazelcast.client.HazelcastClientNotActiveException;
import com.hazelcast.client.connection.ClientConnectionManager;
import com.hazelcast.client.connection.nio.ClientConnection;
import com.hazelcast.client.impl.HazelcastClientInstanceImpl;
import com.hazelcast.client.impl.protocol.ClientMessage;
import com.hazelcast.client.impl.protocol.codec.ClientRemoveAllListenersCodec;
import com.hazelcast.client.impl.protocol.parameters.ExceptionResultParameters;
import com.hazelcast.client.spi.ClientExecutionService;
import com.hazelcast.client.spi.ClientInvocationService;
import com.hazelcast.client.spi.ClientPartitionService;
import com.hazelcast.client.spi.EventHandler;
import com.hazelcast.logging.ILogger;
import com.hazelcast.logging.Logger;
import com.hazelcast.nio.Address;
import com.hazelcast.nio.Connection;
import com.hazelcast.nio.ConnectionListener;
import com.hazelcast.spi.exception.TargetDisconnectedException;
import com.hazelcast.util.ConstructorFunction;
import java.io.IOException;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.Iterator;
import java.util.Map;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.RejectedExecutionException;
import java.util.concurrent.atomic.AtomicInteger;
import static com.hazelcast.instance.OutOfMemoryErrorDispatcher.onOutOfMemory;
abstract class ClientInvocationServiceSupport implements ClientInvocationService,
ConnectionHeartbeatListener, ConnectionListener {
private static final int WAIT_TIME_FOR_PACKETS_TO_BE_CONSUMED = 10;
private static final int WAIT_TIME_FOR_PACKETS_TO_BE_CONSUMED_THRESHOLD = 5000;
protected final HazelcastClientInstanceImpl client;
protected final ClientConnectionManager connectionManager;
protected final ClientPartitionService partitionService;
protected final ClientExecutionService executionService;
private final ILogger logger = Logger.getLogger(ClientInvocationService.class);
private final ResponseThread responseThread;
private final ConcurrentMap callIdMap
= new ConcurrentHashMap();
private final ConcurrentMap eventHandlerMap
= new ConcurrentHashMap();
private final AtomicInteger callIdIncrementer = new AtomicInteger();
private volatile boolean isShutdown;
public ClientInvocationServiceSupport(HazelcastClientInstanceImpl client) {
this.client = client;
this.connectionManager = client.getConnectionManager();
this.executionService = client.getClientExecutionService();
connectionManager.addConnectionListener(this);
connectionManager.addConnectionHeartbeatListener(this);
this.partitionService = client.getClientPartitionService();
responseThread = new ResponseThread(client.getThreadGroup(), client.getName() + ".response-",
client.getClientConfig().getClassLoader());
responseThread.start();
}
@Override
public boolean isRedoOperation() {
return client.getClientConfig().getNetworkConfig().isRedoOperation();
}
protected void send(ClientInvocation invocation, ClientConnection connection) throws IOException {
if (isShutdown) {
throw new HazelcastClientNotActiveException("Client is shut down");
}
registerInvocation(invocation);
ClientMessage clientMessage = invocation.getClientMessage();
if (!isAllowedToSendRequest(connection, invocation) || !writeToConnection(connection, clientMessage)) {
final int callId = clientMessage.getCorrelationId();
deRegisterCallId(callId);
deRegisterEventHandler(callId);
throw new IOException("Packet not send to " + connection.getRemoteEndpoint());
}
invocation.setSendConnection(connection);
}
private boolean writeToConnection(ClientConnection connection, ClientMessage clientMessage) {
clientMessage.addFlag(ClientMessage.BEGIN_AND_END_FLAGS);
return connection.write(clientMessage);
}
private boolean isAllowedToSendRequest(ClientConnection connection, ClientInvocation invocation) {
if (!connection.isHeartBeating()) {
if (invocation.shouldBypassHeartbeatCheck()) {
//ping and removeAllListeners should be send even though heart is not beating
return true;
}
if (logger.isFinestEnabled()) {
logger.warning("Connection is not heart-beating, won't write client message -> "
+ invocation.getClientMessage());
}
return false;
}
return true;
}
private void registerInvocation(ClientInvocation clientInvocation) {
short protocolVersion = client.getProtocolVersion();
final int correlationId = newCorrelationId();
clientInvocation.getClientMessage().setCorrelationId(correlationId).setVersion(protocolVersion);
callIdMap.put(correlationId, clientInvocation);
if (clientInvocation instanceof ClientListenerInvocation) {
eventHandlerMap.put(correlationId, (ClientListenerInvocation) clientInvocation);
}
}
private ClientInvocation deRegisterCallId(int callId) {
return callIdMap.remove(callId);
}
private ClientInvocation deRegisterEventHandler(int callId) {
return eventHandlerMap.remove(callId);
}
@Override
public EventHandler getEventHandler(int callId) {
final ClientListenerInvocation clientInvocation = eventHandlerMap.get(callId);
if (clientInvocation == null) {
return null;
}
return clientInvocation.getHandler();
}
@Override
public boolean removeEventHandler(Integer callId) {
if (callId != null) {
return eventHandlerMap.remove(callId) != null;
}
return false;
}
public void cleanResources(ConstructorFunction