Skip to content
terma edited this page Sep 4, 2012 · 9 revisions

Scope

Java TCP proxy based on NIO. Support standalone run and embedded mode.

License

Apache License Version 2.0, January 2004 http://www.apache.org/licenses/

How to use

From command line

Info: TCP Proxy doesn't need any external jars only JRE =)

Create configuration file proxy.config:

# example config for tcp proxy
proxy1.localPort = 8000
proxy1.remoteHost = my.server
proxy1.remotePort = 80

Run proxy with configuration file in parameter:

java -jar javaniotcpproxy-1.2-SNAPSHOT.jar proxy.config

Note: Current implementation tries to use all available processors, so if you setup 2 proxy and your machine has 4 cores, proxy will start 2 workers per instance for avoid context switching. In any cases if you have only 2 cores and setup 4 instances proxy will start 1 worker per instance.

Run more one proxy from command line

Just add settings for new proxy to your config file:

# first instance
proxy1.localPort = 8000
proxy1.remoteHost = my.server
proxy1.remotePort = 80

# second instance
proxy2.localPort = 9000
proxy2.remoteHost = second.server
proxy2.remotePort = 8080

From code

Add to your pom, maven artifact:

Repository: http://oss.sonatype.org

<dependency>
    <groupId>com.github.terma</groupId>
    <artifactId>javaniotcpproxy</artifactId>
    <version>1.1</version>
</dependency>

Create and configure proxy:

// create config for proxy
TcpProxyConfig config = new TcpProxyConfig(localPort, remoteHost, remotePort);
config.setWorkerCount(workerCount);

// init proxy
TcpProxy proxy = new TcpProxy(config);

// start proxy
proxy.start();

// stop proxy
proxy.shutdown();

Start is not blocking method, it just start worker thread which will listening server socket and process incoming connections.