public void producer(String msgData) throws JMSException {
    StompJmsConnectionFactory factory = new StompJmsConnectionFactory();
    factory.setBrokerURI("tcp://" + apolloHost + ":" + apolloPort);
    System.out.println(factory.getBrokerURI());

    Connection connection = factory.createConnection(apolloUser, apolloPassword);
    connection.start();
    Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
    Destination dest = new StompJmsDestination(stompQueue);
    MessageProducer producer = session.createProducer(dest);
    System.out.println("Sending messages to " + stompQueue + "...");

    TextMessage msg = session.createTextMessage(msgData);
    msg.setLongProperty("id", System.currentTimeMillis());
    System.out.println(msg.getText());
    producer.send(msg);
    // producer.send(session.createTextMessage("SHUTDOWN"));
    connection.close();
  }
  @Override
  public void run(LibraryServiceConfiguration configuration, Environment environment)
      throws Exception {
    // This is how you pull the configurations from library_x_config.yml
    String queueName = configuration.getStompQueueName();
    String topicName = configuration.getStompTopicName();
    String instanceName = configuration.getInstanceName();
    String user = configuration.getApolloUser();
    String password = configuration.getApolloPassword();
    String host = configuration.getApolloHost();
    int port = Integer.parseInt(configuration.getApolloPort());
    log.debug("Queue name is {}. Topic name is {}", queueName, topicName);
    // TODO: Apollo STOMP Broker URL and login

    StompJmsConnectionFactory factory = new StompJmsConnectionFactory();
    factory.setBrokerURI("tcp://54.215.210.214:61613");

    Connection connection = factory.createConnection(user, password);
    connection.start();

    /** Root API */
    environment.addResource(RootResource.class);
    /** Books APIs */
    BookRepositoryInterface bookRepository = new BookRepository();
    environment.addResource(
        new BookResource(
            bookRepository,
            queueName,
            topicName,
            instanceName,
            user,
            password,
            host,
            port,
            connection));

    /** UI Resources */
    environment.addResource(new HomeResource(bookRepository));
  }
  public Runnable listener() throws JMSException {
    long isbn;
    String bookTitle;
    String bookCategory;
    String webURL;
    Book tempBook = new Book();
    ArrayList<String> arrivals = new ArrayList<String>();
    StompJmsConnectionFactory factory = new StompJmsConnectionFactory();
    factory.setBrokerURI("tcp://" + apolloHost + ":" + apolloPort);
    System.currentTimeMillis();
    // System.out.println("Waiting for messages...");
    while (true) {
      Connection connection = factory.createConnection(apolloUser, apolloPassword);
      connection.start();
      Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
      Destination dest = new StompJmsDestination(stompTopic);
      MessageConsumer consumer = session.createConsumer(dest);
      while (true) {
        Message msg = consumer.receive(500);
        if (msg == null) break;
        if (msg instanceof TextMessage) {
          String body = ((TextMessage) msg).getText();
          arrivals.add(body);

        } else {
          System.out.println("Unexpected message type: " + msg.getClass());
        }
      }
      connection.close();
      if (!arrivals.isEmpty()) {
        for (String arrival : arrivals) {
          isbn = Long.parseLong(arrival.split(":")[0]);
          bookTitle = arrival.split(":")[1].replaceAll("^\"|\"$", "");
          bookCategory = arrival.split(":")[2].replaceAll("^\"|\"$", "");
          webURL = arrival.split(":\"")[3];
          webURL = webURL.substring(0, webURL.length() - 1);
          tempBook = bookRepository.getBookByISBN(isbn);
          // System.out.println("tempBook is "+tempBook);
          // System.out.println("dummyBook is "+dummyBook);

          if (tempBook.getIsbn() == 0) {
            // System.out.println("reachable");
            tempBook.setIsbn(isbn);
            tempBook.setCategory(bookCategory);
            tempBook.setTitle(bookTitle);
            try {
              tempBook.setCoverimage(new URL(webURL));
            } catch (MalformedURLException e) {
              e.printStackTrace();
            }
            bookRepository.addBook(tempBook);

          } else {
            // System.out.println("reachable, changing to available");
            tempBook.setStatus(Status.available);
          }
        }
        arrivals.clear();
      }
    }
  }