GithubHelp home page GithubHelp logo

nifty's Introduction

Nifty

Nifty is an implementation of Thrift clients and servers on Netty.

It is also the implementation used by Swift.

Examples

To create a basic Thrift server using Nifty, use the Thrift 0.9.0 code generator to generate Java stub code, write a Handler for your service interface, and pass it to Nifty like this:

public void startServer() {
    // Create the handler
    MyService.Iface serviceInterface = new MyServiceHandler();

    // Create the processor
    TProcessor processor = new MyService.Processor<>(serviceInterface);

    // Build the server definition
    ThriftServerDef serverDef = new ThriftServerDefBuilder().withProcessor(processor)
                                                            .build();

    // Create the server transport
    final NettyServerTransport server = new NettyServerTransport(serverDef,
                                                                 new NettyConfigBuilder(),
                                                                 new DefaultChannelGroup(),
                                                                 new HashedWheelTimer());

    // Create netty boss and executor thread pools
    ExecutorService bossExecutor = Executors.newCachedThreadPool();
    ExecutorService workerExecutor = Executors.newCachedThreadPool();

    // Start the server
    server.start(bossExecutor, workerExecutor);

    // Arrange to stop the server at shutdown
    Runtime.getRuntime().addShutdownHook(new Thread() {
        @Override
        public void run() {
            try {
                server.stop();
            } catch (InterruptedException e) {
                Thread.currentThread().interrupt();
            }
        }
    });
}

Or the same thing using guice:

public void startGuiceServer() {
    final NiftyBootstrap bootstrap = Guice.createInjector(
        Stage.PRODUCTION,
        new NiftyModule() {
            @Override
            protected void configureNifty() {
                // Create the handler
                MyService.Iface serviceInterface = new MyServiceHandler();

                // Create the processor
                TProcessor processor = new MyService.Processor<>(serviceInterface);

                // Build the server definition
                ThriftServerDef serverDef = new ThriftServerDefBuilder().withProcessor(processor)
                                                                        .build();

                // Bind the definition
                bind().toInstance(serverDef);
            }
        }).getInstance(NiftyBootstrap.class);

    // Start the server
    bootstrap.start();

    // Arrange to stop the server at shutdown
    Runtime.getRuntime().addShutdownHook(new Thread() {
        @Override
        public void run() {
            bootstrap.stop();
        }
    });
}

nifty's People

Contributors

andrewcox avatar dain avatar jaxlaw avatar hgschmie avatar tarikozket avatar

Watchers

James Cloos avatar baeeq avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.