Skip to content

Commit

Permalink
Reduce work in doPost by moving declareChannel to pool implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
eldss committed Nov 18, 2020
1 parent 5979648 commit 184d5ef
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 12 deletions.
17 changes: 7 additions & 10 deletions src/main/java/SkierServlet.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.pool2.ObjectPool;
import org.apache.commons.pool2.impl.GenericObjectPool;
import org.json.JSONException;
Expand All @@ -43,7 +42,9 @@ public void init(ServletConfig config) throws ServletException {

// Connection setup
ConnectionFactory factory = new ConnectionFactory();
factory.setHost("localhost");
//factory.setHost("3.82.190.158");
factory.setHost("localhost"); // for local testing

try {
rmqConn = factory.newConnection();
} catch (IOException | TimeoutException e) {
Expand All @@ -52,7 +53,7 @@ public void init(ServletConfig config) throws ServletException {
}

// Channel pool setup
channelPool= new GenericObjectPool<>(new RabbitMQChannelFactory(rmqConn));
channelPool= new GenericObjectPool<>(new RabbitMQChannelFactory(rmqConn, QUEUE_NAME));
}

/**
Expand All @@ -65,7 +66,7 @@ public void destroy() {
e.printStackTrace();
}
try {
channelPool.clear();
channelPool.close();
} catch (Exception e) {
e.printStackTrace();
}
Expand Down Expand Up @@ -119,24 +120,20 @@ protected void doPost(HttpServletRequest request, HttpServletResponse response)
try {
// Get and declare a channel
Channel channel = channelPool.borrowObject();
//Channel channel = rmqConn.createChannel();
channel.queueDeclare(QUEUE_NAME, false, false, false, null);

// Keep the message as simple as possible, just the arguments.
// Consumers will know the schema here.
String message = String.format(
"%s,%s,%s,%s,%s", resort, day, skier, time, lift);
// Send it
channel.basicPublish("", QUEUE_NAME, null, message.getBytes());

// Return the channel to the pool
channelPool.returnObject(channel);

// Send a successful response
response.setStatus(HttpServletResponse.SC_CREATED);

} catch (SQLException e) {
response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
out.println(JsonFormatter.buildError("problem executing SQL: "
+ e.getMessage())); // For development
} catch (Exception e) {
// Error with RabbitMQ
response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
Expand Down
8 changes: 6 additions & 2 deletions src/main/java/utils/RabbitMQChannelFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,19 @@ public class RabbitMQChannelFactory
extends BasePooledObjectFactory<Channel> {

private final Connection conn;
private final String queueName;

public RabbitMQChannelFactory(Connection conn) {
public RabbitMQChannelFactory(Connection conn, String queueName) {
super();
this.conn = conn;
this.queueName = queueName;
}

@Override
public Channel create() throws Exception {
return conn.createChannel();
Channel chan = conn.createChannel();
chan.queueDeclare(queueName, false, false, false, null);
return chan;
}

@Override
Expand Down

0 comments on commit 184d5ef

Please sign in to comment.