Skip to content

FailoverClientConnectionFactory does not cache shared connection #3199

@garyrussell

Description

@garyrussell

See https://stackoverflow.com/questions/60432039/failoverclientconnectionfactory-connected-with-fail-over-server-but-still-tryin

@SpringBootApplication
@EnableScheduling
public class So60432039Application {

	public static void main(String[] args) {
		SpringApplication.run(So60432039Application.class, args);
	}

	@Bean
	public AbstractServerConnectionFactory server1() {
		return Tcp.netServer(1234).get();
	}

	@Bean
	public AbstractServerConnectionFactory server2() {
		AbstractServerConnectionFactory factory = Tcp.netServer(1235).get();
		factory.registerListener(m -> {
			System.out.println(m.getPayload());
			return false;
		});
		return factory;
	}

	@Bean
	public AbstractClientConnectionFactory client1() {
		return Tcp.netClient("localhost", 1234).get();
	}

	@Bean
	public AbstractClientConnectionFactory client2() {
		return Tcp.netClient("localhost", 1235).get();
	}

	@Bean
	public FailoverClientConnectionFactory failover() {
		FailoverClientConnectionFactory factory =
				new FailoverClientConnectionFactory(Arrays.asList(client1(), client2()));
		factory.registerListener(m -> false);
		return factory;
	}

	@Scheduled(fixedDelay = 5_000)
	public void getConnection() {
		try {
			TcpConnectionSupport connection = failover().getConnection();
			connection.send(new GenericMessage<>("foo"));
			System.out.println(connection.getConnectionId());
		}
		catch (InterruptedException e) {
			Thread.currentThread().interrupt();
		}
		catch (RuntimeException e) {
			System.out.println(e.getMessage());
		}
	}

	@Bean
	public ApplicationRunner runner() {
		return args -> {
			client1().start();
			client2().start();
			failover().start();
			System.out.println("Hit enter to start server2");
			System.in.read();
			server2().start();
		};
	}

}

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions