Skip to content

Commit

Permalink
[#noissue] Add jetty plugin testweb
Browse files Browse the repository at this point in the history
  • Loading branch information
jaehong-kim committed Jun 21, 2022
1 parent 8cdd56c commit 1fb4c26
Show file tree
Hide file tree
Showing 6 changed files with 168 additions and 0 deletions.
13 changes: 13 additions & 0 deletions agent-testweb/jetty-plugin-testweb/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@

## Install
```
$ mvn install
```

## Run
```
$ mvn exec:java -Dexec.mainClass="com.pinpoint.test.plugin.JettyServerStarterMain"
```
You can then access here: http://localhost:18080/

## Stop
47 changes: 47 additions & 0 deletions agent-testweb/jetty-plugin-testweb/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?xml version="1.0" encoding="UTF-8"?>
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.navercorp.pinpoint</groupId>
<artifactId>pinpoint-agent-testweb</artifactId>
<version>2.5.0-SNAPSHOT</version>
</parent>

<artifactId>pinpoint-jetty-plugin-testweb</artifactId>

<packaging>jar</packaging>

<properties>
<pinpoint.agent.jvmargument>
${pinpoint.agent.default.jvmargument}
</pinpoint.agent.jvmargument>
<jetty.version>9.4.42.v20210604</jetty.version>
</properties>

<dependencies>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-server</artifactId>
<version>${jetty.version}</version>
<exclusions>
<exclusion>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-servlet</artifactId>
<version>${jetty.version}</version>
</dependency>
</dependencies>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package com.pinpoint.test.plugin;

import javax.servlet.AsyncContext;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.WriteListener;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;

public class AsyncServlet extends HttpServlet {
private static final String HEAVY_RESOURCE = "This is some heavy resource that will be served in an async way";

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
ByteBuffer content = ByteBuffer.wrap(HEAVY_RESOURCE.getBytes(StandardCharsets.UTF_8));

AsyncContext async = request.startAsync();
ServletOutputStream out = response.getOutputStream();
out.setWriteListener(new WriteListener() {
@Override
public void onWritePossible() throws IOException {
while (out.isReady()) {
if (!content.hasRemaining()) {
response.setStatus(200);
async.complete();
return;
}
out.write(content.get());
}
}

@Override
public void onError(Throwable t) {
getServletContext().log("Async Error", t);
async.complete();
}
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.pinpoint.test.plugin;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class BlockingServlet extends HttpServlet {

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("application/json");
response.setStatus(HttpServletResponse.SC_OK);
response.getWriter().println("{ \"status\": \"ok\"}");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package com.pinpoint.test.plugin;

import org.eclipse.jetty.server.Connector;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.ServerConnector;
import org.eclipse.jetty.servlet.ServletHandler;
import org.eclipse.jetty.util.thread.QueuedThreadPool;

public class JettyServerStarterMain {

private Server server;

void start() throws Exception {

int maxThreads = 10;
int minThreads = 3;
int idleTimeout = 30;

QueuedThreadPool threadPool = new QueuedThreadPool(maxThreads, minThreads, idleTimeout);

server = new Server(threadPool);
ServerConnector connector = new ServerConnector(server);
connector.setPort(18080);
server.setConnectors(new Connector[]{connector});

ServletHandler servletHandler = new ServletHandler();
server.setHandler(servletHandler);

servletHandler.addServletWithMapping(BlockingServlet.class, "/status");
servletHandler.addServletWithMapping(AsyncServlet.class, "/async");

server.start();
}

void stop() throws Exception {
if (server != null) {
server.stop();
}
}

public static void main(String[] args) {
JettyServerStarterMain starter = new JettyServerStarterMain();
try {
starter.start();
} catch (Exception e) {
e.printStackTrace();
}
}
}
1 change: 1 addition & 0 deletions agent-testweb/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@
<module>postgresql-plugin-testweb</module>
<module>undertow-plugin-testweb</module>
<module>redis-lettuce-plugin-testweb</module>
<module>jetty-plugin-testweb</module>
</modules>

<dependencyManagement>
Expand Down

0 comments on commit 1fb4c26

Please sign in to comment.