brikis98 / ping-play

BigPipe streaming for the Play Framework
MIT License
307 stars 60 forks source link

get /sample-app-common/src/main/scala/data in Java ? #27

Closed shay-te closed 8 years ago

shay-te commented 8 years ago

hi, i want to learn more about ping-play, so i want to place sample-app-common from the example inside my java application, since i don't have experience in scala. i am a bit struggling with the conversion, is it possible to have also a java version ?

thanks

brikis98 commented 8 years ago

See sample-app-java for a sample app written in Java.

shay-te commented 8 years ago

hey, thanks. i was referring to the common files here https://github.com/brikis98/ping-play/tree/master/sample-app-common/src/main/scala/data

brikis98 commented 8 years ago

Ah, that code is intentionally in a single place to try to reduce duplication. I'm not sure I'd want to convert it to Java and have to maintain both separately...

shay-te commented 8 years ago

hmmm.... i understand :-) it will be more easy for me, as one that don't know scala. i have a bit of difficulties converting to java.

brikis98 commented 8 years ago

What specifically are you struggling with?

shay-te commented 8 years ago

FutureUtil has lots of errors when i am trying to convert it, like he can't recognize Feature, even when he is ok with the import import scala.concurrent.Future;. i can't get the timeout method signature right... after trying to convert to java, the first and last methods results the same signature.

i don't know how to convert "Response" to java. since i am not a scala developer. i can guess case class Response(id: String, delay: Long) means

public class Response {

    private String m_id;
    private Long m_delay;

    public Response(String id, Long delay) {
        m_id    = id;
        m_delay = delay;
    }
}

as for

object Response {
  implicit val responseWrites = Json.writes[Response]
} 

i have no idea, :) well ill have to learn scala for that

brikis98 commented 8 years ago

First, just to confirm, are you converting all of this stuff as a learning project? Because if you're using ping-play for a real-world use case, you don't need any of the code from the sample apps; you would swap them out with libraries that do real work, such as calling an actual remote service instead of using FutureUtil, which just returns fixed data.

That said, there are a quite a few issues with your conversion.

First, Future should typically only be used with Scala code. For Java code, you should be using Promise. The Java sample app I linked earlier does exactly that.

Second, a case class is just (wonderful) syntactic sugar for a standard Java POJO, with auto generated getters, setters, equals, hashCode, and lots of other goodies. The rough equivalent of the Response class would be something like (with most of the goodies omitted, since you probably don't need them for a simple example):

public class Response {
  public final String id;
  public final long delay;

  public Response(String id, long delay) {
    this.id = id;
    this.delay = delay;
  }

Finally, the Json.writes bit is the way Play does JSON serialization for Scala apps. For Java apps, it's done differently: https://playframework.com/documentation/2.4.x/JavaJsonActions

shay-te commented 8 years ago

thank for the answers, and the time, i want it for a real project. so i guess for a real project i just have to replace this ,,, F.Promise profilePromise = serviceClient.fakeRemoteCallMedium("profile") ,,, With calling some other entry in the controller, and get the result as a promise object.

brikis98 commented 8 years ago

Yes. For example, you could use Play's Web Services Client:

F.Promise<WSResponse> examplePromise = WS.url("http://www.example.com").get()
shay-te commented 8 years ago

thanks for the response. i am trying to convert some other entry to piglet, can you help me please i am trying something much simples

promiseFoo.map(views.html.foo) what should i transfer to the map also return ok(HtmlStreamHelper.toChunks(views.stream.application.apply(bigPipe, profile))); views.stream.application.apply? when trying to convert from withBigPipe example

public class Application extends Controller {

    public Result foo() {
        return ok("Got request " + request() + "!");
    }

    public Result index() {

        Promise<Result> promiseFoo = Promise.promise(new F.Function0<Result>() {
            @Override
            public Result apply() throws Throwable {
                return foo();
            }
        });

        Pagelet profile = new HtmlPagelet("profile", promiseFoo.map(views.html.foo));

        BigPipe bigPipe = new BigPipe(PageletRenderOptions.ClientSide, profile);
        return ok(HtmlStreamHelper.toChunks(views.stream.application.apply(bigPipe, profile)));
    }

}
brikis98 commented 8 years ago

And what happens when you do that?

shay-te commented 8 years ago

(inside eclipse) views.html.foo cannot be resolved also views.stream cannot be resolved

i think i am missing something :)

brikis98 commented 8 years ago

In your example, you used views.stream.application. This is referencing a file application.scala.stream. Do you have such a file?

If you're using views.html.foo, then you would need an application.scala.html.

shay-te commented 8 years ago

in my example i was trying to stick to the WithBigPipe example as possible so i 'kind of' trying to replace the template and the controller names with mine, which docent make sense. i don't have application.scala.stream, i called it same as in the example. just replaces withBigPipe with application

this is what i am asking, how to do this right, i will say that ill trying to stick with java as much as possible

thank you for you patient !!

brikis98 commented 8 years ago

It seems that the things you're struggling with are not ping-play specific, but basic Play problems. Out of curiosity, have you used Play before? Do you understand how templates get compiled into classes? BigPipe streaming is a somewhat advanced topic, so if you're new to Play, it's probably not the best way to get started.

shay-te commented 8 years ago

i have been using play 1.2, and its my more strong side . and i do understand big pipe as a web optimization. i am not sure big-pipe advantage is only by taking pagelets from other sites. (using web service) the main use of it is to compile parts of same site. those entry should also be available to ajax calls, and not be restricted only to "pagelet" loading. i guess i don't understand something here. or most of the code is scala. so its hard for me to learn from it. any way. if i am a hard nut. it's ok, ill stragle with it till ill get it :)

brikis98 commented 8 years ago

Play 2 is very different than Play 1. It was a complete rewrite from the ground up. Before diving into ping-play, please spend some time going through the Play 2 documentation (especially Play 2 for Java Developers) until you get a feel for how the built-in controllers, libraries, and templating mechanism work. Pay special attention to how templates are compiled. You can also find examples in Typesafe Activator.

Once you get a feel for that, come back to this project, and I suspect you'll know how to solve the issues you're seeing now.

shay-te commented 8 years ago

thanks