PageRenderTime 21ms CodeModel.GetById 14ms app.highlight 4ms RepoModel.GetById 2ms app.codeStats 0ms

/hazelcast-client/src/main/java/com/hazelcast/client/InRunnable.java

https://bitbucket.org/gabral6_gmailcom/hazelcast
Java | 105 lines | 80 code | 9 blank | 16 comment | 17 complexity | 3d221a1658811345b5b6fd65065adbe7 MD5 | raw file
  1/*
  2 * Copyright (c) 2008-2013, Hazelcast, Inc. All Rights Reserved.
  3 *
  4 * Licensed under the Apache License, Version 2.0 (the "License");
  5 * you may not use this file except in compliance with the License.
  6 * You may obtain a copy of the License at
  7 *
  8 * http://www.apache.org/licenses/LICENSE-2.0
  9 *
 10 * Unless required by applicable law or agreed to in writing, software
 11 * distributed under the License is distributed on an "AS IS" BASIS,
 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 13 * See the License for the specific language governing permissions and
 14 * limitations under the License.
 15 */
 16
 17package com.hazelcast.client;
 18
 19import com.hazelcast.impl.ClusterOperation;
 20import com.hazelcast.util.Clock;
 21
 22import java.io.IOException;
 23import java.util.Map;
 24import java.util.logging.Level;
 25
 26public class InRunnable extends IORunnable implements Runnable {
 27    final PacketReader reader;
 28    Connection connection = null;
 29    private final OutRunnable outRunnable;
 30
 31    volatile long lastReceived;
 32
 33    public InRunnable(HazelcastClient client, OutRunnable outRunnable, Map<Long, Call> calls, PacketReader reader) {
 34        super(client, calls);
 35        this.outRunnable = outRunnable;
 36        this.reader = reader;
 37    }
 38
 39    protected void customRun() throws InterruptedException {
 40        if (outRunnable.reconnection.get()) {
 41            Thread.sleep(10L);
 42            return;
 43        }
 44        Packet packet;
 45        try {
 46            Connection oldConnection = connection;
 47            connection = client.connectionManager.getConnection();
 48            if (restoredConnection(oldConnection, connection)) {
 49                if (outRunnable.sendReconnectCall(connection)) {
 50                    logger.log(Level.FINEST, "restoredConnection");
 51                    if (oldConnection != null) {
 52                        redoUnfinishedCalls(oldConnection);
 53                    }
 54                }
 55                return;
 56            }
 57            if (connection == null) {
 58                outRunnable.clusterIsDown(oldConnection);
 59                Thread.sleep(10);
 60            } else {
 61                packet = reader.readPacket(connection);
 62//                logger.log(Level.FINEST, "Reading " + packet.getOperation() + " Call id: " + packet.getCallId());
 63                this.lastReceived = Clock.currentTimeMillis();
 64                Call call = callMap.remove(packet.getCallId());
 65                if (call != null) {
 66                    call.received = System.nanoTime();
 67                    call.setResponse(packet);
 68                } else {
 69                    if (packet.getOperation().equals(ClusterOperation.EVENT)) {
 70                        client.getListenerManager().enqueue(packet);
 71                    }
 72                    if (packet.getCallId() != -1) {
 73                        logger.log(Level.SEVERE, "In Thread can not handle: " + packet.getOperation() + " : " + packet.getCallId());
 74                    }
 75                }
 76            }
 77        } catch (Throwable e) {
 78            logger.log(Level.FINEST, "InRunnable [" + connection + "] got an exception:" + e.toString(), e);
 79            outRunnable.clusterIsDown(connection);
 80        }
 81    }
 82
 83    private void redoUnfinishedCalls(Connection oldConnection) {
 84        onDisconnect(oldConnection);
 85    }
 86
 87    public void shutdown() {
 88        synchronized (monitor) {
 89            if (running) {
 90                this.running = false;
 91                try {
 92                    Connection connection = client.connectionManager.getConnection();
 93                    if (connection != null) {
 94                        connection.close();
 95                    }
 96                } catch (IOException ignored) {
 97                }
 98                try {
 99                    monitor.wait(5000L);
100                } catch (InterruptedException ignored) {
101                }
102            }
103        }
104    }
105}