use of java.util.concurrent.Callable in project graylog2-server by Graylog2.
the class AbstractTcpTransportTest method getBaseChannelHandlersGeneratesSelfSignedCertificates.
@Test
public void getBaseChannelHandlersGeneratesSelfSignedCertificates() {
final Configuration configuration = new Configuration(ImmutableMap.of("bind_address", "localhost", "port", 12345, "tls_enable", true));
final AbstractTcpTransport transport = new AbstractTcpTransport(configuration, throughputCounter, localRegistry, bossPool, workerPool, connectionCounter) {
@Override
protected Bootstrap getBootstrap() {
return super.getBootstrap();
}
@Override
protected LinkedHashMap<String, Callable<? extends ChannelHandler>> getBaseChannelHandlers(MessageInput input) {
return super.getBaseChannelHandlers(input);
}
};
final MessageInput input = mock(MessageInput.class);
assertThat(transport.getBaseChannelHandlers(input)).containsKey("tls");
}
use of java.util.concurrent.Callable in project graylog2-server by Graylog2.
the class AbstractTcpTransportTest method getBaseChannelHandlersFailsIfTempDirIsNoDirectory.
@Test
public void getBaseChannelHandlersFailsIfTempDirIsNoDirectory() throws IOException {
final File file = temporaryFolder.newFile();
assumeTrue(file.isFile());
System.setProperty("java.io.tmpdir", file.getAbsolutePath());
final Configuration configuration = new Configuration(ImmutableMap.of("bind_address", "localhost", "port", 12345, "tls_enable", true));
final AbstractTcpTransport transport = new AbstractTcpTransport(configuration, throughputCounter, localRegistry, bossPool, workerPool, connectionCounter) {
@Override
protected Bootstrap getBootstrap() {
return super.getBootstrap();
}
@Override
protected LinkedHashMap<String, Callable<? extends ChannelHandler>> getBaseChannelHandlers(MessageInput input) {
return super.getBaseChannelHandlers(input);
}
};
expectedException.expect(IllegalStateException.class);
expectedException.expectMessage("Couldn't write to temporary directory: " + file.getAbsolutePath());
transport.getBaseChannelHandlers(input);
}
use of java.util.concurrent.Callable in project alluxio by Alluxio.
the class FileSystemMaster method startupCheckConsistency.
/**
* Checks the consistency of the root in a multi-threaded and incremental fashion. This method
* will only READ lock the directories and files actively being checked and release them after the
* check on the file / directory is complete.
*
* @return a list of paths in Alluxio which are not consistent with the under storage
* @throws InterruptedException if the thread is interrupted during execution
* @throws IOException if an error occurs interacting with the under storage
*/
private List<AlluxioURI> startupCheckConsistency(final ExecutorService service) throws InterruptedException, IOException {
/** A marker {@link StartupConsistencyChecker}s add to the queue to signal completion */
final long completionMarker = -1;
/** A shared queue of directories which have yet to be checked */
final BlockingQueue<Long> dirsToCheck = new LinkedBlockingQueue<>();
/**
* A {@link Callable} which checks the consistency of a directory.
*/
final class StartupConsistencyChecker implements Callable<List<AlluxioURI>> {
/** The path to check, guaranteed to be a directory in Alluxio. */
private final Long mFileId;
/**
* Creates a new callable which checks the consistency of a directory.
* @param fileId the path to check
*/
private StartupConsistencyChecker(Long fileId) {
mFileId = fileId;
}
/**
* Checks the consistency of the directory and all immediate children which are files. All
* immediate children which are directories are added to the shared queue of directories to
* check. The parent directory is READ locked during the entire call while the children are
* READ locked only during the consistency check of the children files.
*
* @return a list of inconsistent uris
* @throws IOException if an error occurs interacting with the under storage
*/
@Override
public List<AlluxioURI> call() throws IOException {
List<AlluxioURI> inconsistentUris = new ArrayList<>();
try (LockedInodePath dir = mInodeTree.lockFullInodePath(mFileId, InodeTree.LockMode.READ)) {
Inode parentInode = dir.getInode();
AlluxioURI parentUri = dir.getUri();
if (!checkConsistencyInternal(parentInode, parentUri)) {
inconsistentUris.add(parentUri);
}
for (Inode childInode : ((InodeDirectory) parentInode).getChildren()) {
try {
childInode.lockReadAndCheckParent(parentInode);
} catch (InvalidPathException e) {
// This should be safe, continue.
LOG.debug("Error during startup check consistency, ignoring and continuing.", e);
continue;
}
try {
AlluxioURI childUri = parentUri.join(childInode.getName());
if (childInode.isDirectory()) {
dirsToCheck.add(childInode.getId());
} else {
if (!checkConsistencyInternal(childInode, childUri)) {
inconsistentUris.add(childUri);
}
}
} finally {
childInode.unlockRead();
}
}
} catch (FileDoesNotExistException e) {
// This should be safe, continue.
LOG.debug("A file scheduled for consistency check was deleted before the check.");
} catch (InvalidPathException e) {
// This should not happen.
LOG.error("An invalid path was discovered during the consistency check, skipping.", e);
}
dirsToCheck.add(completionMarker);
return inconsistentUris;
}
}
// Add the root to the directories to check.
dirsToCheck.add(mInodeTree.getRoot().getId());
List<Future<List<AlluxioURI>>> results = new ArrayList<>();
// Tracks how many checkers have been started.
long started = 0;
// Tracks how many checkers have completed.
long completed = 0;
do {
Long fileId = dirsToCheck.take();
if (fileId == completionMarker) {
// A thread signaled completion.
completed++;
} else {
// A new directory needs to be checked.
StartupConsistencyChecker checker = new StartupConsistencyChecker(fileId);
results.add(service.submit(checker));
started++;
}
} while (started != completed);
// Return the total set of inconsistent paths discovered.
List<AlluxioURI> inconsistentUris = new ArrayList<>();
for (Future<List<AlluxioURI>> result : results) {
try {
inconsistentUris.addAll(result.get());
} catch (Exception e) {
// This shouldn't happen, all futures should be complete.
Throwables.propagate(e);
}
}
service.shutdown();
return inconsistentUris;
}
use of java.util.concurrent.Callable in project nhin-d by DirectProject.
the class NHINDSecurityAndTrustMailet_functionalTest method testProcessOutgoingMessageEndToEnd_multipleProcessThreads.
public void testProcessOutgoingMessageEndToEnd_multipleProcessThreads() throws Exception {
new TestPlan() {
protected String getMessageToProcess() throws Exception {
return TestUtils.readMessageResource("PlainOutgoingMessage.txt");
}
final class MessageEncrypter implements Callable<MimeMessage> {
private final Mailet theMailet;
public MessageEncrypter(Mailet theMailet) {
this.theMailet = theMailet;
}
public MimeMessage call() throws Exception {
// encrypt
String originalMessage = getMessageToProcess();
MimeMessage msg = EntitySerializer.Default.deserialize(originalMessage);
// add an MDN request
msg.setHeader(MDNStandard.Headers.DispositionNotificationTo, msg.getHeader(MailStandard.Headers.From, ","));
MockMail theMessage = new MockMail(msg);
theMailet.service(theMessage);
assertNotNull(theMessage);
assertNotNull(theMessage.getMessage());
msg = theMessage.getMessage();
return msg;
}
}
protected void performInner() throws Exception {
ExecutorService execService = Executors.newFixedThreadPool(10);
String originalMessage = getMessageToProcess();
// execute 100 times
GatewayState.getInstance().setSettingsUpdateInterval(1);
Mailet theMailet = getMailet("ValidConfig.xml");
List<Future<MimeMessage>> futures = new ArrayList<Future<MimeMessage>>();
for (int i = 0; i < 100; ++i) {
futures.add(execService.submit(new MessageEncrypter(theMailet)));
}
assertEquals(100, futures.size());
GatewayState.getInstance().setSettingsUpdateInterval(300);
for (Future<MimeMessage> future : futures) {
// decrypt
theMailet = getMailet("ValidConfigStateLine.txt");
MockMail theMessage = new MockMail(future.get());
theMailet.service(theMessage);
assertNotNull(theMessage);
assertNotNull(theMessage.getMessage());
MimeMessage msg = theMessage.getMessage();
assertFalse(SMIMEStandard.isEncrypted(msg));
assertEquals(theMessage.getState(), Mail.TRANSPORT);
Message compareMessage = new Message(theMessage.getMessage());
// remove the MDN before comparison
compareMessage.removeHeader(MDNStandard.Headers.DispositionNotificationTo);
assertEquals(originalMessage, compareMessage.toString());
}
}
}.perform();
}
use of java.util.concurrent.Callable in project opennms by OpenNMS.
the class Jsr160ConnectionFactory method getMBeanServerConnection.
public static JmxServerConnectionWrapper getMBeanServerConnection(Map<String, String> propertiesMap, InetAddress address) throws IOException {
final long timeout = DEFAULT_TIMEOUT;
propertiesMap.putIfAbsent("factory", "STANDARD");
propertiesMap.putIfAbsent("port", "1099");
propertiesMap.putIfAbsent("protocol", "rmi");
propertiesMap.putIfAbsent("urlPath", "/jmxrmi");
propertiesMap.putIfAbsent("timeout", Long.toString(timeout));
final Callable<JmxServerConnectionWrapper> task = new Callable<JmxServerConnectionWrapper>() {
@Override
public JmxServerConnectionWrapper call() throws Exception {
return new DefaultJmxConnector().createConnection(address, propertiesMap);
}
};
final Future<JmxServerConnectionWrapper> future = executor.submit(task);
try {
final JmxServerConnectionWrapper connectionWrapper = future.get(timeout, TimeUnit.MILLISECONDS);
return connectionWrapper;
} catch (InterruptedException | ExecutionException | TimeoutException e) {
final String url = JmxConnectionConfigBuilder.buildFrom(address, propertiesMap).build().getUrl();
LOG.info("Exception connecting JMXConnectorFactory url {} , Error: {}", url, e.getMessage());
if (!future.isDone()) {
future.cancel(true);
LOG.info(" the task {}", future.isCancelled() ? "was cancelled" : "could not be cancelled");
}
throw new ConnectException("Error connecting JMXConnectionFactory " + url);
}
}
Aggregations