本文整理汇总了Scala中io.netty.channel.nio.NioEventLoopGroup类的典型用法代码示例。如果您正苦于以下问题:Scala NioEventLoopGroup类的具体用法?Scala NioEventLoopGroup怎么用?Scala NioEventLoopGroup使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了NioEventLoopGroup类的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Scala代码示例。
示例1: Pusher
//设置package包名称以及导入依赖的类
package reactivehub.akka.stream.apns.pusher
import akka.actor.ActorSystem
import akka.kafka.ConsumerSettings
import akka.kafka.scaladsl.Consumer
import akka.stream.ActorMaterializer
import akka.stream.scaladsl.{Keep, Sink}
import io.netty.channel.nio.NioEventLoopGroup
import io.netty.handler.ssl.SslContext
import org.apache.kafka.clients.consumer.ConsumerConfig.AUTO_OFFSET_RESET_CONFIG
import reactivehub.akka.stream.apns.Environment._
import reactivehub.akka.stream.apns.TlsUtil._
import reactivehub.akka.stream.apns._
import reactivehub.akka.stream.apns.marshallers.SprayJsonSupport
object Pusher extends SprayJsonSupport {
val kafka = "192.168.99.100:9092"
val clientId = "pusher1"
val consumerGroup = "pusher"
val topics = Set("notifications")
implicit val system = ActorSystem("system")
implicit val materializer = ActorMaterializer()
import system.dispatcher
def main(args: Array[String]): Unit = {
val group = new NioEventLoopGroup()
val apns = ApnsExt(system).connection[Long](Development, sslContext, group)
Consumer.atMostOnceSource(consumerSettings)
.map(msg => msg.key -> toNotification(msg.value))
.filter(_._2.deviceToken.bytes.length < 100)
.viaMat(apns)(Keep.right)
.log("pusher", _.toString())
.to(Sink.ignore).run()
.onComplete { _ =>
group.shutdownGracefully()
system.terminate()
}
}
private def sslContext: SslContext =
loadPkcs12FromResource("/cert.p12", "password")
private def consumerSettings: ConsumerSettings[Long, PushData] =
ConsumerSettings(system, ScalaLongDeserializer, PushDataDeserializer, topics)
.withBootstrapServers(kafka)
.withClientId(clientId)
.withGroupId(consumerGroup)
.withProperty(AUTO_OFFSET_RESET_CONFIG, "earliest")
private def toNotification(pushData: PushData): Notification = {
var builder = Payload.Builder()
pushData.alert.foreach(alert => builder = builder.withAlert(alert))
pushData.badge.foreach(badge => builder = builder.withBadge(badge))
Notification(DeviceToken(pushData.token), builder.result)
}
}
示例2: SockServer
//设置package包名称以及导入依赖的类
package tk.dasb.server
import io.netty.bootstrap.ServerBootstrap
import io.netty.channel.ChannelOption
import io.netty.channel.nio.NioEventLoopGroup
import io.netty.channel.socket.nio.NioServerSocketChannel
import tk.dasb.handler.Sock5Handler
import tk.dasb.util.Log
object SockServer extends Log {
def startServer(port: Int) = {
val boss = new NioEventLoopGroup()
val worker = new NioEventLoopGroup()
try {
val b = new ServerBootstrap()
b.group(boss, worker).channel(classOf[NioServerSocketChannel])
.option[java.lang.Integer](ChannelOption.SO_BACKLOG, 128)
.childOption[java.lang.Boolean](ChannelOption.SO_KEEPALIVE, true)
.childHandler(new Sock5Handler)
log.info("starting server on port:{}", port)
b.bind(port).sync().channel().closeFuture().sync()
} finally {
boss.shutdownGracefully()
worker.shutdownGracefully()
}
}
}
示例3: InputStreamingServer
//设置package包名称以及导入依赖的类
package com.bwsw.sj.engine.input.connection.tcp.server
import java.util.concurrent.{Callable, ArrayBlockingQueue}
import com.bwsw.sj.engine.core.input.InputStreamingExecutor
import io.netty.bootstrap.ServerBootstrap
import io.netty.buffer.ByteBuf
import io.netty.channel._
import io.netty.channel.nio.NioEventLoopGroup
import io.netty.channel.socket.nio.NioServerSocketChannel
import io.netty.handler.logging.{LogLevel, LoggingHandler}
import org.slf4j.LoggerFactory
import scala.collection.concurrent
class InputStreamingServer(host: String,
port: Int,
executor: InputStreamingExecutor,
channelContextQueue: ArrayBlockingQueue[ChannelHandlerContext],
bufferForEachContext: concurrent.Map[ChannelHandlerContext, ByteBuf]) extends Callable[Unit] {
private val logger = LoggerFactory.getLogger(this.getClass)
override def call() = {
logger.info(s"Launch input streaming server on: '$host:$port'\n")
val bossGroup: EventLoopGroup = new NioEventLoopGroup()
val workerGroup = new NioEventLoopGroup()
try {
val bootstrapServer = new ServerBootstrap()
bootstrapServer.group(bossGroup, workerGroup)
.channel(classOf[NioServerSocketChannel])
.handler(new LoggingHandler(LogLevel.INFO))
.childHandler(new InputStreamingChannelInitializer(executor, channelContextQueue, bufferForEachContext))
bootstrapServer.bind(host, port).sync().channel().closeFuture().sync()
} finally {
workerGroup.shutdownGracefully()
bossGroup.shutdownGracefully()
}
}
}
示例4: Server
//设置package包名称以及导入依赖的类
package com.scxmpp.server
import java.net.InetSocketAddress
import io.netty.bootstrap.ServerBootstrap
import io.netty.buffer.PooledByteBufAllocator
import io.netty.channel.epoll.{Epoll, EpollEventLoopGroup, EpollServerSocketChannel}
import io.netty.channel.nio.NioEventLoopGroup
import io.netty.channel.socket.nio.NioServerSocketChannel
import io.netty.channel.{Channel, ChannelOption, EventLoopGroup, ServerChannel, ChannelInitializer}
import scala.collection.JavaConversions._
import scala.collection.mutable.ArrayBuffer
import com.typesafe.config.Config
class Server(context: ServerContext) {
var channels = ArrayBuffer.empty[Channel]
var bootstraps = ArrayBuffer.empty[ServerBootstrap]
private def doRun(group: EventLoopGroup, clazz: Class[_ <: ServerChannel]): Unit = {
try {
for(server <- context.servers) {
val bootstrap = new ServerBootstrap()
bootstrap.childOption(ChannelOption.ALLOCATOR, new PooledByteBufAllocator(true))
val initializer = context.dynamicAccess.createInstanceFor[ChannelInitializer[Channel]](
server.getString("module"), List(classOf[ServerContext] -> context, classOf[Config] -> server)).get
bootstrap.group(group).channel(clazz).childHandler(initializer)
val inet: InetSocketAddress = new InetSocketAddress(
server.getString("endpoint.address"), server.getInt("endpoint.port"))
channels.add(bootstrap.bind(inet).sync.channel)
}
} finally {
for (channel <- channels)
channel.closeFuture.sync
group.shutdownGracefully.sync
}
}
def run(): Unit = {
if (Epoll.isAvailable) {
doRun(new EpollEventLoopGroup, classOf[EpollServerSocketChannel])
}
else {
doRun(new NioEventLoopGroup, classOf[NioServerSocketChannel])
}
}
}
示例5: NettyHttpServer
//设置package包名称以及导入依赖的类
package woshilaiceshide.sserver.benchmark.netty
import io.netty.bootstrap.ServerBootstrap
import io.netty.channel.Channel
import io.netty.channel.ChannelInitializer
import io.netty.channel.ChannelOption
import io.netty.channel.EventLoopGroup
import io.netty.channel.nio.NioEventLoopGroup
import io.netty.channel.socket.SocketChannel
import io.netty.channel.socket.nio.NioServerSocketChannel
import io.netty.example.http.helloworld.HttpHelloWorldServerHandler
import io.netty.handler.codec.http.HttpServerCodec;
import io.netty.channel.socket.nio.NioServerSocketChannel
object NettyHttpServer extends App with woshilaiceshide.sserver.benchmark.ServerProperty {
class HttpHelloWorldServerInitializer extends ChannelInitializer[SocketChannel] {
override def initChannel(ch: SocketChannel) {
//ch.config().setAllowHalfClosure(true)
val p = ch.pipeline();
p.addLast(new HttpServerCodec());
p.addLast(new HttpHelloWorldServerHandler());
}
}
val bossGroup: EventLoopGroup = new NioEventLoopGroup(1);
val workerGroup: EventLoopGroup = new NioEventLoopGroup(2);
try {
val b = new ServerBootstrap();
b.option[java.lang.Integer](ChannelOption.SO_BACKLOG, 1024);
b.group(bossGroup, workerGroup).channel(classOf[NioServerSocketChannel]).childHandler(new HttpHelloWorldServerInitializer());
val ch: Channel = b.bind(interface, port).sync().channel();
//??? just check for ipv4
System.err.println(s"Open your web browser and navigate to http://${if ("0.0.0.0" == interface) "127.0.0.1" else interface}:${port}/");
ch.closeFuture().sync();
} finally {
bossGroup.shutdownGracefully();
workerGroup.shutdownGracefully();
}
}
示例6: PortClient
//设置package包名称以及导入依赖的类
package io.dac.mara.ports
import io.netty.bootstrap.Bootstrap
import io.netty.buffer.Unpooled
import io.netty.channel._
import io.netty.channel.nio.NioEventLoopGroup
import io.netty.channel.socket.SocketChannel
import io.netty.channel.socket.nio._
import io.netty.handler.codec.string.{StringDecoder, StringEncoder}
import io.netty.util.CharsetUtil
class PortClient {
val group = new NioEventLoopGroup()
def connect(group: EventLoopGroup): ChannelFuture = {
val bootstrap = new Bootstrap()
.group(group)
.channel(classOf[NioSocketChannel])
.option[java.lang.Boolean](ChannelOption.TCP_NODELAY, true)
.handler(new ChannelInitializer[SocketChannel]{
override def initChannel(ch: SocketChannel): Unit = {
val pipeline = ch.pipeline()
println("Adding handler to pipeline")
pipeline.addLast(new StringEncoder(CharsetUtil.UTF_8))
pipeline.addLast(new StringDecoder(CharsetUtil.UTF_8))
pipeline.addLast(new PortClientHandler)
}
})
bootstrap.connect("127.0.0.1", 12345).sync()
}
def start: Unit = {
try {
val future = connect(group)
val channel = future.channel()
val message = "D0\"hello, world\""
val buffer = Unpooled.buffer(message.length)
message.foreach { ch =>
buffer.writeByte(ch.toInt)
}
channel.writeAndFlush(buffer).sync()
} finally {
Thread.sleep(10000)
group.shutdownGracefully()
println("Shut down")
}
}
}
object PortClient {
def main(args: Array[String]): Unit = {
val client = new PortClient
client.start
}
}
示例7: GrpcGatewayServerBuilder
//设置package包名称以及导入依赖的类
package grpcgateway.server
import grpcgateway.handlers.{GrpcGatewayHandler, SwaggerHandler}
import io.netty.bootstrap.ServerBootstrap
import io.netty.channel.ChannelInitializer
import io.netty.channel.nio.NioEventLoopGroup
import io.netty.channel.socket.SocketChannel
import io.netty.channel.socket.nio.NioServerSocketChannel
import io.netty.handler.codec.http.{HttpObjectAggregator, HttpServerCodec}
case class GrpcGatewayServerBuilder(
port: Int = 80,
services: Seq[GrpcGatewayHandler] = Nil
) {
def forPort(port: Int): GrpcGatewayServerBuilder = {
copy(port = port)
}
def addService(service: GrpcGatewayHandler): GrpcGatewayServerBuilder = {
copy(services = services :+ service)
}
def build(): GrpcGatewayServer = {
val masterGroup = new NioEventLoopGroup()
val slaveGroup = new NioEventLoopGroup()
val bootstrap = new ServerBootstrap()
bootstrap
.group(masterGroup, slaveGroup)
.channel(classOf[NioServerSocketChannel])
.childHandler(new ChannelInitializer[SocketChannel] {
override def initChannel(ch: SocketChannel): Unit = {
ch.pipeline().addLast("codec", new HttpServerCodec())
ch.pipeline().addLast("aggregator", new HttpObjectAggregator(512 * 1024))
ch.pipeline().addLast("swagger", new SwaggerHandler)
services.foreach { handler =>
ch.pipeline().addLast(handler.name, handler)
}
}
})
new GrpcGatewayServer(port, bootstrap, masterGroup, slaveGroup, services.toList)
}
}
object GrpcGatewayServerBuilder {
def forPort(port: Int): GrpcGatewayServerBuilder =
new GrpcGatewayServerBuilder().forPort(port)
def addService(service: GrpcGatewayHandler): GrpcGatewayServerBuilder =
new GrpcGatewayServerBuilder().addService(service)
}
示例8: init
//设置package包名称以及导入依赖的类
package org.nerver.core.server
import io.netty.bootstrap.ServerBootstrap
import io.netty.channel.ChannelOption
import io.netty.channel.nio.NioEventLoopGroup
import io.netty.channel.socket.nio.NioServerSocketChannel
import io.netty.handler.logging.{LogLevel, LoggingHandler}
import org.nerver.core.HandlerScanner
import org.nerver.core.annotation.Method
import org.nerver.core.handler.HttpServerHandlerInitializer
def init(): Unit = {
//configure server
val bossGroup = new NioEventLoopGroup()
val workGroup = new NioEventLoopGroup()
try {
val serverBootstrap = new ServerBootstrap()
serverBootstrap.option[java.lang.Integer](ChannelOption.SO_BACKLOG, 1024)
serverBootstrap.childOption[java.lang.Boolean](ChannelOption.TCP_NODELAY, true)
serverBootstrap.childOption[java.lang.Boolean](ChannelOption.SO_KEEPALIVE, true)
serverBootstrap.group(bossGroup, workGroup)
.channel(classOf[NioServerSocketChannel])
.handler(new LoggingHandler(LogLevel.INFO))
.childHandler(new HttpServerHandlerInitializer())
// TODO: add http handler
val channel = serverBootstrap.bind(port).sync().channel()
channel.closeFuture().sync()
} finally {
bossGroup.shutdownGracefully()
workGroup.shutdownGracefully()
}
}
}
object BaseServer {
val getMethodMap: scala.collection.mutable.HashMap[String, java.lang.reflect.Method] = scala.collection.mutable.HashMap()
val postMethodMap: scala.collection.mutable.HashMap[String, java.lang.reflect.Method] = scala.collection.mutable.HashMap()
val getHandlerMap: scala.collection.mutable.HashMap[String, java.lang.Class[_]] = scala.collection.mutable.HashMap()
val postHandlerMap: scala.collection.mutable.HashMap[String, java.lang.Class[_]] = scala.collection.mutable.HashMap()
val typeMap = scala.collection.mutable.HashMap(Method.GET -> getHandlerMap, Method.POST -> postHandlerMap)
val methodTypeMap = scala.collection.mutable.HashMap(Method.GET -> getMethodMap, Method.POST -> postMethodMap)
def main(args: Array[String]): Unit = {
val handlerScanner = new HandlerScanner(args(1))
handlerScanner.init()
val baseServer = new BaseServer(Integer.parseInt(args(0)))
baseServer.init()
}
}