Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

finagle-example: adds an HTTP java example. #470

Closed
wants to merge 2 commits into from

Conversation

@leonmaia
Copy link
Contributor

commented Feb 25, 2016

Problem

Lack of examples to create a Http server using java.

Solution

Build a very minimal example showing how to create a server, some filters, and a very basic routing.

Result

More examples of how to start a java project using Finagle.

package com.twitter.finagle.example.java.http;

public final class CatService {
static CatsDB db = new CatsDB();

This comment has been minimized.

Copy link
@kevinoliver

kevinoliver Mar 8, 2016

Member

can we mark this private final? good to have good code hygiene even in sample code.

import java.util.HashMap;

public class CatsDB {
static HashMap<Integer, Cat> db = new HashMap<>();

This comment has been minimized.

Copy link
@kevinoliver

kevinoliver Mar 8, 2016

Member

same private final here too.

import org.jboss.netty.handler.codec.http.HttpResponseStatus;

/**
* A simple Finagle that intercepts Exceptions and converts them to a more comprehensible HTTP Response.

This comment has been minimized.

Copy link
@kevinoliver

kevinoliver Mar 8, 2016

Member

s/Finagle/Filter/


return resp;
}
in.printStackTrace();

This comment has been minimized.

Copy link
@kevinoliver

kevinoliver Mar 8, 2016

Member

feel free to ignore, but i prefer not teaching people to log, so can we omit this line?


@Override
public Future<Response> apply(Request req, Service<Request, Response> service) {
final Duration e = Stopwatch$.MODULE$.start().apply();

This comment has been minimized.

Copy link
@kevinoliver

kevinoliver Mar 8, 2016

Member

bummer that we don't have a nicer java API for Stopwatch. i'll try to address that for the future.

that said, you should capture the Elapsed from calling start(). then call apply() inside the map function below.


public class HttpServer {

static InetSocketAddress addr = new InetSocketAddress(InetAddress.getLoopbackAddress(), 8888);

This comment has been minimized.

Copy link
@kevinoliver

kevinoliver Mar 8, 2016

Member

private final here too please.

@kevinoliver

This comment has been minimized.

Copy link
Member

commented Mar 8, 2016

@leonmaia this is great — thanks! i just have a few small fixes i'd like to see applied and then would be happy to pull this in.

@mosesn

This comment has been minimized.

Copy link
Contributor

commented Mar 14, 2016

Hey @leonmaia, do you think you'll have a chance to get to this sometime this week? No rush if you're busy 😀

@leonmaia

This comment has been minimized.

Copy link
Contributor Author

commented Mar 15, 2016

Thanks for the feedback @kevinoliver :)

@mosesn sure! Sorry for taking so long :) I was very busy last week.

@mosesn

This comment has been minimized.

Copy link
Contributor

commented Mar 15, 2016

No worries, just wanted to make sure it didn't fall off your plate.

@leonmaia

This comment has been minimized.

Copy link
Contributor Author

commented Mar 17, 2016

I've applied all changes @kevinoliver, please let me know if there's something else. :)

@kevinoliver

This comment has been minimized.

Copy link
Member

commented Mar 17, 2016

@leonmaia lgtm i'll work on getting this merged in now. thanks!

@kevinoliver kevinoliver added the Ship It label Mar 17, 2016

return resp;
}
resp.setStatus(HttpResponseStatus.INTERNAL_SERVER_ERROR);
resp.setContent(ChannelBuffers.wrappedBuffer(in.getMessage().getBytes()));

This comment has been minimized.

Copy link
@luciferous

luciferous Mar 17, 2016

Contributor

I'm surprised this works – I thought finagle-http API was Netty free. Is this about Java subverting Scala access modifiers?

@mosesn

This comment has been minimized.

Copy link
Contributor

commented Mar 18, 2016

@luciferous yes. HttpMessageProxy extends Proxy, which makes the netty Message methods available.

@kevinoliver

This comment has been minimized.

Copy link
Member

commented Mar 18, 2016

Just merged this locally. Should show up on the develop branch this Monday. Thanks again.

@leonmaia leonmaia deleted the leonmaia:http-java-example branch Nov 2, 2017

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
4 participants
You can’t perform that action at this time.