1 package dst.ass3.messaging.impl;
3 import com.fasterxml.jackson.databind.ObjectMapper;
4 import com.rabbitmq.client.Channel;
5 import com.rabbitmq.client.Connection;
6 import com.rabbitmq.client.ConnectionFactory;
7 import com.rabbitmq.client.MessageProperties;
8 import dst.ass3.messaging.Constants;
9 import dst.ass3.messaging.IRequestGateway;
10 import dst.ass3.messaging.UploadRequest;
12 import java.io.IOException;
13 import java.util.concurrent.TimeoutException;
15 public class RequestGateway implements IRequestGateway {
19 public RequestGateway() {
20 ConnectionFactory factory = new ConnectionFactory();
21 factory.setHost(Constants.RMQ_HOST);
22 factory.setPort(Integer.parseInt(Constants.RMQ_PORT));
23 factory.setUsername(Constants.RMQ_USER);
24 factory.setPassword(Constants.RMQ_PASSWORD);
27 conn = factory.newConnection();
28 channel = conn.createChannel();
29 } catch (IOException | TimeoutException e) {
30 throw new RuntimeException("Irrecoverable error", e); // Fail horribly
35 public void uploadRequest(UploadRequest request) {
36 String routingKey = null;
37 switch (request.getType()) {
39 routingKey = Constants.QUEUE_DOCUMENT;
42 routingKey = Constants.QUEUE_QUIZ;
45 routingKey = Constants.QUEUE_VIDEO;
49 ObjectMapper mapper = new ObjectMapper();
51 channel.basicPublish("", routingKey, false, false, MessageProperties.BASIC, mapper.writeValueAsBytes(request));
52 } catch (IOException e) {
53 e.printStackTrace(); // TODO: may be recoverable?
58 public void close() throws IOException {
61 } catch (TimeoutException ignored) {