Skip to content

Commit 963efd5

Browse files
Extract method
Per discussion with Simon.
1 parent a129316 commit 963efd5

File tree

3 files changed

+24
-15
lines changed

3 files changed

+24
-15
lines changed

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

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -281,11 +281,8 @@ public void start()
281281

282282
// start the main loop going
283283
MainLoop loop = new MainLoop();
284-
final String threadName = "AMQP Connection " + getHostAddress() + ":" + getPort();
285-
mainLoopThread = threadFactory.newThread(loop);
286-
if(Environment.isAllowedToModifyThreads()) {
287-
mainLoopThread.setName(threadName);
288-
}
284+
final String name = "AMQP Connection " + getHostAddress() + ":" + getPort();
285+
mainLoopThread = Environment.newThread(threadFactory, loop, name);
289286
mainLoopThread.start();
290287
// after this point clear-up of MainLoop is triggered by closing the frameHandler.
291288

@@ -669,11 +666,9 @@ public void handleConnectionClose(Command closeCommand) {
669666
} catch (IOException _) { } // ignore
670667
_brokerInitiatedShutdown = true;
671668
SocketCloseWait scw = new SocketCloseWait(sse);
672-
Thread waiter = threadFactory.newThread(scw);
673-
if(Environment.isAllowedToModifyThreads()) {
674-
waiter.setName("AMQP Connection Closing Monitor " +
675-
getHostAddress() + ":" + getPort());
676-
}
669+
final String name = "AMQP Connection Closing Monitor " +
670+
getHostAddress() + ":" + getPort();
671+
Thread waiter = Environment.newThread(threadFactory, scw, name);
677672
waiter.start();
678673
}
679674

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

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -113,11 +113,7 @@ public void run() {
113113
ssWorkService.shutdown();
114114
}
115115
};
116-
Thread shutdownThread = threadFactory.newThread(target);
117-
if(Environment.isAllowedToModifyThreads()) {
118-
shutdownThread.setName("ConsumerWorkService shutdown monitor");
119-
shutdownThread.setDaemon(true);
120-
}
116+
Thread shutdownThread = Environment.newThread(threadFactory, target, "ConsumerWorkService shutdown monitor", true);
121117
shutdownThread.start();
122118
}
123119

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.rabbitmq.client.impl;
22

3+
import java.util.concurrent.ThreadFactory;
4+
35
/**
46
* Infers information about the execution environment, e.g.
57
* security permissions.
@@ -15,4 +17,20 @@ public static boolean isAllowedToModifyThreads() {
1517
return false;
1618
}
1719
}
20+
21+
public static Thread newThread(ThreadFactory factory, Runnable runnable, String name) {
22+
Thread t = factory.newThread(runnable);
23+
if(isAllowedToModifyThreads()) {
24+
t.setName(name);
25+
}
26+
return t;
27+
}
28+
29+
public static Thread newThread(ThreadFactory factory, Runnable runnable, String name, boolean isDaemon) {
30+
Thread t = newThread(factory, runnable, name);
31+
if(isAllowedToModifyThreads()) {
32+
t.setDaemon(isDaemon);
33+
}
34+
return t;
35+
}
1836
}

0 commit comments

Comments
 (0)