Example usage for java.util.concurrent Semaphore Semaphore

List of usage examples for java.util.concurrent Semaphore Semaphore

Introduction

In this page you can find the example usage for java.util.concurrent Semaphore Semaphore.

Prototype

public Semaphore(int permits) 

Source Link

Document

Creates a Semaphore with the given number of permits and nonfair fairness setting.

Usage

From source file:com.arpnetworking.metrics.impl.ApacheHttpSinkTest.java

@Test
public void testHttpClientResponseException() throws InterruptedException, IOException {
    final CloseableHttpClient httpClient = Mockito.mock(CloseableHttpClient.class);
    final CloseableHttpResponse httpResponse = Mockito.mock(CloseableHttpResponse.class);
    Mockito.doReturn(httpResponse).when(httpClient).execute(Mockito.any(HttpPost.class));
    Mockito.doThrow(new NullPointerException("Throw by default")).when(httpResponse).getStatusLine();

    final org.slf4j.Logger logger = Mockito.mock(org.slf4j.Logger.class);
    final Semaphore semaphore = new Semaphore(0);
    final Sink sink = new ApacheHttpSink(
            new ApacheHttpSink.Builder().setUri(URI.create("http://nohost.example.com" + PATH))
                    .setEventHandler(new CompletionHandler(semaphore)),
            () -> httpClient, logger);

    final TsdEvent event = new TsdEvent(ANNOTATIONS, TEST_EMPTY_SERIALIZATION_TIMERS,
            TEST_EMPTY_SERIALIZATION_COUNTERS, TEST_EMPTY_SERIALIZATION_GAUGES);

    sink.record(event);//  ww w .  j  a va  2s.c o  m
    semaphore.acquire();

    // Verify mocks
    Mockito.verify(httpClient).execute(Mockito.any(HttpPost.class));
    Mockito.verifyNoMoreInteractions(httpClient);
    Mockito.verify(httpResponse).getStatusLine();
    Mockito.verify(httpResponse).close();
    Mockito.verifyNoMoreInteractions(httpResponse);

    // Request matcher
    final RequestPatternBuilder requestPattern = WireMock.postRequestedFor(WireMock.urlEqualTo(PATH))
            .withHeader("Content-Type", WireMock.equalTo("application/octet-stream"));

    // Assert that no data was sent
    _wireMockRule.verify(0, requestPattern);
    Assert.assertTrue(_wireMockRule.findUnmatchedRequests().getRequests().isEmpty());

    // Assert that the runtime exception was captured
    Mockito.verify(logger).error(
            Mockito.startsWith("Encountered failure when sending metrics to HTTP endpoint; uri="),
            Mockito.any(NullPointerException.class));
}

From source file:org.springframework.integration.ip.tcp.TcpSendingMessageHandlerTests.java

@Test
public void testNioSingleUseNoInbound() throws Exception {
    final int port = SocketUtils.findAvailableServerSocket();
    final CountDownLatch latch = new CountDownLatch(1);
    final Semaphore semaphore = new Semaphore(0);
    final AtomicBoolean done = new AtomicBoolean();
    Executors.newSingleThreadExecutor().execute(new Runnable() {
        public void run() {
            try {
                ServerSocket server = ServerSocketFactory.getDefault().createServerSocket(port);
                latch.countDown();/*www .j  av a2 s  . c o  m*/
                for (int i = 0; i < 2; i++) {
                    Socket socket = server.accept();
                    semaphore.release();
                    byte[] b = new byte[8];
                    readFully(socket.getInputStream(), b);
                    semaphore.release();
                    socket.close();
                }
                server.close();
            } catch (Exception e) {
                if (!done.get()) {
                    e.printStackTrace();
                }
            }
        }
    });
    AbstractConnectionFactory ccf = new TcpNioClientConnectionFactory("localhost", port);
    ByteArrayCrLfSerializer serializer = new ByteArrayCrLfSerializer();
    ccf.setSerializer(serializer);
    ccf.setDeserializer(serializer);
    ccf.setSoTimeout(5000);
    ccf.start();
    ccf.setSingleUse(true);
    TcpSendingMessageHandler handler = new TcpSendingMessageHandler();
    handler.setConnectionFactory(ccf);
    assertTrue(latch.await(10, TimeUnit.SECONDS));
    handler.handleMessage(MessageBuilder.withPayload("Test.1").build());
    handler.handleMessage(MessageBuilder.withPayload("Test.2").build());
    assertTrue(semaphore.tryAcquire(4, 10000, TimeUnit.MILLISECONDS));
    done.set(true);
    ccf.stop();
}

From source file:net.bluehack.ui.ChatActivity.java

@Override
public boolean onFragmentCreate() {
    final int chatId = arguments.getInt("chat_id", 0);
    final int userId = arguments.getInt("user_id", 0);
    final int encId = arguments.getInt("enc_id", 0);
    inlineReturn = arguments.getLong("inline_return", 0);
    String inlineQuery = arguments.getString("inline_query");
    startLoadFromMessageId = arguments.getInt("message_id", 0);
    int migrated_to = arguments.getInt("migrated_to", 0);
    scrollToTopOnResume = arguments.getBoolean("scrollToTopOnResume", false);

    if (chatId != 0) {
        currentChat = MessagesController.getInstance().getChat(chatId);
        if (currentChat == null) {
            final Semaphore semaphore = new Semaphore(0);
            MessagesStorage.getInstance().getStorageQueue().postRunnable(new Runnable() {
                @Override// w  w w .  j  a  v  a2  s  . c o m
                public void run() {
                    currentChat = MessagesStorage.getInstance().getChat(chatId);
                    semaphore.release();
                }
            });
            try {
                semaphore.acquire();
            } catch (Exception e) {
                FileLog.e("tmessages", e);
            }
            if (currentChat != null) {
                MessagesController.getInstance().putChat(currentChat, true);
            } else {
                return false;
            }
        }
        if (chatId > 0) {
            dialog_id = -chatId;
        } else {
            isBroadcast = true;
            dialog_id = AndroidUtilities.makeBroadcastId(chatId);
        }
        if (ChatObject.isChannel(currentChat)) {
            MessagesController.getInstance().startShortPoll(chatId, false);
        }
    } else if (userId != 0) {
        currentUser = MessagesController.getInstance().getUser(userId);
        if (currentUser == null) {
            final Semaphore semaphore = new Semaphore(0);
            MessagesStorage.getInstance().getStorageQueue().postRunnable(new Runnable() {
                @Override
                public void run() {
                    currentUser = MessagesStorage.getInstance().getUser(userId);
                    semaphore.release();
                }
            });
            try {
                semaphore.acquire();
            } catch (Exception e) {
                FileLog.e("tmessages", e);
            }
            if (currentUser != null) {
                MessagesController.getInstance().putUser(currentUser, true);
            } else {
                return false;
            }
        }
        dialog_id = userId;
        botUser = arguments.getString("botUser");
        if (inlineQuery != null) {
            MessagesController.getInstance().sendBotStart(currentUser, inlineQuery);
        }
    } else if (encId != 0) {
        currentEncryptedChat = MessagesController.getInstance().getEncryptedChat(encId);
        if (currentEncryptedChat == null) {
            final Semaphore semaphore = new Semaphore(0);
            MessagesStorage.getInstance().getStorageQueue().postRunnable(new Runnable() {
                @Override
                public void run() {
                    currentEncryptedChat = MessagesStorage.getInstance().getEncryptedChat(encId);
                    semaphore.release();
                }
            });
            try {
                semaphore.acquire();
            } catch (Exception e) {
                FileLog.e("tmessages", e);
            }
            if (currentEncryptedChat != null) {
                MessagesController.getInstance().putEncryptedChat(currentEncryptedChat, true);
            } else {
                return false;
            }
        }
        currentUser = MessagesController.getInstance().getUser(currentEncryptedChat.user_id);
        if (currentUser == null) {
            final Semaphore semaphore = new Semaphore(0);
            MessagesStorage.getInstance().getStorageQueue().postRunnable(new Runnable() {
                @Override
                public void run() {
                    currentUser = MessagesStorage.getInstance().getUser(currentEncryptedChat.user_id);
                    semaphore.release();
                }
            });
            try {
                semaphore.acquire();
            } catch (Exception e) {
                FileLog.e("tmessages", e);
            }
            if (currentUser != null) {
                MessagesController.getInstance().putUser(currentUser, true);
            } else {
                return false;
            }
        }
        dialog_id = ((long) encId) << 32;
        maxMessageId[0] = maxMessageId[1] = Integer.MIN_VALUE;
        minMessageId[0] = minMessageId[1] = Integer.MAX_VALUE;
        MediaController.getInstance().startMediaObserver();
    } else {
        return false;
    }

    NotificationCenter.getInstance().addObserver(this, NotificationCenter.messagesDidLoaded);
    NotificationCenter.getInstance().addObserver(this, NotificationCenter.emojiDidLoaded);
    NotificationCenter.getInstance().addObserver(this, NotificationCenter.updateInterfaces);
    NotificationCenter.getInstance().addObserver(this, NotificationCenter.didReceivedNewMessages);
    NotificationCenter.getInstance().addObserver(this, NotificationCenter.closeChats);
    NotificationCenter.getInstance().addObserver(this, NotificationCenter.messagesRead);
    NotificationCenter.getInstance().addObserver(this, NotificationCenter.messagesDeleted);
    NotificationCenter.getInstance().addObserver(this, NotificationCenter.messageReceivedByServer);
    NotificationCenter.getInstance().addObserver(this, NotificationCenter.messageReceivedByAck);
    NotificationCenter.getInstance().addObserver(this, NotificationCenter.messageSendError);
    NotificationCenter.getInstance().addObserver(this, NotificationCenter.chatInfoDidLoaded);
    NotificationCenter.getInstance().addObserver(this, NotificationCenter.contactsDidLoaded);
    NotificationCenter.getInstance().addObserver(this, NotificationCenter.encryptedChatUpdated);
    NotificationCenter.getInstance().addObserver(this, NotificationCenter.messagesReadEncrypted);
    NotificationCenter.getInstance().addObserver(this, NotificationCenter.removeAllMessagesFromDialog);
    NotificationCenter.getInstance().addObserver(this, NotificationCenter.audioProgressDidChanged);
    NotificationCenter.getInstance().addObserver(this, NotificationCenter.audioDidReset);
    NotificationCenter.getInstance().addObserver(this, NotificationCenter.audioPlayStateChanged);
    NotificationCenter.getInstance().addObserver(this, NotificationCenter.screenshotTook);
    NotificationCenter.getInstance().addObserver(this, NotificationCenter.blockedUsersDidLoaded);
    NotificationCenter.getInstance().addObserver(this, NotificationCenter.FileNewChunkAvailable);
    NotificationCenter.getInstance().addObserver(this, NotificationCenter.didCreatedNewDeleteTask);
    NotificationCenter.getInstance().addObserver(this, NotificationCenter.audioDidStarted);
    NotificationCenter.getInstance().addObserver(this, NotificationCenter.updateMessageMedia);
    NotificationCenter.getInstance().addObserver(this, NotificationCenter.replaceMessagesObjects);
    NotificationCenter.getInstance().addObserver(this, NotificationCenter.notificationsSettingsUpdated);
    NotificationCenter.getInstance().addObserver(this, NotificationCenter.didLoadedReplyMessages);
    NotificationCenter.getInstance().addObserver(this, NotificationCenter.didReceivedWebpages);
    NotificationCenter.getInstance().addObserver(this, NotificationCenter.didReceivedWebpagesInUpdates);
    NotificationCenter.getInstance().addObserver(this, NotificationCenter.messagesReadContent);
    NotificationCenter.getInstance().addObserver(this, NotificationCenter.botInfoDidLoaded);
    NotificationCenter.getInstance().addObserver(this, NotificationCenter.botKeyboardDidLoaded);
    NotificationCenter.getInstance().addObserver(this, NotificationCenter.chatSearchResultsAvailable);
    NotificationCenter.getInstance().addObserver(this, NotificationCenter.didUpdatedMessagesViews);
    NotificationCenter.getInstance().addObserver(this, NotificationCenter.chatInfoCantLoad);
    NotificationCenter.getInstance().addObserver(this, NotificationCenter.didLoadedPinnedMessage);
    NotificationCenter.getInstance().addObserver(this, NotificationCenter.peerSettingsDidLoaded);
    NotificationCenter.getInstance().addObserver(this, NotificationCenter.newDraftReceived);

    super.onFragmentCreate();

    if (currentEncryptedChat == null && !isBroadcast) {
        BotQuery.loadBotKeyboard(dialog_id);
    }

    loading = true;
    MessagesController.getInstance().loadPeerSettings(dialog_id, currentUser, currentChat);
    MessagesController.getInstance().setLastCreatedDialogId(dialog_id, true);
    if (startLoadFromMessageId != 0) {
        needSelectFromMessageId = true;
        waitingForLoad.add(lastLoadIndex);
        if (migrated_to != 0) {
            mergeDialogId = migrated_to;
            MessagesController.getInstance().loadMessages(mergeDialogId, AndroidUtilities.isTablet() ? 30 : 20,
                    startLoadFromMessageId, true, 0, classGuid, 3, 0, ChatObject.isChannel(currentChat),
                    lastLoadIndex++);
        } else {
            MessagesController.getInstance().loadMessages(dialog_id, AndroidUtilities.isTablet() ? 30 : 20,
                    startLoadFromMessageId, true, 0, classGuid, 3, 0, ChatObject.isChannel(currentChat),
                    lastLoadIndex++);
        }
    } else {
        waitingForLoad.add(lastLoadIndex);
        MessagesController.getInstance().loadMessages(dialog_id, AndroidUtilities.isTablet() ? 30 : 20, 0, true,
                0, classGuid, 2, 0, ChatObject.isChannel(currentChat), lastLoadIndex++);
    }

    if (currentChat != null) {
        Semaphore semaphore = null;
        if (isBroadcast) {
            semaphore = new Semaphore(0);
        }
        MessagesController.getInstance().loadChatInfo(currentChat.id, semaphore,
                ChatObject.isChannel(currentChat));
        if (isBroadcast && semaphore != null) {
            try {
                semaphore.acquire();
            } catch (Exception e) {
                FileLog.e("tmessages", e);
            }
        }
    }

    if (userId != 0 && currentUser.bot) {
        BotQuery.loadBotInfo(userId, true, classGuid);
    } else if (info instanceof TLRPC.TL_chatFull) {
        for (int a = 0; a < info.participants.participants.size(); a++) {
            TLRPC.ChatParticipant participant = info.participants.participants.get(a);
            TLRPC.User user = MessagesController.getInstance().getUser(participant.user_id);
            if (user != null && user.bot) {
                BotQuery.loadBotInfo(user.id, true, classGuid);
            }
        }
    }

    if (currentUser != null) {
        userBlocked = MessagesController.getInstance().blockedUsers.contains(currentUser.id);
    }

    if (AndroidUtilities.isTablet()) {
        NotificationCenter.getInstance().postNotificationName(NotificationCenter.openedChatChanged, dialog_id,
                false);
    }

    if (currentEncryptedChat != null && AndroidUtilities
            .getMyLayerVersion(currentEncryptedChat.layer) != SecretChatHelper.CURRENT_SECRET_CHAT_LAYER) {
        SecretChatHelper.getInstance().sendNotifyLayerMessage(currentEncryptedChat, null);
    }

    return true;
}

From source file:kr.wdream.ui.ChatActivity.java

@Override
public boolean onFragmentCreate() {
    final int chatId = arguments.getInt("chat_id", 0);
    final int userId = arguments.getInt("user_id", 0);
    final int encId = arguments.getInt("enc_id", 0);
    inlineReturn = arguments.getLong("inline_return", 0);
    String inlineQuery = arguments.getString("inline_query");
    startLoadFromMessageId = arguments.getInt("message_id", 0);
    int migrated_to = arguments.getInt("migrated_to", 0);
    scrollToTopOnResume = arguments.getBoolean("scrollToTopOnResume", false);

    if (chatId != 0) {
        currentChat = MessagesController.getInstance().getChat(chatId);
        if (currentChat == null) {
            final Semaphore semaphore = new Semaphore(0);
            MessagesStorage.getInstance().getStorageQueue().postRunnable(new Runnable() {
                @Override//from  www  .  j a v  a 2 s.c  o  m
                public void run() {
                    currentChat = MessagesStorage.getInstance().getChat(chatId);
                    semaphore.release();

                    Log.d(LOG_TAG, "currentChat : " + currentChat);
                }
            });
            try {
                semaphore.acquire();
            } catch (Exception e) {
                FileLog.e("tmessages", e);
            }
            if (currentChat != null) {
                MessagesController.getInstance().putChat(currentChat, true);

                Log.d(LOG_TAG, "currentChat2 : " + currentChat);
            } else {
                return false;
            }
        }
        if (chatId > 0) {
            dialog_id = -chatId;
        } else {
            isBroadcast = true;
            dialog_id = AndroidUtilities.makeBroadcastId(chatId);
        }
        if (ChatObject.isChannel(currentChat)) {
            MessagesController.getInstance().startShortPoll(chatId, false);
        }
    } else if (userId != 0) {
        currentUser = MessagesController.getInstance().getUser(userId);
        if (currentUser == null) {
            final Semaphore semaphore = new Semaphore(0);
            MessagesStorage.getInstance().getStorageQueue().postRunnable(new Runnable() {
                @Override
                public void run() {
                    currentUser = MessagesStorage.getInstance().getUser(userId);
                    semaphore.release();
                }
            });
            try {
                semaphore.acquire();
            } catch (Exception e) {
                FileLog.e("tmessages", e);
            }
            if (currentUser != null) {
                MessagesController.getInstance().putUser(currentUser, true);
            } else {
                return false;
            }
        }
        dialog_id = userId;
        botUser = arguments.getString("botUser");
        if (inlineQuery != null) {
            MessagesController.getInstance().sendBotStart(currentUser, inlineQuery);
        }
    } else if (encId != 0) {
        currentEncryptedChat = MessagesController.getInstance().getEncryptedChat(encId);
        if (currentEncryptedChat == null) {
            final Semaphore semaphore = new Semaphore(0);
            MessagesStorage.getInstance().getStorageQueue().postRunnable(new Runnable() {
                @Override
                public void run() {
                    currentEncryptedChat = MessagesStorage.getInstance().getEncryptedChat(encId);
                    semaphore.release();
                }
            });
            try {
                semaphore.acquire();
            } catch (Exception e) {
                FileLog.e("tmessages", e);
            }
            if (currentEncryptedChat != null) {
                MessagesController.getInstance().putEncryptedChat(currentEncryptedChat, true);
            } else {
                return false;
            }
        }
        currentUser = MessagesController.getInstance().getUser(currentEncryptedChat.user_id);
        if (currentUser == null) {
            final Semaphore semaphore = new Semaphore(0);
            MessagesStorage.getInstance().getStorageQueue().postRunnable(new Runnable() {
                @Override
                public void run() {
                    currentUser = MessagesStorage.getInstance().getUser(currentEncryptedChat.user_id);
                    semaphore.release();
                }
            });
            try {
                semaphore.acquire();
            } catch (Exception e) {
                FileLog.e("tmessages", e);
            }
            if (currentUser != null) {
                MessagesController.getInstance().putUser(currentUser, true);
            } else {
                return false;
            }
        }
        dialog_id = ((long) encId) << 32;
        maxMessageId[0] = maxMessageId[1] = Integer.MIN_VALUE;
        minMessageId[0] = minMessageId[1] = Integer.MAX_VALUE;
        MediaController.getInstance().startMediaObserver();
    } else {
        return false;
    }

    NotificationCenter.getInstance().addObserver(this, NotificationCenter.messagesDidLoaded);
    NotificationCenter.getInstance().addObserver(this, NotificationCenter.emojiDidLoaded);
    NotificationCenter.getInstance().addObserver(this, NotificationCenter.updateInterfaces);
    NotificationCenter.getInstance().addObserver(this, NotificationCenter.didReceivedNewMessages);
    NotificationCenter.getInstance().addObserver(this, NotificationCenter.closeChats);
    NotificationCenter.getInstance().addObserver(this, NotificationCenter.messagesRead);
    NotificationCenter.getInstance().addObserver(this, NotificationCenter.messagesDeleted);
    NotificationCenter.getInstance().addObserver(this, NotificationCenter.messageReceivedByServer);
    NotificationCenter.getInstance().addObserver(this, NotificationCenter.messageReceivedByAck);
    NotificationCenter.getInstance().addObserver(this, NotificationCenter.messageSendError);
    NotificationCenter.getInstance().addObserver(this, NotificationCenter.chatInfoDidLoaded);
    NotificationCenter.getInstance().addObserver(this, NotificationCenter.contactsDidLoaded);
    NotificationCenter.getInstance().addObserver(this, NotificationCenter.encryptedChatUpdated);
    NotificationCenter.getInstance().addObserver(this, NotificationCenter.messagesReadEncrypted);
    NotificationCenter.getInstance().addObserver(this, NotificationCenter.removeAllMessagesFromDialog);
    NotificationCenter.getInstance().addObserver(this, NotificationCenter.audioProgressDidChanged);
    NotificationCenter.getInstance().addObserver(this, NotificationCenter.audioDidReset);
    NotificationCenter.getInstance().addObserver(this, NotificationCenter.audioPlayStateChanged);
    NotificationCenter.getInstance().addObserver(this, NotificationCenter.screenshotTook);
    NotificationCenter.getInstance().addObserver(this, NotificationCenter.blockedUsersDidLoaded);
    NotificationCenter.getInstance().addObserver(this, NotificationCenter.FileNewChunkAvailable);
    NotificationCenter.getInstance().addObserver(this, NotificationCenter.didCreatedNewDeleteTask);
    NotificationCenter.getInstance().addObserver(this, NotificationCenter.audioDidStarted);
    NotificationCenter.getInstance().addObserver(this, NotificationCenter.updateMessageMedia);
    NotificationCenter.getInstance().addObserver(this, NotificationCenter.replaceMessagesObjects);
    NotificationCenter.getInstance().addObserver(this, NotificationCenter.notificationsSettingsUpdated);
    NotificationCenter.getInstance().addObserver(this, NotificationCenter.didLoadedReplyMessages);
    NotificationCenter.getInstance().addObserver(this, NotificationCenter.didReceivedWebpages);
    NotificationCenter.getInstance().addObserver(this, NotificationCenter.didReceivedWebpagesInUpdates);
    NotificationCenter.getInstance().addObserver(this, NotificationCenter.messagesReadContent);
    NotificationCenter.getInstance().addObserver(this, NotificationCenter.botInfoDidLoaded);
    NotificationCenter.getInstance().addObserver(this, NotificationCenter.botKeyboardDidLoaded);
    NotificationCenter.getInstance().addObserver(this, NotificationCenter.chatSearchResultsAvailable);
    NotificationCenter.getInstance().addObserver(this, NotificationCenter.didUpdatedMessagesViews);
    NotificationCenter.getInstance().addObserver(this, NotificationCenter.chatInfoCantLoad);
    NotificationCenter.getInstance().addObserver(this, NotificationCenter.didLoadedPinnedMessage);
    NotificationCenter.getInstance().addObserver(this, NotificationCenter.peerSettingsDidLoaded);
    NotificationCenter.getInstance().addObserver(this, NotificationCenter.newDraftReceived);

    super.onFragmentCreate();

    if (currentEncryptedChat == null && !isBroadcast) {
        BotQuery.loadBotKeyboard(dialog_id);
    }

    loading = true;
    MessagesController.getInstance().loadPeerSettings(dialog_id, currentUser, currentChat);
    MessagesController.getInstance().setLastCreatedDialogId(dialog_id, true);
    if (startLoadFromMessageId != 0) {
        needSelectFromMessageId = true;
        waitingForLoad.add(lastLoadIndex);
        if (migrated_to != 0) {
            mergeDialogId = migrated_to;
            MessagesController.getInstance().loadMessages(mergeDialogId, AndroidUtilities.isTablet() ? 30 : 20,
                    startLoadFromMessageId, true, 0, classGuid, 3, 0, ChatObject.isChannel(currentChat),
                    lastLoadIndex++);
        } else {
            MessagesController.getInstance().loadMessages(dialog_id, AndroidUtilities.isTablet() ? 30 : 20,
                    startLoadFromMessageId, true, 0, classGuid, 3, 0, ChatObject.isChannel(currentChat),
                    lastLoadIndex++);
        }
    } else {
        waitingForLoad.add(lastLoadIndex);
        MessagesController.getInstance().loadMessages(dialog_id, AndroidUtilities.isTablet() ? 30 : 20, 0, true,
                0, classGuid, 2, 0, ChatObject.isChannel(currentChat), lastLoadIndex++);
    }

    if (currentChat != null) {
        Semaphore semaphore = null;
        if (isBroadcast) {
            semaphore = new Semaphore(0);
        }
        MessagesController.getInstance().loadChatInfo(currentChat.id, semaphore,
                ChatObject.isChannel(currentChat));
        if (isBroadcast && semaphore != null) {
            try {
                semaphore.acquire();
            } catch (Exception e) {
                FileLog.e("tmessages", e);
            }
        }
    }

    if (userId != 0 && currentUser.bot) {
        BotQuery.loadBotInfo(userId, true, classGuid);
    } else if (info instanceof TLRPC.TL_chatFull) {
        for (int a = 0; a < info.participants.participants.size(); a++) {
            TLRPC.ChatParticipant participant = info.participants.participants.get(a);
            TLRPC.User user = MessagesController.getInstance().getUser(participant.user_id);
            if (user != null && user.bot) {
                BotQuery.loadBotInfo(user.id, true, classGuid);
            }
        }
    }

    if (currentUser != null) {
        userBlocked = MessagesController.getInstance().blockedUsers.contains(currentUser.id);
    }

    if (AndroidUtilities.isTablet()) {
        NotificationCenter.getInstance().postNotificationName(NotificationCenter.openedChatChanged, dialog_id,
                false);
    }

    if (currentEncryptedChat != null && AndroidUtilities
            .getMyLayerVersion(currentEncryptedChat.layer) != SecretChatHelper.CURRENT_SECRET_CHAT_LAYER) {
        SecretChatHelper.getInstance().sendNotifyLayerMessage(currentEncryptedChat, null);
    }

    return true;
}

From source file:org.springframework.batch.core.jsr.launch.JsrJobOperator.java

/**
 * Creates a child {@link ApplicationContext} for the job being requested based upon
 * the /META-INF/batch.xml (if exists) and the /META-INF/batch-jobs/&lt;jobName&gt;.xml
 * configuration and launches the job.  Per JSR-352, calls to this method will always
 * create a new {@link JobInstance} (and related {@link JobExecution}).
 *
 * @param jobName the name of the job XML file without the .xml that is located within the
 * /META-INF/batch-jobs directory.// www  .  java 2 s. c o m
 * @param params any job parameters to be used during the execution of this job.
 */
@Override
public long start(String jobName, Properties params) throws JobStartException, JobSecurityException {
    final JsrXmlApplicationContext batchContext = new JsrXmlApplicationContext(params);
    batchContext.setValidating(false);

    Resource batchXml = new ClassPathResource("/META-INF/batch.xml");
    String jobConfigurationLocation = "/META-INF/batch-jobs/" + jobName + ".xml";
    Resource jobXml = new ClassPathResource(jobConfigurationLocation);

    if (batchXml.exists()) {
        batchContext.load(batchXml);
    }

    if (jobXml.exists()) {
        batchContext.load(jobXml);
    }

    AbstractBeanDefinition beanDefinition = BeanDefinitionBuilder
            .genericBeanDefinition("org.springframework.batch.core.jsr.JsrJobContextFactoryBean")
            .getBeanDefinition();
    beanDefinition.setScope(BeanDefinition.SCOPE_SINGLETON);
    batchContext.registerBeanDefinition(JSR_JOB_CONTEXT_BEAN_NAME, beanDefinition);

    if (baseContext != null) {
        batchContext.setParent(baseContext);
    } else {
        batchContext.getBeanFactory().registerSingleton("jobExplorer", jobExplorer);
        batchContext.getBeanFactory().registerSingleton("jobRepository", jobRepository);
        batchContext.getBeanFactory().registerSingleton("jobParametersConverter", jobParametersConverter);
        batchContext.getBeanFactory().registerSingleton("transactionManager", transactionManager);
    }

    try {
        batchContext.refresh();
    } catch (BeanCreationException e) {
        throw new JobStartException(e);
    }

    Assert.notNull(jobName, "The job name must not be null.");

    final org.springframework.batch.core.JobExecution jobExecution;

    try {
        JobParameters jobParameters = jobParametersConverter.getJobParameters(params);
        String[] jobNames = batchContext.getBeanNamesForType(Job.class);

        if (jobNames == null || jobNames.length <= 0) {
            throw new BatchRuntimeException("No Job defined in current context");
        }

        org.springframework.batch.core.JobInstance jobInstance = jobRepository.createJobInstance(jobNames[0],
                jobParameters);
        jobExecution = jobRepository.createJobExecution(jobInstance, jobParameters, jobConfigurationLocation);
    } catch (Exception e) {
        throw new JobStartException(e);
    }

    try {
        final Semaphore semaphore = new Semaphore(1);
        final List<Exception> exceptionHolder = Collections.synchronizedList(new ArrayList<Exception>());
        semaphore.acquire();

        taskExecutor.execute(new Runnable() {

            @Override
            public void run() {
                JsrJobContextFactoryBean factoryBean = null;
                try {
                    factoryBean = (JsrJobContextFactoryBean) batchContext
                            .getBean("&" + JSR_JOB_CONTEXT_BEAN_NAME);
                    factoryBean.setJobExecution(jobExecution);
                    final Job job = batchContext.getBean(Job.class);
                    semaphore.release();
                    // Initialization of the JobExecution for job level dependencies
                    jobRegistry.register(job, jobExecution);
                    job.execute(jobExecution);
                    jobRegistry.remove(jobExecution);
                } catch (Exception e) {
                    exceptionHolder.add(e);
                } finally {
                    if (factoryBean != null) {
                        factoryBean.close();
                    }

                    batchContext.close();

                    if (semaphore.availablePermits() == 0) {
                        semaphore.release();
                    }
                }
            }
        });

        semaphore.acquire();
        if (exceptionHolder.size() > 0) {
            semaphore.release();
            throw new JobStartException(exceptionHolder.get(0));
        }
    } catch (Exception e) {
        if (jobRegistry.exists(jobExecution.getId())) {
            jobRegistry.remove(jobExecution);
        }
        jobExecution.upgradeStatus(BatchStatus.FAILED);
        if (jobExecution.getExitStatus().equals(ExitStatus.UNKNOWN)) {
            jobExecution.setExitStatus(ExitStatus.FAILED.addExitDescription(e));
        }
        jobRepository.update(jobExecution);

        if (batchContext.isActive()) {
            batchContext.close();
        }

        throw new JobStartException(e);
    }
    return jobExecution.getId();
}

From source file:com.amazonaws.services.kinesis.clientlibrary.lib.worker.WorkerTest.java

/**
 * This test is testing the {@link Worker}'s shutdown behavior and by extension the behavior of
 * {@link ThreadPoolExecutor#shutdownNow()}. It depends on the thread pool sending an interrupt to the pool threads.
 * This behavior makes the test a bit racy, since we need to ensure a specific order of events.
 * // w  w w  . j a  v  a 2 s . c  o m
 * @throws Exception
 */
@Test
public final void testWorkerForcefulShutdown() throws Exception {
    final List<Shard> shardList = createShardListWithOneShard();
    final boolean callProcessRecordsForEmptyRecordList = true;
    final long failoverTimeMillis = 50L;
    final int numberOfRecordsPerShard = 10;

    final List<KinesisClientLease> initialLeases = new ArrayList<KinesisClientLease>();
    for (Shard shard : shardList) {
        KinesisClientLease lease = ShardSyncer.newKCLLease(shard);
        lease.setCheckpoint(ExtendedSequenceNumber.TRIM_HORIZON);
        initialLeases.add(lease);
    }

    final File file = KinesisLocalFileDataCreator.generateTempDataFile(shardList, numberOfRecordsPerShard,
            "normalShutdownUnitTest");
    final IKinesisProxy fileBasedProxy = new KinesisLocalFileProxy(file.getAbsolutePath());

    // Get executor service that will be owned by the worker, so we can get interrupts.
    ExecutorService executorService = getWorkerThreadPoolExecutor();

    // Make test case as efficient as possible.
    final CountDownLatch processRecordsLatch = new CountDownLatch(1);
    final AtomicBoolean recordProcessorInterrupted = new AtomicBoolean(false);
    when(v2RecordProcessorFactory.createProcessor()).thenReturn(v2RecordProcessor);
    final Semaphore actionBlocker = new Semaphore(1);
    final Semaphore shutdownBlocker = new Semaphore(1);

    actionBlocker.acquire();

    doAnswer(new Answer<Object>() {
        @Override
        public Object answer(InvocationOnMock invocation) throws Throwable {
            // Signal that record processor has started processing records.
            processRecordsLatch.countDown();

            // Block for some time now to test forceful shutdown. Also, check if record processor
            // was interrupted or not.
            final long startTimeMillis = System.currentTimeMillis();
            long elapsedTimeMillis = 0;

            LOG.info("Entering sleep @ " + startTimeMillis + " with elapsedMills: " + elapsedTimeMillis);
            shutdownBlocker.acquire();
            try {
                actionBlocker.acquire();
            } catch (InterruptedException e) {
                LOG.info("Sleep interrupted @ " + System.currentTimeMillis() + " elapsedMillis: "
                        + (System.currentTimeMillis() - startTimeMillis));
                recordProcessorInterrupted.getAndSet(true);
            }
            shutdownBlocker.release();
            elapsedTimeMillis = System.currentTimeMillis() - startTimeMillis;
            LOG.info(
                    "Sleep completed @ " + System.currentTimeMillis() + " elapsedMillis: " + elapsedTimeMillis);

            return null;
        }
    }).when(v2RecordProcessor).processRecords(any(ProcessRecordsInput.class));

    WorkerThread workerThread = runWorker(shardList, initialLeases, callProcessRecordsForEmptyRecordList,
            failoverTimeMillis, numberOfRecordsPerShard, fileBasedProxy, v2RecordProcessorFactory,
            executorService, nullMetricsFactory);

    // Only sleep for time that is required.
    processRecordsLatch.await();

    // Make sure record processor is initialized and processing records.
    verify(v2RecordProcessorFactory, times(1)).createProcessor();
    verify(v2RecordProcessor, times(1)).initialize(any(InitializationInput.class));
    verify(v2RecordProcessor, atLeast(1)).processRecords(any(ProcessRecordsInput.class));
    verify(v2RecordProcessor, times(0)).shutdown(any(ShutdownInput.class));

    workerThread.getWorker().shutdown();
    workerThread.join();

    Assert.assertTrue(workerThread.getState() == State.TERMINATED);
    // Shutdown should not be called in this case because record processor is blocked.
    verify(v2RecordProcessor, times(0)).shutdown(any(ShutdownInput.class));

    //
    // Release the worker thread
    //
    actionBlocker.release();
    //
    // Give the worker thread time to execute it's interrupted handler.
    //
    shutdownBlocker.tryAcquire(100, TimeUnit.MILLISECONDS);
    //
    // Now we can see if it was actually interrupted. It's possible it wasn't and this will fail.
    //
    assertThat(recordProcessorInterrupted.get(), equalTo(true));
}

From source file:org.apache.solr.cloud.TestTlogReplica.java

@SuppressWarnings("unchecked")
@BadApple(bugUrl = "https://issues.apache.org/jira/browse/SOLR-12028")
public void testRecovery() throws Exception {
    boolean useKill = random().nextBoolean();
    createAndWaitForCollection(1, 0, 2, 0);

    CloudSolrClient cloudClient = cluster.getSolrClient();
    new UpdateRequest().add(sdoc("id", "3")).add(sdoc("id", "4")).commit(cloudClient, collectionName);
    new UpdateRequest().add(sdoc("id", "5")).process(cloudClient, collectionName);
    JettySolrRunner solrRunner = getSolrRunner(false).get(0);
    if (useKill) {
        ChaosMonkey.kill(solrRunner);//w ww.j a v a2 s  .c om
    } else {
        ChaosMonkey.stop(solrRunner);
    }
    waitForState("Replica still up", collectionName, activeReplicaCount(0, 1, 0));
    new UpdateRequest().add(sdoc("id", "6")).process(cloudClient, collectionName);
    ChaosMonkey.start(solrRunner);
    waitForState("Replica didn't recover", collectionName, activeReplicaCount(0, 2, 0));
    // We skip peerSync, so replica will always trigger commit on leader
    // We query only the non-leader replicas, since we haven't opened a new searcher on the leader yet
    waitForNumDocsInAllReplicas(4, getNonLeaderReplias(collectionName), 10); //timeout for stale collection state

    // If I add the doc immediately, the leader fails to communicate with the follower with broken pipe.
    // Options are, wait or retry...
    for (int i = 0; i < 3; i++) {
        UpdateRequest ureq = new UpdateRequest().add(sdoc("id", "7"));
        ureq.setParam("collection", collectionName);
        ureq.setParam(UpdateRequest.MIN_REPFACT, "2");
        NamedList<Object> response = cloudClient.request(ureq);
        if ((Integer) ((NamedList<Object>) response.get("responseHeader")).get(UpdateRequest.REPFACT) >= 2) {
            break;
        }
        LOG.info("Min RF not achieved yet. retrying");
    }
    checkRTG(3, 7, cluster.getJettySolrRunners());
    DirectUpdateHandler2.commitOnClose = false;
    ChaosMonkey.stop(solrRunner);
    waitForState("Replica still up", collectionName, activeReplicaCount(0, 1, 0));
    DirectUpdateHandler2.commitOnClose = true;
    ChaosMonkey.start(solrRunner);
    waitForState("Replica didn't recover", collectionName, activeReplicaCount(0, 2, 0));
    waitForNumDocsInAllReplicas(5, getNonLeaderReplias(collectionName), 10); //timeout for stale collection state
    checkRTG(3, 7, cluster.getJettySolrRunners());
    cluster.getSolrClient().commit(collectionName);

    // Test replica recovery apply buffer updates
    Semaphore waitingForBufferUpdates = new Semaphore(0);
    Semaphore waitingForReplay = new Semaphore(0);
    RecoveryStrategy.testing_beforeReplayBufferingUpdates = () -> {
        try {
            waitingForReplay.release();
            waitingForBufferUpdates.acquire();
        } catch (InterruptedException e) {
            e.printStackTrace();
            fail("Test interrupted: " + e.getMessage());
        }
    };
    if (useKill) {
        ChaosMonkey.kill(solrRunner);
    } else {
        ChaosMonkey.stop(solrRunner);
    }
    ChaosMonkey.start(solrRunner);
    waitingForReplay.acquire();
    new UpdateRequest().add(sdoc("id", "8")).add(sdoc("id", "9")).process(cloudClient, collectionName);
    waitingForBufferUpdates.release();
    RecoveryStrategy.testing_beforeReplayBufferingUpdates = null;
    waitForState("Replica didn't recover", collectionName, activeReplicaCount(0, 2, 0));
    checkRTG(3, 9, cluster.getJettySolrRunners());
    for (SolrCore solrCore : getSolrCore(false)) {
        RefCounted<IndexWriter> iwRef = solrCore.getUpdateHandler().getSolrCoreState().getIndexWriter(null);
        assertFalse("IndexWriter at replicas must not see updates ", iwRef.get().hasUncommittedChanges());
        iwRef.decref();
    }
}

From source file:edu.illinois.enforcemop.examples.jbosscache.PessimisticSyncReplTxTest.java

/**
 * Have both cache1 and cache2 do add and commit. cache1 commit should time
 * out since it can't obtain the lock when trying to replicate cache2. On the
 * other hand, cache2 commit will succeed since now that cache1 is rollbacked
 * and lock is released.// ww  w. j  a v  a2 s  .c  om
 */
public void testPutTx1() throws Exception {
    final CacheSPI<Object, Object> c1 = this.cache1;

    final Semaphore threadOneFirstPart = new Semaphore(0);
    final Semaphore threadTwoFirstPart = new Semaphore(0);
    final Semaphore threadOneSecondPart = new Semaphore(0);

    Thread t1 = new Thread() {
        public void run() {
            TransactionManager tm;

            try {
                tm = beginTransaction();
                c1.put("/a/b/c", "age", 38);
                c1.put("/a/b/c", "age", 39);
                threadOneFirstPart.release();

                threadTwoFirstPart.acquire();
                try {
                    tm.commit();
                } catch (RollbackException ex) {
                } finally {
                    threadOneSecondPart.release();
                }
            } catch (Throwable ex) {
                ex.printStackTrace();
                t1_ex = ex;
            }
        }
    };

    Thread t2 = new Thread() {
        public void run() {
            TransactionManager tm;

            try {
                threadOneFirstPart.acquire();
                tm = beginTransaction();
                assertNull(cache2.get("/a/b/c", "age"));// must be null as not yet
                                                        // committed
                cache2.put("/a/b/c", "age", 40);

                threadTwoFirstPart.release();

                threadOneSecondPart.acquire();
                assertEquals(40, cache2.get("/a/b/c", "age"));// must not be null
                tm.commit();

                tm = beginTransaction();
                assertEquals("After cache2 commit", 40, cache2.get("/a/b/c", "age"));
                tm.commit();
            } catch (Throwable ex) {
                ex.printStackTrace();
                t2_ex = ex;
            } finally {
                lock.release();
            }
        }
    };

    // Let the game start
    t1.start();
    t2.start();

    t1.join();
    t2.join();

    if (t1_ex != null) {
        fail("Thread1 failed: " + t1_ex);
    }
    if (t2_ex != null) {
        fail("Thread2 failed: " + t2_ex);
    }
}

From source file:com.parse.ParsePushTest.java

@Test
public void testSendMessageInBackgroundWithCallback() throws Exception {
    // Mock controller
    ParsePushController controller = mock(ParsePushController.class);
    when(controller.sendInBackground(any(ParsePush.State.class), anyString()))
            .thenReturn(Task.<Void>forResult(null));
    ParseCorePlugins.getInstance().registerPushController(controller);

    // Make sample ParsePush data and call method
    ParseQuery<ParseInstallation> query = ParseInstallation.getQuery();
    query.getBuilder().whereEqualTo("foo", "bar");
    final Semaphore done = new Semaphore(0);
    final Capture<Exception> exceptionCapture = new Capture<>();
    ParsePush.sendMessageInBackground("test", query, new SendCallback() {
        @Override/*from  w w w. j av  a  2s .  c  om*/
        public void done(ParseException e) {
            exceptionCapture.set(e);
            done.release();
        }
    });

    // Make sure controller is executed and state parameter is correct
    assertNull(exceptionCapture.get());
    assertTrue(done.tryAcquire(1, 10, TimeUnit.SECONDS));
    ArgumentCaptor<ParsePush.State> stateCaptor = ArgumentCaptor.forClass(ParsePush.State.class);
    verify(controller, times(1)).sendInBackground(stateCaptor.capture(), anyString());
    ParsePush.State state = stateCaptor.getValue();
    // Verify query state
    ParseQuery.State<ParseInstallation> queryState = state.queryState();
    JSONObject queryStateJson = queryState.toJSON(PointerEncoder.get());
    assertEquals("bar", queryStateJson.getJSONObject("where").getString("foo"));
    // Verify message
    assertEquals("test", state.data().getString(ParsePush.KEY_DATA_MESSAGE));
}

From source file:org.commoncrawl.service.listcrawler.CacheManager.java

private final void flushLocalLog(final long bytesToRemove, final int itemsToRemove,
        final List<FingerprintAndOffsetTuple> flushedTupleList,
        final ArrayList<IndexDataFileTriple> tempFileTriples) {

    LOG.info("Acquiring Log Access Semaphores");
    // first boost this thread's priority ... 
    int originalThreadPriority = Thread.currentThread().getPriority();
    Thread.currentThread().setPriority(Thread.MAX_PRIORITY);
    // next acquire all permits to the local access log ... block until we get there ... 
    getLocalLogAccessSemaphore().acquireUninterruptibly(LOG_ACCESS_SEMAPHORE_COUNT);
    // now that we have all the semaphores we need, reduce the thread's priority to normal
    Thread.currentThread().setPriority(originalThreadPriority);
    LOG.info("Acquired ALL Log Access Semaphores");

    long timeStart = System.currentTimeMillis();

    // now we have exclusive access to the local transaction log ... 
    File activeLogFilePath = getActiveLogFilePath();
    File checkpointLogFilePath = getCheckpointLogFilePath();
    try {//from   ww w.  j  ava2 s.com
        // delete checkpoint file if it existed ... 
        checkpointLogFilePath.delete();
        // now rename activelog to checkpoint path 
        activeLogFilePath.renameTo(checkpointLogFilePath);

        long logFileConsolidationStartTime = System.currentTimeMillis();
        // now trap for exceptions in case something fails 
        try {
            // fix up the header ... 
            _header._fileSize -= bytesToRemove;
            _header._itemCount -= itemsToRemove;

            // open a old file and new file 
            RandomAccessFile newFile = new RandomAccessFile(activeLogFilePath, "rw");
            RandomAccessFile oldFile = new RandomAccessFile(checkpointLogFilePath, "r");

            LOG.info("Opened new and old files. New Header FileSize is:" + _header._fileSize + " ItemCount:"
                    + _header._itemCount);
            try {
                // write out header ...
                long bytesRemainingInLogFile = _header._fileSize;

                LOG.info("Writing Header to New File. Bytes Remaining for Data are:" + bytesRemainingInLogFile);
                // write header to new file ... 
                _header.writeHeader(newFile);
                // decrement bytes available ... 
                bytesRemainingInLogFile -= LocalLogFileHeader.SIZE;

                if (bytesRemainingInLogFile != 0) {
                    byte transferBuffer[] = new byte[(1 << 20) * 16];
                    LOG.info("Seeking old file past flushed data (pos:" + LocalLogFileHeader.SIZE
                            + bytesToRemove + ")");
                    // seek past old data ... 
                    oldFile.seek(LocalLogFileHeader.SIZE + bytesToRemove);
                    // and copy across remaining data 
                    while (bytesRemainingInLogFile != 0) {
                        int bytesToReadWriteThisIteration = Math.min((int) bytesRemainingInLogFile,
                                transferBuffer.length);
                        oldFile.read(transferBuffer, 0, bytesToReadWriteThisIteration);
                        newFile.write(transferBuffer, 0, bytesToReadWriteThisIteration);
                        LOG.info("Copied " + bytesToReadWriteThisIteration + " from Old to New");
                        bytesRemainingInLogFile -= bytesToReadWriteThisIteration;
                    }
                }
            } finally {
                if (newFile != null) {
                    newFile.close();
                }
                if (oldFile != null) {
                    oldFile.close();
                }
            }
            // if we reached here then checkpoint was successfull ... 
            LOG.info("Checkpoint - Log Consolidation Successfull! TOOK:"
                    + (System.currentTimeMillis() - logFileConsolidationStartTime));

            LOG.info("Loading Index Files");
            for (IndexDataFileTriple triple : tempFileTriples) {
                LOG.info("Loading Index File:" + triple._localIndexFilePath);
                final HDFSFileIndex fileIndex = new HDFSFileIndex(_remoteFileSystem, triple._localIndexFilePath,
                        triple._dataFilePath);
                LOG.info("Loaded Index File");
                // update hdfs index list ... 
                synchronized (CacheManager.this) {
                    LOG.info("Adding HDFS Index to list");
                    _hdfsIndexList.addElement(fileIndex);
                }
            }

            // create a semaphore to wait on 
            final Semaphore semaphore = new Semaphore(0);

            LOG.info("Scheduling Async Event");
            // now we need to schedule an async call to main thread to update data structures safely ... 
            _eventLoop.setTimer(new Timer(0, false, new Timer.Callback() {

                @Override
                public void timerFired(Timer timer) {
                    LOG.info("Cleaning Map");

                    synchronized (CacheManager.this) {
                        // walk tuples 
                        for (FingerprintAndOffsetTuple tuple : flushedTupleList) {
                            //TODO: HACK!
                            // remove from collection ... 
                            _fingerprintToLocalLogPos.removeAll(tuple._fingerprint);
                        }
                    }
                    LOG.info("Increment Offset Info");
                    // finally increment locallog offset by bytes removed ... 
                    _localLogStartOffset += bytesToRemove;

                    LOG.info("Releasing Wait Semaphore");
                    //release wait sempahore 
                    semaphore.release();
                }
            }));

            LOG.info("Waiting for Async Event to Complete");
            //wait for async operation to complete ...
            semaphore.acquireUninterruptibly();

            LOG.info("Async Event to Completed");
        } catch (IOException e) {
            LOG.error("Checkpoint Failed with Exception:" + CCStringUtils.stringifyException(e));
            // delete new file ... 
            activeLogFilePath.delete();
            // and rename checkpoint file to active file ... 
            checkpointLogFilePath.renameTo(activeLogFilePath);
        }
    } finally {
        LOG.info("Releasing ALL Log Access Semaphores. HELD FOR:" + (System.currentTimeMillis() - timeStart));
        getLocalLogAccessSemaphore().release(LOG_ACCESS_SEMAPHORE_COUNT);
    }
}