Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Java Asynchronous web programming using Java

  1. Apr 7, 2017 #1

    ShayanJ

    User Avatar
    Gold Member

    I want to learn web programming with Java. I'm using eclipse neon with tomcat 8.5.13. My question is do these tools support asynchronous programming and server push technologies or should I use other tools?
    Thanks
     
  2. jcsd
  3. Apr 7, 2017 #2

    jedishrfu

    Staff: Mentor

    NodeJS is a popular alternative to Tomcat and Java. Everything client and server side is done in javascript. Its asynchronous and scalable. Many sites rely on NodeJS too.
     
  4. Apr 7, 2017 #3

    ShayanJ

    User Avatar
    Gold Member

    I know about NodeJS but I want to learn Java web programming. I'm sure there is an implementation of asynchronous web programming for Java too, isn't there?
     
  5. Apr 7, 2017 #4

    jedishrfu

    Staff: Mentor

    Ahh okay.

    So when you say server push, you really mean the client sends a request and once the server responds the client immediately sends another request for update. This allows the server to respond whenever and it has a port to do it.

    https://en.wikipedia.org/wiki/Push_technology
     
  6. Apr 7, 2017 #5
    There is a simple async API built into Java in the form of AsynchronousSocketChannel and AsynchronousServerSocketChannel. Those are already sufficient to implement server push. But there are also more full featured libraries like e.g. Netty.
     
  7. Apr 8, 2017 #6

    ShayanJ

    User Avatar
    Gold Member

    I downloaded Netty and tried to run an example. So I created a project with the code below(got it from here):
    Code (Java):

    //package io.netty.example.discard;

    import io.netty.buffer.ByteBuf;

    import io.netty.channel.ChannelHandlerContext;
    import io.netty.channel.ChannelInboundHandlerAdapter;

    /**
    * Handles a server-side channel.
    */

    public class DiscardServerHandler extends ChannelInboundHandlerAdapter { // (1)

        @Override
        public void channelRead(ChannelHandlerContext ctx, Object msg) { // (2)
            // Discard the received data silently.
            ((ByteBuf) msg).release(); // (3)
        }

        @Override
        public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) { // (4)
            // Close the connection when an exception is raised.
            cause.printStackTrace();
            ctx.close();
        }
    }
    But when I run it with tomcat, I get:
    P.S.
    I commented package io.netty.example.discard; because it gives the error:
     
  8. Apr 8, 2017 #7
    I don't have a tomcat installation. But I did run the example code on http://netty.io/wiki/user-guide-for-4.x.html directly with
    and it worked fine. There must be some error in your tomcat configuration.
    Btw. There are many libraries you could use. It doesn't have to be netty. A quick google search for "netty alternatives" will give you several other options. You could even just use the built in classes if your task is relatively simple.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?
Draft saved Draft deleted



Similar Discussions: Asynchronous web programming using Java
  1. Java programs (Replies: 9)

  2. Java Programming (Replies: 12)

  3. Programming using Java (Replies: 3)

Loading...