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

Support custom ThreadFactory. #793

Open
wants to merge 6 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
2 changes: 1 addition & 1 deletion src/main/java/com/jcraft/jsch/ChannelDirectTCPIP.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public void connect(int connectTimeout) throws JSchException {
}

if (io.in != null) {
thread = new Thread(this::run);
thread = _session.getThreadFactory().newThread(this::run);
thread.setName("DirectTCPIP thread " + _session.getHost());
if (_session.daemon_thread) {
thread.setDaemon(_session.daemon_thread);
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/jcraft/jsch/ChannelExec.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public void start() throws JSchException {
}

if (io.in != null) {
thread = new Thread(this::run);
thread = _session.getThreadFactory().newThread(this::run);
thread.setName("Exec thread " + _session.getHost());
if (_session.daemon_thread) {
thread.setDaemon(_session.daemon_thread);
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/jcraft/jsch/ChannelForwardedTCPIP.java
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public void run() {

daemon.setChannel(this, getInputStream(), out);
daemon.setArg(_config.arg);
new Thread(daemon).start();
getSession().getThreadFactory().newThread(daemon).start();
} else {
ConfigLHost _config = (ConfigLHost) config;
socket =
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/jcraft/jsch/ChannelShell.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public void start() throws JSchException {
}

if (io.in != null) {
thread = new Thread(this::run);
thread = _session.getThreadFactory().newThread(this::run);
thread.setName("Shell for " + _session.host);
if (_session.daemon_thread) {
thread.setDaemon(_session.daemon_thread);
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/jcraft/jsch/ChannelSubsystem.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public void start() throws JSchException {
throw new JSchException("ChannelSubsystem", e);
}
if (io.in != null) {
thread = new Thread(this::run);
thread = _session.getThreadFactory().newThread(this::run);
thread.setName("Subsystem for " + _session.host);
if (_session.daemon_thread) {
thread.setDaemon(_session.daemon_thread);
Expand Down
33 changes: 29 additions & 4 deletions src/main/java/com/jcraft/jsch/Session.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,10 @@
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import java.util.Objects;
import java.util.Properties;
import java.util.Vector;
import java.util.concurrent.ThreadFactory;
import javax.crypto.AEADBadTagException;

public class Session {
Expand Down Expand Up @@ -174,6 +176,8 @@ public class Session {
JSch jsch;
Logger logger;

private ThreadFactory threadFactory = Thread::new;

Session(JSch jsch, String username, String host, int port) throws JSchException {
super();
this.jsch = jsch;
Expand Down Expand Up @@ -528,7 +532,7 @@ public void connect(int connectTimeout) throws JSchException {

synchronized (lock) {
if (isConnected) {
connectThread = new Thread(this::run);
connectThread = getThreadFactory().newThread(this::run);
connectThread.setName("Connect thread " + host + " session");
if (daemon_thread) {
connectThread.setDaemon(daemon_thread);
Expand Down Expand Up @@ -2005,7 +2009,7 @@ void run() {
channel.getData(buf);
channel.init();

Thread tmp = new Thread(channel::run);
Thread tmp = getThreadFactory().newThread(channel::run);
tmp.setName("Channel " + ctyp + " " + host);
if (daemon_thread) {
tmp.setDaemon(daemon_thread);
Expand Down Expand Up @@ -2156,6 +2160,27 @@ public void disconnect() {
// System.gc();
}

/**
* Sets a thread factory to be used for creating new threads in this instance.
*
* @param threadFactory The thread factory to be used; must not be <code>null</code>
* @throws NullPointerException if the provided thread factory is <code>null</code>
*/
public void setThreadFactory(ThreadFactory threadFactory) {
this.threadFactory = Objects.requireNonNull(threadFactory);
}


/**
* Returns the thread factory used by this instance.
*
* @return The thread factory associated with this instance. If no specific thread factory has
* been set, a default thread factory is returned.
*/
public ThreadFactory getThreadFactory() {
return threadFactory;
}

/**
* Registers the local port forwarding for loop-back interface. If <code>lport</code> is
* <code>0</code>, the tcp port will be allocated.
Expand Down Expand Up @@ -2230,7 +2255,7 @@ public int setPortForwardingL(String bind_address, int lport, String host, int r
ServerSocketFactory ssf, int connectTimeout) throws JSchException {
PortWatcher pw = PortWatcher.addPort(this, bind_address, lport, host, rport, ssf);
pw.setConnectTimeout(connectTimeout);
Thread tmp = new Thread(pw::run);
Thread tmp = getThreadFactory().newThread(pw::run);
tmp.setName("PortWatcher Thread for " + host);
if (daemon_thread) {
tmp.setDaemon(daemon_thread);
Expand All @@ -2243,7 +2268,7 @@ public int setSocketForwardingL(String bindAddress, int lport, String socketPath
ServerSocketFactory ssf, int connectTimeout) throws JSchException {
PortWatcher pw = PortWatcher.addSocket(this, bindAddress, lport, socketPath, ssf);
pw.setConnectTimeout(connectTimeout);
Thread tmp = new Thread(pw::run);
Thread tmp = getThreadFactory().newThread(pw::run);
tmp.setName("PortWatcher Thread for " + host);
if (daemon_thread) {
tmp.setDaemon(daemon_thread);
Expand Down