Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #21520 > unrolled thread

high availability and failover for tcp/ip connections

Started byme 2 <winona_whitener@yahoo.com>
First post2013-01-18 08:00 -0800
Last post2013-05-23 12:43 -0700
Articles 5 — 5 participants

Back to article view | Back to comp.lang.java.programmer


Contents

  high availability and failover for tcp/ip connections me 2 <winona_whitener@yahoo.com> - 2013-01-18 08:00 -0800
    Re: high availability and failover for tcp/ip connections Arne Vajhøj <arne@vajhoej.dk> - 2013-05-18 19:02 -0400
      Re: high availability and failover for tcp/ip connections Joerg Meier <joergmmeier@arcor.de> - 2013-05-19 11:59 +0200
    Re: high availability and failover for tcp/ip connections Patrick May <patrick@softwarematters.org> - 2013-05-22 13:51 -0400
    Re: high availability and failover for tcp/ip connections bob smith <bob@coolfone.comze.com> - 2013-05-23 12:43 -0700

#21520 — high availability and failover for tcp/ip connections

Fromme 2 <winona_whitener@yahoo.com>
Date2013-01-18 08:00 -0800
Subjecthigh availability and failover for tcp/ip connections
Message-ID<21bb7da2-cad6-4c3c-aba9-fa7abb81e754@googlegroups.com>
Greetings,

I was wondering if anyone knew of a library that could help me with some socket programming.  Ideally, I'd be able to provide my main program with a socket connection that would be able to go through a list of potential targets for connection and then, if the socket died unexpectedly, try to reach the next potential target.  It has to ultimately use a TCP socket and no I can't use a JMS server product--but maybe a single part of the library.  I saw something similar to what I want for c# at http://www.codeproject.com/Articles/20106/Failover-Socket-Client and I thought that I'd ask around and see if there was some cool library that I just didn't know about.

Cheers,
Me

[toc] | [next] | [standalone]


#24133

FromArne Vajhøj <arne@vajhoej.dk>
Date2013-05-18 19:02 -0400
Message-ID<51980897$0$32107$14726298@news.sunsite.dk>
In reply to#21520
On 1/18/2013 11:00 AM, me 2 wrote:
> I was wondering if anyone knew of a library that could help me with
> some socket programming.  Ideally, I'd be able to provide my main
> program with a socket connection that would be able to go through a
> list of potential targets for connection and then, if the socket died
> unexpectedly, try to reach the next potential target.  It has to
> ultimately use a TCP socket and no I can't use a JMS server
> product--but maybe a single part of the library.  I saw something
> similar to what I want for c# at
> http://www.codeproject.com/Articles/20106/Failover-Socket-Client and
> I thought that I'd ask around and see if there was some cool library
> that I just didn't know about.

It is a actually a tricky problem and I certainly do not like
the approach in the link above.

Below is some half finished (or 1/3 finished) code, that outlines
how I would attack the problem.

Note that making this code robust will certainly take an effort, but
I like the almost transparent usage model (even though the
implementation does violate some OO recommendations).

Example of client code:

List<MultiSocket.Destination> dest = new 
ArrayList<MultiSocket.Destination>();
dest.add(new MultiSocket.Destination("localhost", 12345));
dest.add(new MultiSocket.Destination("localhost", 12346));
Socket s = new MultiSocket(dest);
OutputStream os = s.getOutputStream();
InputStream is = s.getInputStream();
int b;
os.write(1);
b = is.read();

Arne

====

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.Socket;
import java.net.SocketAddress;
import java.net.SocketException;
import java.nio.channels.SocketChannel;
import java.util.ArrayList;
import java.util.List;

public class MultiSocket extends Socket {
	public static class Destination {
		private String host;
		private int port;
		public Destination(String host, int port) {
			this.host = host;
			this.port = port;
		}
		public String getHost() {
			return host;
		}
		public int getPort() {
			return port;
		}
	}
	private List<Socket> alls;
	private List<InputStream> allis;
	private List<OutputStream> allos;
	private int lastwrite;
	private byte[] buf;
	private int buflen;
	private class MultiInputStream extends InputStream {
		@Override
		public int read() throws IOException {
			if(lastwrite < 0) {
				throw new IllegalStateException("MultiSocket is only for 
request-response");
			}
			while(alls.size() > 0) {
				try {
					int b = allis.get(lastwrite).read();
					buflen = 0;
					return b;
				} catch (Exception e) {
					remove(lastwrite);
					allos.get(0).write(buf,  0,  buflen); // additional code is 
necessary to handle if this call throws an exception
					lastwrite = 0;
				}
			}
			throw new IOException("No working sockets");
		}
	}
	private class MultiOutputStream extends OutputStream {
		@Override
		public void write(int b) throws IOException {
			while(alls.size() > 0) {
				try {
					buf[buflen] = (byte)b;
					buflen++;
					allos.get(0).write(b);
					lastwrite = 0;
					return;
				} catch (Exception e) {
					remove(0);
					allos.get(0).write(buf,  0,  buflen); // additional code is 
necessary to handle if this call throws an exception
					lastwrite = 0;
				}
			}
			throw new IOException("No working sockets");
		}
	}
	private void remove(int ix) {
		OutputStream os = allos.remove(ix);
		try {
			os.close();
		} catch(Exception ex) {
			// nothing to do
		}
		InputStream is = allis.remove(ix);
		try {
			is.close();
		} catch(Exception ex) {
			// nothing to do
		}
		Socket s = alls.remove(ix);
		try {
			s.close();
		} catch(Exception ex) {
			// nothing to do
		}
	}
	public MultiSocket(List<Destination> dest) throws IOException {
		alls = new ArrayList<Socket>();
		for(Destination d : dest) {
			alls.add(new Socket(d.getHost(), d.getPort()));
		}
		lastwrite = -1;
		buf = new byte[102400];
		buflen = 0;
	}
	@Override
	public InputStream getInputStream() throws IOException {
		allis = new ArrayList<InputStream>();
		for(Socket s : alls) {
			allis.add(s.getInputStream());
		}
		return new MultiInputStream();
	}
	@Override
	public OutputStream getOutputStream() throws IOException {
		allos = new ArrayList<OutputStream>();
		for(Socket s : alls) {
			allos.add(s.getOutputStream());
		}
		return new MultiOutputStream();
	}
	@Override
	public synchronized void close() throws IOException {
		while(alls.size() > 0) {
			remove(0);
		}
	}
	// move exception throwing stubs up as they get an implementation that 
makes sense
	@Override
	public void connect(SocketAddress endpoint) throws IOException {
		throw new RuntimeException("Not supported by MultiSocket");
	}
	@Override
	public void connect(SocketAddress endpoint, int timeout) throws 
IOException {
		throw new RuntimeException("Not supported by MultiSocket");
	}
	@Override
	public void bind(SocketAddress bindpoint) throws IOException {
		throw new RuntimeException("Not supported by MultiSocket");
	}
	@Override
	public InetAddress getInetAddress() {
		throw new RuntimeException("Not supported by MultiSocket");
	}
	@Override
	public InetAddress getLocalAddress() {
		throw new RuntimeException("Not supported by MultiSocket");
	}
	@Override
	public int getPort() {
		throw new RuntimeException("Not supported by MultiSocket");
	}
	@Override
	public int getLocalPort() {
		throw new RuntimeException("Not supported by MultiSocket");
	}
	@Override
	public SocketAddress getRemoteSocketAddress() {
		throw new RuntimeException("Not supported by MultiSocket");
	}
	@Override
	public SocketAddress getLocalSocketAddress() {
		throw new RuntimeException("Not supported by MultiSocket");
	}
	@Override
	public SocketChannel getChannel() {
		throw new RuntimeException("Not supported by MultiSocket");
	}
	@Override
	public void setTcpNoDelay(boolean on) throws SocketException {
		throw new RuntimeException("Not supported by MultiSocket");
	}
	@Override
	public boolean getTcpNoDelay() throws SocketException {
		throw new RuntimeException("Not supported by MultiSocket");
	}
	@Override
	public void setSoLinger(boolean on, int linger) throws SocketException {
		throw new RuntimeException("Not supported by MultiSocket");
	}
	@Override
	public int getSoLinger() throws SocketException {
		throw new RuntimeException("Not supported by MultiSocket");
	}
	@Override
	public void sendUrgentData(int data) throws IOException {
		throw new RuntimeException("Not supported by MultiSocket");
	}
	@Override
	public void setOOBInline(boolean on) throws SocketException {
		throw new RuntimeException("Not supported by MultiSocket");
	}
	@Override
	public boolean getOOBInline() throws SocketException {
		throw new RuntimeException("Not supported by MultiSocket");
	}
	@Override
	public synchronized void setSoTimeout(int timeout) throws SocketException {
		throw new RuntimeException("Not supported by MultiSocket");
	}
	@Override
	public synchronized int getSoTimeout() throws SocketException {
		throw new RuntimeException("Not supported by MultiSocket");
	}
	@Override
	public synchronized void setSendBufferSize(int size) throws 
SocketException {
		throw new RuntimeException("Not supported by MultiSocket");
	}
	@Override
	public synchronized int getSendBufferSize() throws SocketException {
		throw new RuntimeException("Not supported by MultiSocket");
	}
	@Override
	public synchronized void setReceiveBufferSize(int size)
			throws SocketException {
		throw new RuntimeException("Not supported by MultiSocket");
	}
	@Override
	public synchronized int getReceiveBufferSize() throws SocketException {
		throw new RuntimeException("Not supported by MultiSocket");
	}
	@Override
	public void setKeepAlive(boolean on) throws SocketException {
		throw new RuntimeException("Not supported by MultiSocket");
	}
	@Override
	public boolean getKeepAlive() throws SocketException {
		throw new RuntimeException("Not supported by MultiSocket");
	}
	@Override
	public void setTrafficClass(int tc) throws SocketException {
		throw new RuntimeException("Not supported by MultiSocket");
	}
	@Override
	public int getTrafficClass() throws SocketException {
		throw new RuntimeException("Not supported by MultiSocket");
	}
	@Override
	public void setReuseAddress(boolean on) throws SocketException {
		throw new RuntimeException("Not supported by MultiSocket");
	}
	@Override
	public boolean getReuseAddress() throws SocketException {
		throw new RuntimeException("Not supported by MultiSocket");
	}
	@Override
	public void shutdownInput() throws IOException {
		throw new RuntimeException("Not supported by MultiSocket");
	}
	@Override
	public void shutdownOutput() throws IOException {
		throw new RuntimeException("Not supported by MultiSocket");
	}
	@Override
	public String toString() {
		throw new RuntimeException("Not supported by MultiSocket");
	}
	@Override
	public boolean isConnected() {
		throw new RuntimeException("Not supported by MultiSocket");
	}
	@Override
	public boolean isBound() {
		throw new RuntimeException("Not supported by MultiSocket");
	}
	@Override
	public boolean isClosed() {
		throw new RuntimeException("Not supported by MultiSocket");
	}
	@Override
	public boolean isInputShutdown() {
		throw new RuntimeException("Not supported by MultiSocket");
	}
	@Override
	public boolean isOutputShutdown() {
		throw new RuntimeException("Not supported by MultiSocket");
	}
	@Override
	public void setPerformancePreferences(int connectionTime, int latency, 
int bandwidth) {
		throw new RuntimeException("Not supported by MultiSocket");
	}
	@Override
	public int hashCode() {
		throw new RuntimeException("Not supported by MultiSocket");
	}
	@Override
	public boolean equals(Object obj) {
		throw new RuntimeException("Not supported by MultiSocket");
	}
	@Override
	protected Object clone() throws CloneNotSupportedException {
		throw new RuntimeException("Not supported by MultiSocket");
	}
	@Override
	protected void finalize() throws Throwable {
		throw new RuntimeException("Not supported by MultiSocket");
	}
}


[toc] | [prev] | [next] | [standalone]


#24137

FromJoerg Meier <joergmmeier@arcor.de>
Date2013-05-19 11:59 +0200
Message-ID<z7f7ry3swinu$.18vozcol6589n$.dlg@40tude.net>
In reply to#24133
On Sat, 18 May 2013 19:02:48 -0400, Arne Vajhøj wrote:

> 	@Override
> 	public synchronized int getReceiveBufferSize() throws SocketException {
> 		throw new RuntimeException("Not supported by MultiSocket");
> 	}
> 	@Override
> 	public void setKeepAlive(boolean on) throws SocketException {
> 		throw new RuntimeException("Not supported by MultiSocket");
> 	}
> 	@Override
> 	public boolean getKeepAlive() throws SocketException {
> 		throw new RuntimeException("Not supported by MultiSocket");
> 	}

[...several pages worth...]

You might want to look into Lombok(-pg):

-------------
@AutoGenMethodStub

With this annotation you can avoid cluttering your code with empty methods
that an interface forces you to implement. Just implement the ones you need
and lombok will create stubs for the rest.
-------------

For your use case:

-------------
@AutoGenMethodStub(throwException = true)
public class MultiSocket extends Socket { //...
-------------

Would then generate a bunch of methods looking like this, waiting for you
to only override the ones you use:

-------------
@Override
public int getTrafficClass() throws SocketException {
	throw new java.lang.UnsupportedOperationException("This method is not
implemented yet.");
}
-------------

Lombok is also a compile-time library, so you need not ship it with your
product (with the exception of their weird function pointers, I believe).

More info at:
https://github.com/peichhorn/lombok-pg/wiki/%40AutoGenMethodStub

Because I love Lombok so much, I lombok'ed your class, so you can see them
next to each other:

Original (with slight auto-formatter changes):
http://pastebin.com/EJBcsVdZ

Lombok'd:
http://pastebin.com/rUFd47Hc

Things I changed: cut down the Destination class, cleaned up the remove()
method, and the aforementioned dummy implementations.

Liebe Gruesse,
		Joerg

-- 
Ich lese meine Emails nicht, replies to Email bleiben also leider
ungelesen.

[toc] | [prev] | [next] | [standalone]


#24170

FromPatrick May <patrick@softwarematters.org>
Date2013-05-22 13:51 -0400
Message-ID<m2bo82lx7y.fsf@gulch.gateway.2wire.net>
In reply to#21520
me 2 <winona_whitener@yahoo.com> writes:
> Greetings,
>
> I was wondering if anyone knew of a library that could help me with some socket programming.  Ideally, I'd be able to provide my main program with a socket connection that would be able to go through a list of potential targets for connection and then, if the socket died unexpectedly, try to reach the next potential target.  It has to ultimately use a TCP socket and no I can't use a JMS server product--but maybe a single part of the library.  I saw something similar to what I want for c# at http://www.codeproject.com/Articles/20106/Failover-Socket-Client and I thought that I'd ask around and see if there was some cool library that I just didn't know about.
>
> Cheers,
> Me

     Depending on your requirements, you might want to look into Jini
(http://www.softwarematters.org/jini-lookup-utils.html).  The technology
itself is more or less abandoned, but the concepts underlying it allow
you to build highly resilient systems on top of unreliable
infrastructure.

Regards,

Patrick

------------------------------------------------------------------------
http://www.softwarematters.org
Large scale, mission-critical, distributed OO systems design and
implementation.  (C++, Java, Common Lisp, Jini, middleware, SOA)

[toc] | [prev] | [next] | [standalone]


#24174

Frombob smith <bob@coolfone.comze.com>
Date2013-05-23 12:43 -0700
Message-ID<584cde8f-7820-4940-a8bc-3cc02cd4de39@googlegroups.com>
In reply to#21520
On Friday, January 18, 2013 10:00:03 AM UTC-6, me 2 wrote:
> Greetings,
> 
> 
> 
> I was wondering if anyone knew of a library that could help me with some socket programming.  Ideally, I'd be able to provide my main program with a socket connection that would be able to go through a list of potential targets for connection and then, if the socket died unexpectedly, try to reach the next potential target.  It has to ultimately use a TCP socket and no I can't use a JMS server product--but maybe a single part of the library.  I saw something similar to what I want for c# at http://www.codeproject.com/Articles/20106/Failover-Socket-Client and I thought that I'd ask around and see if there was some cool library that I just didn't know about.
> 
> 
> 
> Cheers,
> 
> Me

Maybe you can tell us what type of project you are working on?

The above approach sounds like a not-so-great strategy for almost all projects.

It may be ok for e-mail delivery, but I can't think of anything else where you would want that behavior.

Thanks.

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.java.programmer


csiph-web