private ConcurrentHashMap<Long, Book> seedData() { ConcurrentHashMap<Long, Book> bookMap = new ConcurrentHashMap<Long, Book>(); Book book = new Book(); book.setIsbn(1); book.setCategory("computer"); book.setTitle("Java Concurrency in Practice"); try { book.setCoverimage(new URL("http://goo.gl/N96GJN")); } catch (MalformedURLException e) { // eat the exception } bookMap.put(book.getIsbn(), book); book = new Book(); book.setIsbn(2); book.setCategory("computer"); book.setTitle("Restful Web Services"); try { book.setCoverimage(new URL("http://goo.gl/ZGmzoJ")); } catch (MalformedURLException e) { // eat the exception } bookMap.put(book.getIsbn(), book); return bookMap; }
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(); } } }