Skip to content

Instantly share code, notes, and snippets.

@serban-petrescu
Last active May 21, 2018 13:06
Show Gist options
  • Save serban-petrescu/1b3cb07bb00d0755746151b9f2b75e64 to your computer and use it in GitHub Desktop.
Save serban-petrescu/1b3cb07bb00d0755746151b9f2b75e64 to your computer and use it in GitHub Desktop.
SAP CF Logging Async Servlet bug
package com.example.demo;
import com.sap.hcp.cf.logging.servlet.filter.RequestLoggingFilter;
import org.apache.catalina.Context;
import org.apache.catalina.startup.Tomcat;
import org.apache.tomcat.util.descriptor.web.FilterDef;
import org.apache.tomcat.util.descriptor.web.FilterMap;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.File;
public class DemoApplication {
private static final Logger LOG = LoggerFactory.getLogger(DemoApplication.class);
public static void main(String[] args) {
Tomcat tomcat = new Tomcat();
tomcat.setPort(8080);
File baseDir = new File("tomcat");
tomcat.setBaseDir(baseDir.getAbsolutePath());
File applicationDir = new File(baseDir + "/webapps", "/ROOT");
if (!applicationDir.exists()) {
applicationDir.mkdirs();
}
try {
Context appContext = tomcat.addWebapp("/", "ROOT");
Tomcat.addServlet(appContext, "demoServlet", new DemoServlet()).setAsyncSupported(true);
appContext.addServletMapping("/*", "demoServlet");
FilterDef filterDef = new FilterDef();
filterDef.setAsyncSupported("true");
filterDef.setFilterName("requestFilter");
filterDef.setFilter(new RequestLoggingFilter());
appContext.addFilterDef(filterDef);
FilterMap filterMap = new FilterMap();
filterMap.setDispatcher("ASYNC");
filterMap.setDispatcher("REQUEST");
filterMap.setFilterName("requestFilter");
filterMap.addURLPattern("/*");
appContext.addFilterMap(filterMap);
tomcat.start();
LOG.info("Tomcat server: http://localhost:8080/");
tomcat.getServer().await();
} catch (Exception e) {
LOG.error("Unable to start tomcat.", e);
}
}
}
package com.example.demo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.servlet.AsyncContext;
import javax.servlet.DispatcherType;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;
public class DemoServlet extends HttpServlet {
private static final Logger LOG = LoggerFactory.getLogger(DemoServlet.class);
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) {
if (req.getDispatcherType() == DispatcherType.ASYNC) {
return;
}
try {
LOG.info("Inside sync method.");
AsyncContext context = req.startAsync();
context.start(() -> {
try {
LOG.info("Inside async method.");
Thread.sleep(1000);
HttpServletResponse response = (HttpServletResponse) context.getResponse();
response.setStatus(201);
try (PrintWriter writer = response.getWriter()) {
writer.print("Something");
}
context.dispatch();
} catch (Exception e) {
LOG.error("Unable to process request.", e);
}
});
} catch (Exception e) {
LOG.error("Unable to service request.", e);
}
}
}
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1</version>
<packaging>war</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<tomcat.version>8.5.2</tomcat.version>
<cf-logging-version>2.1.4</cf-logging-version>
</properties>
<dependencies>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-core</artifactId>
<version>${tomcat.version}</version>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<version>${tomcat.version}</version>
</dependency>
<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-jasper</artifactId>
<version>${tomcat.version}</version>
</dependency>
<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-jasper-el</artifactId>
<version>${tomcat.version}</version>
</dependency>
<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-jsp-api</artifactId>
<version>${tomcat.version}</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-logging-log4j</artifactId>
<version>${tomcat.version}</version>
</dependency>
<dependency>
<groupId>com.sap.hcp.cf.logging</groupId>
<artifactId>cf-java-logging-support-servlet</artifactId>
<version>${cf-logging-version}</version>
</dependency>
<dependency>
<groupId>com.sap.hcp.cf.logging</groupId>
<artifactId>cf-java-logging-support-log4j2</artifactId>
<version>${cf-logging-version}</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-slf4j-impl</artifactId>
<version>2.8.2</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.8.2</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
<mainClass>org.junjun.util.spring.AppLauncher</mainClass>
</manifest>
<manifestEntries>
<Class-Path>conf/</Class-Path>
</manifestEntries>
</archive>
<includes>
<include>**/*.class</include>
<include>*.properties.not.configurable</include>
</includes>
</configuration>
</plugin>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<executions>
<execution>
<id>build-distribution</id>
<phase>verify</phase>
<goals>
<goal>attached</goal>
</goals>
</execution>
</executions>
<configuration>
<descriptors>
<descriptor>src/assembly/dist-build.xml</descriptor>
</descriptors>
</configuration>
</plugin>
</plugins>
</build>
</project>

SAP CF Logging Async Servlet Bug

Small example which shows the async servlet logging missmatch.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment