use of javax.jmdns.impl.util.NamedThreadFactory in project EngineDriver by JMRI.
the class JmmDNSImpl method list.
/*
* (non-Javadoc)
* @see javax.jmdns.JmmDNS#list(java.lang.String, long)
*/
@Override
public ServiceInfo[] list(final String type, final long timeout) {
final JmDNS[] dnsArray = this.getDNS();
// We need to run this in parallel to respect the timeout.
final Set<ServiceInfo> result = new HashSet<ServiceInfo>(dnsArray.length * 5);
if (dnsArray.length > 0) {
List<Callable<List<ServiceInfo>>> tasks = new ArrayList<Callable<List<ServiceInfo>>>(dnsArray.length);
for (final JmDNS mDNS : dnsArray) {
tasks.add(new Callable<List<ServiceInfo>>() {
@Override
public List<ServiceInfo> call() throws Exception {
return Arrays.asList(mDNS.list(type, timeout));
}
});
}
ExecutorService executor = Executors.newFixedThreadPool(tasks.size(), new NamedThreadFactory("JmmDNS.list"));
try {
List<Future<List<ServiceInfo>>> results = Collections.emptyList();
try {
results = executor.invokeAll(tasks, timeout + 100, TimeUnit.MILLISECONDS);
} catch (InterruptedException exception) {
logger.log(Level.FINE, "Interrupted ", exception);
Thread.currentThread().interrupt();
// Will terminate next loop early.
}
for (Future<List<ServiceInfo>> future : results) {
if (future.isCancelled()) {
continue;
}
try {
result.addAll(future.get());
} catch (InterruptedException exception) {
logger.log(Level.FINE, "Interrupted ", exception);
Thread.currentThread().interrupt();
} catch (ExecutionException exception) {
logger.log(Level.WARNING, "Exception ", exception);
}
}
} finally {
executor.shutdown();
}
}
return result.toArray(new ServiceInfo[result.size()]);
}
use of javax.jmdns.impl.util.NamedThreadFactory in project EngineDriver by JMRI.
the class JmmDNSImpl method close.
/*
* (non-Javadoc)
* @see java.io.Closeable#close()
*/
@Override
public void close() throws IOException {
if (_isClosing.compareAndSet(false, true)) {
if (logger.isLoggable(Level.FINER)) {
logger.finer("Cancelling JmmDNS: " + this);
}
_timer.cancel();
_listenerExecutor.shutdown();
_jmDNSExecutor.shutdown();
// We need to cancel all the DNS
ExecutorService executor = Executors.newCachedThreadPool(new NamedThreadFactory("JmmDNS.close"));
try {
for (final JmDNS mDNS : this.getDNS()) {
executor.submit(new Runnable() {
/**
* {@inheritDoc}
*/
@Override
public void run() {
try {
mDNS.close();
} catch (IOException exception) {
// JmDNS never throws this is only because of the closeable interface
}
}
});
}
} finally {
executor.shutdown();
}
try {
executor.awaitTermination(DNSConstants.CLOSE_TIMEOUT, TimeUnit.MILLISECONDS);
} catch (InterruptedException exception) {
logger.log(Level.WARNING, "Exception ", exception);
}
_knownMDNS.clear();
_services.clear();
_serviceListeners.clear();
_typeListeners.clear();
_serviceTypes.clear();
_closed.set(true);
JmmDNS.Factory.close();
}
}
use of javax.jmdns.impl.util.NamedThreadFactory in project EngineDriver by JMRI.
the class JmmDNSImpl method getServiceInfos.
/*
* (non-Javadoc)
* @see javax.jmdns.JmmDNS#getServiceInfos(java.lang.String, java.lang.String, boolean, long)
*/
@Override
public ServiceInfo[] getServiceInfos(final String type, final String name, final boolean persistent, final long timeout) {
// We need to run this in parallel to respect the timeout.
final JmDNS[] dnsArray = this.getDNS();
final Set<ServiceInfo> result = new HashSet<ServiceInfo>(dnsArray.length);
if (dnsArray.length > 0) {
List<Callable<ServiceInfo>> tasks = new ArrayList<Callable<ServiceInfo>>(dnsArray.length);
for (final JmDNS mDNS : dnsArray) {
tasks.add(new Callable<ServiceInfo>() {
@Override
public ServiceInfo call() throws Exception {
return mDNS.getServiceInfo(type, name, persistent, timeout);
}
});
}
ExecutorService executor = Executors.newFixedThreadPool(tasks.size(), new NamedThreadFactory("JmmDNS.getServiceInfos"));
try {
List<Future<ServiceInfo>> results = Collections.emptyList();
try {
results = executor.invokeAll(tasks, timeout + 100, TimeUnit.MILLISECONDS);
} catch (InterruptedException exception) {
logger.log(Level.FINE, "Interrupted ", exception);
Thread.currentThread().interrupt();
// Will terminate next loop early.
}
for (Future<ServiceInfo> future : results) {
if (future.isCancelled()) {
continue;
}
try {
ServiceInfo info = future.get();
if (info != null) {
result.add(info);
}
} catch (InterruptedException exception) {
logger.log(Level.FINE, "Interrupted ", exception);
Thread.currentThread().interrupt();
} catch (ExecutionException exception) {
logger.log(Level.WARNING, "Exception ", exception);
}
}
} finally {
executor.shutdown();
}
}
return result.toArray(new ServiceInfo[result.size()]);
}
Aggregations