• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

Scala EventLoopGroup类代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了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])
    }
  }
} 
开发者ID:madprogrammer,项目名称:scxmppd,代码行数:50,代码来源:Server.scala


示例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();
  }

} 
开发者ID:woshilaiceshide,项目名称:s-server-benchmark,代码行数:47,代码来源:NettyHttpServer.scala


示例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())
    }
} 
开发者ID:btlines,项目名称:grpcgateway,代码行数:27,代码来源:GrpcGatewayServer.scala



注:本文中的io.netty.channel.EventLoopGroup类示例整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Scala ScalaRunTime类代码示例发布时间:2022-05-23
下一篇:
Scala GraphImpl类代码示例发布时间:2022-05-23
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap