本文整理汇总了Scala中io.netty.channel.EventLoopGroup类的典型用法代码示例。如果您正苦于以下问题:Scala EventLoopGroup类的具体用法?Scala EventLoopGroup怎么用?Scala EventLoopGroup使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了EventLoopGroup类的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Scala代码示例。
示例1: 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])
}
}
}
示例2: 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();
}
}
示例3: GrpcGatewayServer
//设置package包名称以及导入依赖的类
package grpcgateway.server
import grpcgateway.handlers.GrpcGatewayHandler
import io.netty.bootstrap.ServerBootstrap
import io.netty.channel.{ChannelFuture, EventLoopGroup}
class GrpcGatewayServer private[server] (
port: Int,
bootstrap: ServerBootstrap,
masterGroup: EventLoopGroup,
slaveGroup: EventLoopGroup,
services: List[GrpcGatewayHandler]
) {
private var channel: Option[ChannelFuture] = None
def start(): Unit = {
channel = Option(bootstrap.bind(port).sync())
}
def shutdown(): Unit = {
slaveGroup.shutdownGracefully()
masterGroup.shutdownGracefully()
services.foreach(_.shutdown())
channel.foreach(_.channel().closeFuture().sync())
}
}