Skip to content

Commit 129ac77

Browse files
Create MainLoop thread through a thread factory
Environments such as GAE can provide their own factories that use, say, custom restricted thread managers.
1 parent c3dfc41 commit 129ac77

File tree

3 files changed

+51
-1
lines changed

3 files changed

+51
-1
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.rabbitmq.client;
2+
3+
/**
4+
* Extends {@link java.util.concurrent.ThreadFactory} to make it possible to specify
5+
* thread name.
6+
*
7+
* In environments with restricted thread management (e.g. Google App Engine), developers
8+
* can provide a custom factory to control how network I/O thread is created.
9+
*/
10+
public interface ThreadFactory extends java.util.concurrent.ThreadFactory {
11+
/**
12+
* Like {@link java.util.concurrent.ThreadFactory#newThread(Runnable)} but also takes
13+
* a thread name.
14+
*
15+
* @param r runnable to execute
16+
* @param threadName thread name
17+
* @return a new thread
18+
*/
19+
Thread newThread(Runnable r, String threadName);
20+
}

src/com/rabbitmq/client/impl/AMQConnection.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
import com.rabbitmq.client.SaslConfig;
4646
import com.rabbitmq.client.SaslMechanism;
4747
import com.rabbitmq.client.ShutdownSignalException;
48+
import com.rabbitmq.client.ThreadFactory;
4849
import com.rabbitmq.client.impl.AMQChannel.BlockingRpcContinuation;
4950
import com.rabbitmq.utility.BlockingCell;
5051

@@ -63,6 +64,7 @@ public class AMQConnection extends ShutdownNotifierComponent implements Connecti
6364
/** Timeout used while waiting for AMQP handshaking to complete (milliseconds) */
6465
public static final int HANDSHAKE_TIMEOUT = 10000;
6566
private Thread mainLoopThread;
67+
private ThreadFactory threadFactory = new DefaultThreadFactory();
6668

6769
/**
6870
* Retrieve a copy of the default table of client properties that
@@ -313,7 +315,7 @@ public void start()
313315

314316
// start the main loop going
315317
MainLoop loop = new MainLoop();
316-
mainLoopThread = new Thread(loop, "AMQP Connection " + getHostAddress() + ":" + getPort());
318+
mainLoopThread = threadFactory.newThread(loop, "AMQP Connection " + getHostAddress() + ":" + getPort());
317319
mainLoopThread.start();
318320
// after this point clear-up of MainLoop is triggered by closing the frameHandler.
319321

@@ -473,6 +475,16 @@ public void setHeartbeat(int heartbeat) {
473475
}
474476
}
475477

478+
/**
479+
* Makes it possible to override thread factory that is used
480+
* to instantiate connection network I/O loop. Only necessary
481+
* in the environments with restricted
482+
* @param threadFactory
483+
*/
484+
public void setThreadFactory(ThreadFactory threadFactory) {
485+
this.threadFactory = threadFactory;
486+
}
487+
476488
public Map<String, Object> getClientProperties() {
477489
return new HashMap<String, Object>(_clientProperties);
478490
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.rabbitmq.client.impl;
2+
3+
import com.rabbitmq.client.ThreadFactory;
4+
5+
/**
6+
* Default thread factory that instantiates {@link java.lang.Thread}s directly.
7+
*/
8+
public class DefaultThreadFactory implements ThreadFactory {
9+
@Override
10+
public Thread newThread(Runnable r, String threadName) {
11+
return new Thread(r, threadName);
12+
}
13+
14+
@Override
15+
public Thread newThread(Runnable r) {
16+
return new Thread(r);
17+
}
18+
}

0 commit comments

Comments
 (0)