org.springframework.messaging.tcp.TcpConnection Java Examples

The following examples show how to use org.springframework.messaging.tcp.TcpConnection. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: StompBrokerRelayMessageHandler.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
private void sendSystemSubscriptions() {
	int i = 0;
	for (String destination : getSystemSubscriptions().keySet()) {
		StompHeaderAccessor accessor = StompHeaderAccessor.create(StompCommand.SUBSCRIBE);
		accessor.setSubscriptionId(String.valueOf(i++));
		accessor.setDestination(destination);
		if (logger.isDebugEnabled()) {
			logger.debug("Subscribing to " + destination + " on \"system\" connection.");
		}
		TcpConnection<byte[]> conn = getTcpConnection();
		if (conn != null) {
			MessageHeaders headers = accessor.getMessageHeaders();
			conn.send(MessageBuilder.createMessage(EMPTY_PAYLOAD, headers)).addCallback(
					new ListenableFutureCallback<Void>() {
						public void onSuccess(Void result) {
						}
						public void onFailure(Throwable ex) {
							String error = "Failed to subscribe in \"system\" session.";
							handleTcpConnectionFailure(error, ex);
						}
					});
		}
	}
}
 
Example #2
Source File: ReactorNettyTcpClient.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Override
@SuppressWarnings("unchecked")
public Publisher<Void> apply(NettyInbound inbound, NettyOutbound outbound) {
	inbound.withConnection(conn -> {
		if (logger.isDebugEnabled()) {
			logger.debug("Connected to " + conn.address());
		}
	});
	DirectProcessor<Void> completion = DirectProcessor.create();
	TcpConnection<P> connection = new ReactorNettyTcpConnection<>(inbound, outbound,  codec, completion);
	scheduler.schedule(() -> this.connectionHandler.afterConnected(connection));

	inbound.withConnection(conn -> conn.addHandler(new StompMessageDecoder<>(codec)));

	inbound.receiveObject()
			.cast(Message.class)
			.publishOn(scheduler, PUBLISH_ON_BUFFER_SIZE)
			.subscribe(
					this.connectionHandler::handleMessage,
					this.connectionHandler::handleFailure,
					this.connectionHandler::afterConnectionClosed);

	return completion;
}
 
Example #3
Source File: StompBrokerRelayMessageHandler.java    From java-technology-stack with MIT License 6 votes vote down vote up
private void sendSystemSubscriptions() {
	int i = 0;
	for (String destination : getSystemSubscriptions().keySet()) {
		StompHeaderAccessor accessor = StompHeaderAccessor.create(StompCommand.SUBSCRIBE);
		accessor.setSubscriptionId(String.valueOf(i++));
		accessor.setDestination(destination);
		if (logger.isDebugEnabled()) {
			logger.debug("Subscribing to " + destination + " on \"system\" connection.");
		}
		TcpConnection<byte[]> conn = getTcpConnection();
		if (conn != null) {
			MessageHeaders headers = accessor.getMessageHeaders();
			conn.send(MessageBuilder.createMessage(EMPTY_PAYLOAD, headers)).addCallback(
					result -> {},
					ex -> {
						String error = "Failed to subscribe in \"system\" session.";
						handleTcpConnectionFailure(error, ex);
					});
		}
	}
}
 
Example #4
Source File: StompBrokerRelayMessageHandler.java    From java-technology-stack with MIT License 6 votes vote down vote up
/**
 * Clean up state associated with the connection and close it.
 * Any exception arising from closing the connection are propagated.
 */
public void clearConnection() {
	if (logger.isDebugEnabled()) {
		logger.debug("Cleaning up connection state for session " + this.sessionId);
	}

	if (this.isRemoteClientSession) {
		StompBrokerRelayMessageHandler.this.connectionHandlers.remove(this.sessionId);
	}

	this.isStompConnected = false;

	TcpConnection<byte[]> conn = this.tcpConnection;
	this.tcpConnection = null;
	if (conn != null) {
		if (logger.isDebugEnabled()) {
			logger.debug("Closing TCP connection in session " + this.sessionId);
		}
		conn.close();
	}
}
 
Example #5
Source File: WebSocketStompClientTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
@SuppressWarnings({"rawtypes", "unchecked"})
public void cancelInactivityTasks() throws Exception {
	TcpConnection<byte[]> tcpConnection = getTcpConnection();

	ScheduledFuture future = mock(ScheduledFuture.class);
	when(this.taskScheduler.scheduleWithFixedDelay(any(), eq(1L))).thenReturn(future);

	tcpConnection.onReadInactivity(mock(Runnable.class), 2L);
	tcpConnection.onWriteInactivity(mock(Runnable.class), 2L);

	this.webSocketHandlerCaptor.getValue().afterConnectionClosed(this.webSocketSession, CloseStatus.NORMAL);

	verify(future, times(2)).cancel(true);
	verifyNoMoreInteractions(future);
}
 
Example #6
Source File: StompBrokerRelayMessageHandler.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
/**
 * Clean up state associated with the connection and close it.
 * Any exception arising from closing the connection are propagated.
 */
public void clearConnection() {
	if (logger.isDebugEnabled()) {
		logger.debug("Cleaning up connection state for session " + this.sessionId);
	}

	if (this.isRemoteClientSession) {
		StompBrokerRelayMessageHandler.this.connectionHandlers.remove(this.sessionId);
	}

	this.isStompConnected = false;

	TcpConnection<byte[]> conn = this.tcpConnection;
	this.tcpConnection = null;
	if (conn != null) {
		if (logger.isDebugEnabled()) {
			logger.debug("Closing TCP connection in session " + this.sessionId);
		}
		conn.close();
	}
}
 
Example #7
Source File: WebSocketStompClientTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
@SuppressWarnings({"rawtypes", "unchecked"})
public void cancelInactivityTasks() throws Exception {
	TcpConnection<byte[]> tcpConnection = getTcpConnection();

	ScheduledFuture future = mock(ScheduledFuture.class);
	given(this.taskScheduler.scheduleWithFixedDelay(any(), eq(1L))).willReturn(future);

	tcpConnection.onReadInactivity(mock(Runnable.class), 2L);
	tcpConnection.onWriteInactivity(mock(Runnable.class), 2L);

	this.webSocketHandlerCaptor.getValue().afterConnectionClosed(this.webSocketSession, CloseStatus.NORMAL);

	verify(future, times(2)).cancel(true);
	verifyNoMoreInteractions(future);
}
 
Example #8
Source File: ReactorNettyTcpClient.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Override
@SuppressWarnings("unchecked")
public Publisher<Void> apply(NettyInbound inbound, NettyOutbound outbound) {
	inbound.withConnection(conn -> {
		if (logger.isDebugEnabled()) {
			logger.debug("Connected to " + conn.address());
		}
	});
	DirectProcessor<Void> completion = DirectProcessor.create();
	TcpConnection<P> connection = new ReactorNettyTcpConnection<>(inbound, outbound,  codec, completion);
	scheduler.schedule(() -> this.connectionHandler.afterConnected(connection));

	inbound.withConnection(conn -> conn.addHandler(new StompMessageDecoder<>(codec)));

	inbound.receiveObject()
			.cast(Message.class)
			.publishOn(scheduler, PUBLISH_ON_BUFFER_SIZE)
			.subscribe(
					this.connectionHandler::handleMessage,
					this.connectionHandler::handleFailure,
					this.connectionHandler::afterConnectionClosed);

	return completion;
}
 
Example #9
Source File: StompBrokerRelayMessageHandler.java    From spring-analysis-note with MIT License 6 votes vote down vote up
private void sendSystemSubscriptions() {
	int i = 0;
	for (String destination : getSystemSubscriptions().keySet()) {
		StompHeaderAccessor accessor = StompHeaderAccessor.create(StompCommand.SUBSCRIBE);
		accessor.setSubscriptionId(String.valueOf(i++));
		accessor.setDestination(destination);
		if (logger.isDebugEnabled()) {
			logger.debug("Subscribing to " + destination + " on \"system\" connection.");
		}
		TcpConnection<byte[]> conn = getTcpConnection();
		if (conn != null) {
			MessageHeaders headers = accessor.getMessageHeaders();
			conn.send(MessageBuilder.createMessage(EMPTY_PAYLOAD, headers)).addCallback(
					result -> {},
					ex -> {
						String error = "Failed to subscribe in \"system\" session.";
						handleTcpConnectionFailure(error, ex);
					});
		}
	}
}
 
Example #10
Source File: StompBrokerRelayMessageHandler.java    From spring-analysis-note with MIT License 6 votes vote down vote up
/**
 * Clean up state associated with the connection and close it.
 * Any exception arising from closing the connection are propagated.
 */
public void clearConnection() {
	if (logger.isDebugEnabled()) {
		logger.debug("Cleaning up connection state for session " + this.sessionId);
	}

	if (this.isRemoteClientSession) {
		StompBrokerRelayMessageHandler.this.connectionHandlers.remove(this.sessionId);
	}

	this.isStompConnected = false;

	TcpConnection<byte[]> conn = this.tcpConnection;
	this.tcpConnection = null;
	if (conn != null) {
		if (logger.isDebugEnabled()) {
			logger.debug("Closing TCP connection in session " + this.sessionId);
		}
		conn.close();
	}
}
 
Example #11
Source File: WebSocketStompClientTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
@SuppressWarnings({ "rawtypes", "unchecked" })
public void cancelInactivityTasks() throws Exception {
	TcpConnection<byte[]> tcpConnection = getTcpConnection();

	ScheduledFuture future = mock(ScheduledFuture.class);
	when(this.taskScheduler.scheduleWithFixedDelay(any(), eq(1L))).thenReturn(future);

	tcpConnection.onReadInactivity(mock(Runnable.class), 2L);
	tcpConnection.onWriteInactivity(mock(Runnable.class), 2L);

	this.webSocketHandlerCaptor.getValue().afterConnectionClosed(this.webSocketSession, CloseStatus.NORMAL);

	verify(future, times(2)).cancel(true);
	verifyNoMoreInteractions(future);
}
 
Example #12
Source File: WebSocketStompClientTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void readInactivityAfterDelayHasElapsed() throws Exception {
	TcpConnection<byte[]> tcpConnection = getTcpConnection();
	Runnable runnable = mock(Runnable.class);
	long delay = 2;
	tcpConnection.onReadInactivity(runnable, delay);
	testInactivityTaskScheduling(runnable, delay, 10);
}
 
Example #13
Source File: DefaultStompSession.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Override
public void run() {
	TcpConnection<byte[]> conn = connection;
	if (conn != null) {
		conn.send(HEARTBEAT).addCallback(
				new ListenableFutureCallback<Void>() {
					public void onSuccess(Void result) {
					}
					public void onFailure(Throwable ex) {
						handleFailure(ex);
					}
				});
	}
}
 
Example #14
Source File: DefaultStompSession.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
private void resetConnection() {
	TcpConnection<?> conn = this.connection;
	this.connection = null;
	if (conn != null) {
		try {
			conn.close();
		}
		catch (Throwable ex) {
			// Ignore
		}
	}
}
 
Example #15
Source File: DefaultStompSession.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Override
public void afterConnected(TcpConnection<byte[]> connection) {
	this.connection = connection;
	if (logger.isDebugEnabled()) {
		logger.debug("Connection established in session id=" + this.sessionId);
	}
	StompHeaderAccessor accessor = createHeaderAccessor(StompCommand.CONNECT);
	accessor.addNativeHeaders(this.connectHeaders);
	accessor.setAcceptVersion("1.1,1.2");
	Message<byte[]> message = createMessage(accessor, EMPTY_PAYLOAD);
	execute(message);
}
 
Example #16
Source File: StompBrokerRelayMessageHandler.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Override
public void afterConnected(TcpConnection<byte[]> connection) {
	if (logger.isDebugEnabled()) {
		logger.debug("TCP connection opened in session=" + getSessionId());
	}
	this.tcpConnection = connection;
	connection.onReadInactivity(() -> {
		if (this.tcpConnection != null && !this.isStompConnected) {
			handleTcpConnectionFailure("No CONNECTED frame received in " +
					MAX_TIME_TO_CONNECTED_FRAME + " ms.", null);
		}
	}, MAX_TIME_TO_CONNECTED_FRAME);
	connection.send(MessageBuilder.createMessage(EMPTY_PAYLOAD, this.connectHeaders.getMessageHeaders()));
}
 
Example #17
Source File: WebSocketStompClientTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void readInactivityBeforeDelayHasElapsed() throws Exception {
	TcpConnection<byte[]> tcpConnection = getTcpConnection();
	Runnable runnable = mock(Runnable.class);
	long delay = 10000;
	tcpConnection.onReadInactivity(runnable, delay);
	testInactivityTaskScheduling(runnable, delay, 0);
}
 
Example #18
Source File: WebSocketStompClientTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void writeInactivityAfterDelayHasElapsed() throws Exception {
	TcpConnection<byte[]> tcpConnection = getTcpConnection();
	Runnable runnable = mock(Runnable.class);
	long delay = 2;
	tcpConnection.onWriteInactivity(runnable, delay);
	testInactivityTaskScheduling(runnable, delay, 10);
}
 
Example #19
Source File: StompBrokerRelayMessageHandler.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Override
public void afterConnected(TcpConnection<byte[]> connection) {
	if (logger.isDebugEnabled()) {
		logger.debug("TCP connection opened in session=" + getSessionId());
	}
	this.tcpConnection = connection;
	connection.send(MessageBuilder.createMessage(EMPTY_PAYLOAD, this.connectHeaders.getMessageHeaders()));
}
 
Example #20
Source File: WebSocketStompClientTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void writeInactivityBeforeDelayHasElapsed() throws Exception {
	TcpConnection<byte[]> tcpConnection = getTcpConnection();
	Runnable runnable = mock(Runnable.class);
	long delay = 1000;
	tcpConnection.onWriteInactivity(runnable, delay);
	testInactivityTaskScheduling(runnable, delay, 0);
}
 
Example #21
Source File: WebSocketStompClientTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void writeInactivityBeforeDelayHasElapsed() throws Exception {
	TcpConnection<byte[]> tcpConnection = getTcpConnection();
	Runnable runnable = mock(Runnable.class);
	long delay = 1000;
	tcpConnection.onWriteInactivity(runnable, delay);
	testInactivityTaskScheduling(runnable, delay, 0);
}
 
Example #22
Source File: WebSocketStompClientTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void writeInactivityAfterDelayHasElapsed() throws Exception {
	TcpConnection<byte[]> tcpConnection = getTcpConnection();
	Runnable runnable = mock(Runnable.class);
	long delay = 2;
	tcpConnection.onWriteInactivity(runnable, delay);
	testInactivityTaskScheduling(runnable, delay, 10);
}
 
Example #23
Source File: WebSocketStompClientTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void readInactivityBeforeDelayHasElapsed() throws Exception {
	TcpConnection<byte[]> tcpConnection = getTcpConnection();
	Runnable runnable = mock(Runnable.class);
	long delay = 10000;
	tcpConnection.onReadInactivity(runnable, delay);
	testInactivityTaskScheduling(runnable, delay, 0);
}
 
Example #24
Source File: WebSocketStompClientTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void readInactivityAfterDelayHasElapsed() throws Exception {
	TcpConnection<byte[]> tcpConnection = getTcpConnection();
	Runnable runnable = mock(Runnable.class);
	long delay = 2;
	tcpConnection.onReadInactivity(runnable, delay);
	testInactivityTaskScheduling(runnable, delay, 10);
}
 
Example #25
Source File: DefaultStompSession.java    From java-technology-stack with MIT License 5 votes vote down vote up
private void resetConnection() {
	TcpConnection<?> conn = this.connection;
	this.connection = null;
	if (conn != null) {
		try {
			conn.close();
		}
		catch (Throwable ex) {
			// ignore
		}
	}
}
 
Example #26
Source File: DefaultStompSession.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Override
public void afterConnected(TcpConnection<byte[]> connection) {
	this.connection = connection;
	if (logger.isDebugEnabled()) {
		logger.debug("Connection established in session id=" + this.sessionId);
	}
	StompHeaderAccessor accessor = createHeaderAccessor(StompCommand.CONNECT);
	accessor.addNativeHeaders(this.connectHeaders);
	if (this.connectHeaders.getAcceptVersion() == null) {
		accessor.setAcceptVersion("1.1,1.2");
	}
	Message<byte[]> message = createMessage(accessor, EMPTY_PAYLOAD);
	execute(message);
}
 
Example #27
Source File: DefaultStompSession.java    From spring-analysis-note with MIT License 5 votes vote down vote up
private void resetConnection() {
	TcpConnection<?> conn = this.connection;
	this.connection = null;
	if (conn != null) {
		try {
			conn.close();
		}
		catch (Throwable ex) {
			// ignore
		}
	}
}
 
Example #28
Source File: DefaultStompSession.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Override
public void run() {
	TcpConnection<byte[]> conn = connection;
	if (conn != null) {
		conn.send(HEARTBEAT).addCallback(
				new ListenableFutureCallback<Void>() {
					public void onSuccess(@Nullable Void result) {
					}
					public void onFailure(Throwable ex) {
						handleFailure(ex);
					}
				});
	}
}
 
Example #29
Source File: WebSocketStompClientTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void readInactivityAfterDelayHasElapsed() throws Exception {
	TcpConnection<byte[]> tcpConnection = getTcpConnection();
	Runnable runnable = mock(Runnable.class);
	long delay = 2;
	tcpConnection.onReadInactivity(runnable, delay);
	testInactivityTaskScheduling(runnable, delay, 10);
}
 
Example #30
Source File: WebSocketStompClientTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void readInactivityBeforeDelayHasElapsed() throws Exception {
	TcpConnection<byte[]> tcpConnection = getTcpConnection();
	Runnable runnable = mock(Runnable.class);
	long delay = 10000;
	tcpConnection.onReadInactivity(runnable, delay);
	testInactivityTaskScheduling(runnable, delay, 0);
}