Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Increase Android Lock Timeout to over 60 seconds #181

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -148,11 +148,13 @@ public GENAEventProcessor getGenaEventProcessor() {
}

public StreamClient createStreamClient() {
return new StreamClientImpl(
new StreamClientConfigurationImpl(
StreamClientConfigurationImpl configuration = new StreamClientConfigurationImpl(
getSyncProtocolExecutorService()
)
);

configuration.setTimeoutSeconds((int) (getThreadTimeoutInMilliseconds() / 1000));

return new StreamClientImpl(configuration);
}

public MulticastReceiver createMulticastReceiver(NetworkAddressFactory networkAddressFactory) {
Expand Down Expand Up @@ -257,6 +259,15 @@ public NetworkAddressFactory createNetworkAddressFactory() {
return createNetworkAddressFactory(streamListenPort);
}

public long getThreadTimeoutInMilliseconds() {
try {
return Long.parseLong(System.getProperty("cling.thread_timeout", "60000"));
}
catch (NumberFormatException e) {
return 60000L;
}
}

public void shutdown() {
log.fine("Shutting down default executor service");
getDefaultExecutorService().shutdownNow();
Expand Down Expand Up @@ -295,12 +306,12 @@ protected ExecutorService getDefaultExecutorService() {
}

protected ExecutorService createDefaultExecutorService() {
return new ClingExecutor();
return new ClingExecutor(getThreadTimeoutInMilliseconds());
}

public static class ClingExecutor extends ThreadPoolExecutor {

public ClingExecutor() {
public ClingExecutor(long timeoutInMilliseconds) {
this(new ClingThreadFactory(),
new ThreadPoolExecutor.DiscardPolicy() {
// The pool is unbounded but rejections will happen during shutdown
Expand All @@ -310,16 +321,17 @@ public void rejectedExecution(Runnable runnable, ThreadPoolExecutor threadPoolEx
log.info("Thread pool rejected execution of " + runnable.getClass());
super.rejectedExecution(runnable, threadPoolExecutor);
}
}
},
timeoutInMilliseconds
);
}

public ClingExecutor(ThreadFactory threadFactory, RejectedExecutionHandler rejectedHandler) {
public ClingExecutor(ThreadFactory threadFactory, RejectedExecutionHandler rejectedHandler, long timeoutInMilliseconds) {
// This is the same as Executors.newCachedThreadPool
super(0,
Integer.MAX_VALUE,
60L,
TimeUnit.SECONDS,
timeoutInMilliseconds,
TimeUnit.MILLISECONDS,
new SynchronousQueue<Runnable>(),
threadFactory,
rejectedHandler
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,16 @@ public Executor getRegistryListenerExecutor() {
return getDefaultExecutorService();
}

@Override
public long getThreadTimeoutInMilliseconds() {
try {
return Long.parseLong(System.getProperty("cling.thread_timeout", "60000"));
}
catch (NumberFormatException e) {
return 60000L;
}
}

public NetworkAddressFactory createNetworkAddressFactory() {
return createNetworkAddressFactory(streamListenPort);
}
Expand Down Expand Up @@ -255,6 +265,6 @@ protected ExecutorService getDefaultExecutorService() {
}

protected ExecutorService createDefaultExecutorService() {
return new DefaultUpnpServiceConfiguration.ClingExecutor();
return new DefaultUpnpServiceConfiguration.ClingExecutor(getThreadTimeoutInMilliseconds());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,11 @@ public interface UpnpServiceConfiguration {
*/
public Executor getRegistryListenerExecutor();

/**
* @return The timeout for thread connections
*/
public long getThreadTimeoutInMilliseconds();

/**
* Called by the {@link org.fourthline.cling.UpnpService} on shutdown, useful to e.g. shutdown thread pools.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ protected BroadcastReceiver createConnectivityBroadcastReceiver() {

@Override
protected int getLockTimeoutMillis() {
return 15000;
return (int) (getConfiguration().getThreadTimeoutInMilliseconds() + 5000);
}

@Override
Expand Down