Search in sources :

Example 1 with Uploader

use of com.smoketurner.uploader.core.Uploader in project uploader by smoketurner.

the class UploaderApplication method run.

@Override
public void run(@Nonnull final UploaderConfiguration configuration, @Nonnull final Environment environment) throws Exception {
    final NettyConfiguration nettyConfig = configuration.getNetty();
    final AwsConfiguration awsConfig = configuration.getAws();
    // we create the event loop groups first so we can share them between
    // the Netty server receiving the requests and the AWS S3 client
    // uploading the batches to S3.
    final EventLoopGroup bossGroup = Netty.newBossEventLoopGroup();
    final EventLoopGroup workerGroup = Netty.newWorkerEventLoopGroup();
    environment.lifecycle().manage(new EventLoopGroupManager(bossGroup));
    environment.lifecycle().manage(new EventLoopGroupManager(workerGroup));
    final Size maxUploadSize = awsConfig.getMaxUploadSize();
    final EventLoopGroupConfiguration eventLoopConfig = EventLoopGroupConfiguration.builder().eventLoopGroup(workerGroup).build();
    final NettySdkHttpClientFactory nettyFactory = NettySdkHttpClientFactory.builder().eventLoopGroupConfiguration(eventLoopConfig).build();
    final ClientAsyncHttpConfiguration httpConfig = ClientAsyncHttpConfiguration.builder().httpClientFactory(nettyFactory).build();
    // build the asynchronous S3 client with the configured credentials
    // provider and region and use the same Netty event group as the server.
    final S3AsyncClient s3 = S3AsyncClient.builder().credentialsProvider(awsConfig.getCredentials()).region(awsConfig.getRegion()).asyncHttpConfiguration(httpConfig).build();
    environment.lifecycle().manage(new AutoCloseableManager(s3));
    final Uploader uploader = new Uploader(s3, awsConfig);
    final UploadInitializer initializer = new UploadInitializer(nettyConfig, uploader, maxUploadSize.toBytes());
    final ServerBootstrap bootstrap = new ServerBootstrap();
    // Start the server
    final ChannelFuture future = bootstrap.group(bossGroup, workerGroup).handler(new LoggingHandler(LogLevel.INFO)).option(ChannelOption.SO_BACKLOG, 128).channel(Netty.serverChannelType()).childOption(ChannelOption.SO_KEEPALIVE, true).childHandler(initializer).bind(nettyConfig.getListenPort());
    environment.lifecycle().manage(new ChannelFutureManager(future));
    // Resources
    environment.jersey().register(new BatchResource(uploader));
    environment.jersey().register(new PingResource());
    environment.jersey().register(new VersionResource());
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) AwsConfiguration(com.smoketurner.uploader.config.AwsConfiguration) NettySdkHttpClientFactory(software.amazon.awssdk.http.nio.netty.NettySdkHttpClientFactory) LoggingHandler(io.netty.handler.logging.LoggingHandler) EventLoopGroupConfiguration(software.amazon.awssdk.http.nio.netty.EventLoopGroupConfiguration) AutoCloseableManager(io.dropwizard.lifecycle.AutoCloseableManager) Size(io.dropwizard.util.Size) ChannelFutureManager(com.smoketurner.uploader.managed.ChannelFutureManager) VersionResource(com.smoketurner.uploader.resources.VersionResource) EventLoopGroupManager(com.smoketurner.uploader.managed.EventLoopGroupManager) ServerBootstrap(io.netty.bootstrap.ServerBootstrap) PingResource(com.smoketurner.uploader.resources.PingResource) S3AsyncClient(software.amazon.awssdk.services.s3.S3AsyncClient) ClientAsyncHttpConfiguration(software.amazon.awssdk.core.client.builder.ClientAsyncHttpConfiguration) EventLoopGroup(io.netty.channel.EventLoopGroup) UploadInitializer(com.smoketurner.uploader.handler.UploadInitializer) NettyConfiguration(com.smoketurner.uploader.config.NettyConfiguration) BatchResource(com.smoketurner.uploader.resources.BatchResource) Uploader(com.smoketurner.uploader.core.Uploader)

Aggregations

AwsConfiguration (com.smoketurner.uploader.config.AwsConfiguration)1 NettyConfiguration (com.smoketurner.uploader.config.NettyConfiguration)1 Uploader (com.smoketurner.uploader.core.Uploader)1 UploadInitializer (com.smoketurner.uploader.handler.UploadInitializer)1 ChannelFutureManager (com.smoketurner.uploader.managed.ChannelFutureManager)1 EventLoopGroupManager (com.smoketurner.uploader.managed.EventLoopGroupManager)1 BatchResource (com.smoketurner.uploader.resources.BatchResource)1 PingResource (com.smoketurner.uploader.resources.PingResource)1 VersionResource (com.smoketurner.uploader.resources.VersionResource)1 AutoCloseableManager (io.dropwizard.lifecycle.AutoCloseableManager)1 Size (io.dropwizard.util.Size)1 ServerBootstrap (io.netty.bootstrap.ServerBootstrap)1 ChannelFuture (io.netty.channel.ChannelFuture)1 EventLoopGroup (io.netty.channel.EventLoopGroup)1 LoggingHandler (io.netty.handler.logging.LoggingHandler)1 ClientAsyncHttpConfiguration (software.amazon.awssdk.core.client.builder.ClientAsyncHttpConfiguration)1 EventLoopGroupConfiguration (software.amazon.awssdk.http.nio.netty.EventLoopGroupConfiguration)1 NettySdkHttpClientFactory (software.amazon.awssdk.http.nio.netty.NettySdkHttpClientFactory)1 S3AsyncClient (software.amazon.awssdk.services.s3.S3AsyncClient)1