Add RSocket Java DSL

* Add `@NonNullApi` for RSocket packages
* Some code style and JavaDocs polishing
This commit is contained in:
Artem Bilan
2019-06-04 13:06:02 -04:00
committed by Gary Russell
parent 0c32a57e99
commit 5e7c1ae2e7
14 changed files with 527 additions and 15 deletions

View File

@@ -0,0 +1,140 @@
/*
* Copyright 2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.integration.rsocket.dsl;
import static org.assertj.core.api.Assertions.assertThat;
import java.util.function.Function;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.DependsOn;
import org.springframework.core.codec.CharSequenceEncoder;
import org.springframework.core.codec.StringDecoder;
import org.springframework.core.io.buffer.NettyDataBufferFactory;
import org.springframework.integration.config.EnableIntegration;
import org.springframework.integration.dsl.IntegrationFlow;
import org.springframework.integration.dsl.IntegrationFlows;
import org.springframework.integration.rsocket.ClientRSocketConnector;
import org.springframework.integration.rsocket.ServerRSocketConnector;
import org.springframework.integration.rsocket.outbound.RSocketOutboundGateway;
import org.springframework.messaging.rsocket.RSocketStrategies;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
import io.netty.buffer.PooledByteBufAllocator;
import io.netty.util.NetUtil;
import io.rsocket.frame.decoder.PayloadDecoder;
import io.rsocket.transport.netty.client.TcpClientTransport;
import io.rsocket.transport.netty.server.TcpServerTransport;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import reactor.netty.tcp.InetSocketAddressUtil;
import reactor.netty.tcp.TcpClient;
import reactor.netty.tcp.TcpServer;
/**
* @author Artem Bilan
*
* @since 5.2
*/
@SpringJUnitConfig
@DirtiesContext
public class RSocketDslTests {
@Autowired
@Qualifier("rsocketUpperCaseRequestFlow.gateway")
private Function<String, String> rsocketUpperCaseFlowFunction;
@Test
void testRsocketUpperCaseFlows() {
assertThat(this.rsocketUpperCaseFlowFunction.apply("hello world")).isEqualTo("HELLO WORLD");
}
@Configuration
@EnableIntegration
public static class TestConfiguration {
private volatile int port;
@Bean
public RSocketStrategies rsocketStrategies() {
return RSocketStrategies.builder()
.decoder(StringDecoder.allMimeTypes())
.encoder(CharSequenceEncoder.allMimeTypes())
.dataBufferFactory(new NettyDataBufferFactory(PooledByteBufAllocator.DEFAULT))
.build();
}
@Bean
public ServerRSocketConnector serverRSocketConnector() {
TcpServer tcpServer =
TcpServer.create()
.port(0)
.doOnBound((server) -> this.port = server.port());
ServerRSocketConnector serverRSocketConnector =
new ServerRSocketConnector(TcpServerTransport.create(tcpServer));
serverRSocketConnector.setRSocketStrategies(rsocketStrategies());
serverRSocketConnector.setFactoryConfigurer((factory) -> factory.frameDecoder(PayloadDecoder.ZERO_COPY));
return serverRSocketConnector;
}
@Bean
@DependsOn("serverRSocketConnector")
public ClientRSocketConnector clientRSocketConnector() {
ClientRSocketConnector clientRSocketConnector =
new ClientRSocketConnector(
TcpClientTransport.create(
TcpClient.create()
.addressSupplier(() ->
InetSocketAddressUtil.createUnresolved(
NetUtil.LOCALHOST.getHostAddress(), this.port))
));
clientRSocketConnector.setFactoryConfigurer((factory) -> factory.frameDecoder(PayloadDecoder.ZERO_COPY));
clientRSocketConnector.setRSocketStrategies(rsocketStrategies());
clientRSocketConnector.setAutoStartup(false);
return clientRSocketConnector;
}
@Bean
public IntegrationFlow rsocketUpperCaseRequestFlow() {
return IntegrationFlows
.from(Function.class)
.handle(RSockets.outboundGateway("/uppercase")
.command((message) -> RSocketOutboundGateway.Command.requestResponse)
.expectedResponseType("T(java.lang.String)")
.clientRSocketConnector(clientRSocketConnector()))
.get();
}
@Bean
public IntegrationFlow rsocketUpperCaseFlow() {
return IntegrationFlows
.from(RSockets.inboundGateway("/uppercase")
.rsocketStrategies(rsocketStrategies()))
.<Flux<String>, Mono<String>>transform((flux) -> flux.next().map(String::toUpperCase))
.get();
}
}
}