Skip to content

Commit

Permalink
Message passing in RabbitMQ to seperate process successful
Browse files Browse the repository at this point in the history
  • Loading branch information
eldss committed Nov 18, 2020
1 parent 006e3d3 commit 64245f8
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 2 deletions.
2 changes: 1 addition & 1 deletion .idea/artifacts/server_war_exploded.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,12 @@
<artifactId>slf4j-api</artifactId>
<version>1.7.30</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-pool2 -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
<version>2.9.0</version>
</dependency>
</dependencies>

</project>
42 changes: 41 additions & 1 deletion src/main/java/SkierServlet.java
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import data.SkierDbConnection;
Expand All @@ -17,9 +18,13 @@
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;
import org.json.JSONObject;
import utils.JsonFormatter;
import utils.RabbitMQChannelFactory;

@WebServlet(name = "SkierServlet")
public class SkierServlet extends HttpServlet {
Expand All @@ -28,12 +33,15 @@ public class SkierServlet extends HttpServlet {
// RabbitMQ objects
private final static String QUEUE_NAME = "DB_POST";
private Connection rmqConn;
private ObjectPool<Channel> channelPool;

/**
* Used to set up RabbitMQ. Does not use the config object.
*/
public void init(ServletConfig config) throws ServletException {
super.init(config);

// Connection setup
ConnectionFactory factory = new ConnectionFactory();
factory.setHost("localhost");
try {
Expand All @@ -42,14 +50,25 @@ public void init(ServletConfig config) throws ServletException {
e.printStackTrace();
throw new ServletException("couldn't connect to RabbitMQ");
}

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

/**
* Cleans up RabbitMQ leftovers.
*/
public void destroy() {
try {
rmqConn.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
channelPool.clear();
} catch (Exception e) {
e.printStackTrace();
}
}

/**
Expand Down Expand Up @@ -93,15 +112,36 @@ protected void doPost(HttpServletRequest request, HttpServletResponse response)
int time = requestJson.getInt(keys[3]);
int lift = requestJson.getInt(keys[4]);

// Write the response
// Do the request and write response
PrintWriter out = response.getWriter();
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 %d %d %d %d", resort, day, skier, time, lift);
// Send it
channel.basicPublish("", QUEUE_NAME, null, message.getBytes());
channelPool.returnObject(channel);

// TODO: Move db processing to consumer program
dbConn.postLiftRide(resort, day, skier, time, lift);

// 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);
out.println(JsonFormatter.buildError("problem pushing to worker queue: "
+ e.getMessage())); // For development
}
}

Expand Down
31 changes: 31 additions & 0 deletions src/main/java/utils/RabbitMQChannelFactory.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package utils;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import org.apache.commons.pool2.BasePooledObjectFactory;
import org.apache.commons.pool2.PooledObject;
import org.apache.commons.pool2.impl.DefaultPooledObject;

/**
* Provides a connection pool for RabbitMQ channel objects.
*/
public class RabbitMQChannelFactory
extends BasePooledObjectFactory<Channel> {

private final Connection conn;

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

@Override
public Channel create() throws Exception {
return conn.createChannel();
}

@Override
public PooledObject<Channel> wrap(Channel channel) {
return new DefaultPooledObject<>(channel);
}
}

0 comments on commit 64245f8

Please sign in to comment.