Asynchronous web programming using Java

In summary, 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.
  • #1
ShayanJ
Insights Author
Gold Member
2,810
604
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
  • #2
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.
 
  • #3
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?
 
  • #4
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
 
  • #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.
 
  • #6
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 ""
 
  • #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
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.
 

What is asynchronous web programming?

Asynchronous web programming is a programming technique where multiple tasks can be executed simultaneously without waiting for the previous task to finish. This allows for better performance and user experience, as the program can continue to run while waiting for certain tasks to complete.

Why is Java a popular language for asynchronous web programming?

Java is a popular language for asynchronous web programming because it is a robust and scalable language that can handle multiple tasks and processes efficiently. It also has a large community and a vast array of libraries and frameworks that support asynchronous programming.

What are the benefits of using asynchronous web programming in Java?

One of the main benefits of using asynchronous web programming in Java is improved performance. By allowing tasks to run simultaneously, the program can handle a higher volume of requests and process them more efficiently. This can also lead to a better user experience, as the program can continue to run and respond to user input while completing other tasks.

What are some common challenges in asynchronous web programming using Java?

Some common challenges in asynchronous web programming using Java include managing the flow of data between tasks, handling errors and exceptions, and debugging asynchrony-related issues. It is also important to properly handle thread management and synchronization to avoid potential issues such as race conditions.

How can I learn more about asynchronous web programming using Java?

There are many resources available to learn about asynchronous web programming using Java, including online tutorials, books, and courses. It is also helpful to practice and experiment with different techniques and frameworks to gain hands-on experience. Additionally, joining online communities or attending meetups and conferences can provide valuable insights and networking opportunities.

Similar threads

  • Programming and Computer Science
Replies
6
Views
1K
  • Programming and Computer Science
Replies
15
Views
1K
  • Programming and Computer Science
Replies
17
Views
1K
  • Programming and Computer Science
Replies
33
Views
2K
  • Programming and Computer Science
Replies
1
Views
686
  • Programming and Computer Science
Replies
4
Views
1K
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
7
Views
480
  • Programming and Computer Science
Replies
5
Views
1K
  • Programming and Computer Science
Replies
8
Views
3K
Back
Top