/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. package com.hazelcast.client;
  17. import com.hazelcast.impl.ClusterOperation;
  18. import com.hazelcast.util.Clock;
  19. import java.io.IOException;
  20. import java.util.Map;
  21. import java.util.logging.Level;
  22. public class InRunnable extends IORunnable implements Runnable {
  23. final PacketReader reader;
  24. Connection connection = null;
  25. private final OutRunnable outRunnable;
  26. volatile long lastReceived;
  27. public InRunnable(HazelcastClient client, OutRunnable outRunnable, Map<Long, Call> calls, PacketReader reader) {
  28. super(client, calls);
  29. this.outRunnable = outRunnable;
  30. this.reader = reader;
  31. }
  32. protected void customRun() throws InterruptedException {
  33. if (outRunnable.reconnection.get()) {
  34. Thread.sleep(10L);
  35. return;
  36. }
  37. Packet packet;
  38. try {
  39. Connection oldConnection = connection;
  40. connection = client.connectionManager.getConnection();
  41. if (restoredConnection(oldConnection, connection)) {
  42. if (outRunnable.sendReconnectCall(connection)) {
  43. logger.log(Level.FINEST, "restoredConnection");
  44. if (oldConnection != null) {
  45. redoUnfinishedCalls(oldConnection);
  46. }
  47. }
  48. return;
  49. }
  50. if (connection == null) {
  51. outRunnable.clusterIsDown(oldConnection);
  52. Thread.sleep(10);
  53. } else {
  54. packet = reader.readPacket(connection);
  55. // logger.log(Level.FINEST, "Reading " + packet.getOperation() + " Call id: " + packet.getCallId());
  56. this.lastReceived = Clock.currentTimeMillis();
  57. Call call = callMap.remove(packet.getCallId());
  58. if (call != null) {
  59. call.received = System.nanoTime();
  60. call.setResponse(packet);
  61. } else {
  62. if (packet.getOperation().equals(ClusterOperation.EVENT)) {
  63. client.getListenerManager().enqueue(packet);
  64. }
  65. if (packet.getCallId() != -1) {
  66. logger.log(Level.SEVERE, "In Thread can not handle: " + packet.getOperation() + " : " + packet.getCallId());
  67. }
  68. }
  69. }
  70. } catch (Throwable e) {
  71. logger.log(Level.FINEST, "InRunnable [" + connection + "] got an exception:" + e.toString(), e);
  72. outRunnable.clusterIsDown(connection);
  73. }
  74. }
  75. private void redoUnfinishedCalls(Connection oldConnection) {
  76. onDisconnect(oldConnection);
  77. }
  78. public void shutdown() {
  79. synchronized (monitor) {
  80. if (running) {
  81. this.running = false;
  82. try {
  83. Connection connection = client.connectionManager.getConnection();
  84. if (connection != null) {
  85. connection.close();
  86. }
  87. } catch (IOException ignored) {
  88. }
  89. try {
  90. monitor.wait(5000L);
  91. } catch (InterruptedException ignored) {
  92. }
  93. }
  94. }
  95. }
  96. }