Win a copy of 97 Things Every Java Programmer Should Know this week in the Java in General forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Jeanne Boyarsky
  • Junilu Lacar
  • Henry Wong
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Frits Walraven
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • salvin francis
  • fred rosenberger

Piotr's mock exam question 67, onError() is not called.

 
Ranch Foreman
Posts: 1898
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


package com.nullhaus;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;

public class MyListener implements AsyncListener {
public void onComplete(AsyncEvent event) {
System.out.println("#Async listener [onComplete]");
}

public void onError(AsyncEvent event) {
System.out.println("#Async listener [onError]");
}

public void onStartAsync(AsyncEvent event) {
System.out.println("#Async listener [onStartAsync]");
}

public void onTimeout(AsyncEvent event) {
System.out.println("#Async listener [onTimeout]");
}
}



package com.nullhaus;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;

@WebServlet(urlPatterns = "/foo/*",
name="NullServlet",
asyncSupported=true)
public class NullServlet extends HttpServlet {
public void doGet(HttpServletRequest req,
HttpServletResponse resp)
throws ServletException {

final AsyncContext ac = req.startAsync();

try {
Class lClass = Class.forName("com.nullhaus.MyListener");
AsyncListener al = ac.createListener(lClass);

ac.addListener(al);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}

ac.setTimeout(3000);

ac.start(new Thread() {
public void run() {
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
ac.complete();
}
});
}
}



According to the answer, onError() will be called.
But when I ran it with Tomcat 8.0, it throws IllegalStateException , but the onError() is not called.
 
It will give me the powers of the gods. Not bad for a tiny ad:
Devious Experiments for a Truly Passive Greenhouse!
https://www.kickstarter.com/projects/paulwheaton/greenhouse-1
    Bookmark Topic Watch Topic
  • New Topic