├── .gitignore ├── 0-introduction ├── README.md └── students │ └── README.md ├── 1-threads-basics ├── README.md ├── solution │ ├── pom.xml │ └── src │ │ ├── exercise1 │ │ └── Main.java │ │ ├── exercise2 │ │ ├── Main.java │ │ └── MyThread.java │ │ ├── exercise3 │ │ ├── Main.java │ │ └── MyRunnable.java │ │ └── exercise4 │ │ ├── Main.java │ │ └── MyRunnable.java └── students │ ├── pom.xml │ └── src │ ├── exercise1 │ └── Main.java │ ├── exercise2 │ ├── Main.java │ └── MyThread.java │ ├── exercise3 │ ├── Main.java │ └── MyRunnable.java │ └── exercise4 │ ├── Main.java │ └── MyRunnable.java ├── 10-rest-web-service ├── README.md ├── solution │ └── restful │ │ ├── books │ │ ├── pom.xml │ │ └── src │ │ │ └── main │ │ │ ├── java │ │ │ └── pl │ │ │ │ └── edu │ │ │ │ └── amu │ │ │ │ └── dji │ │ │ │ └── jms │ │ │ │ └── lab10 │ │ │ │ └── books │ │ │ │ ├── BookConfiguration.java │ │ │ │ ├── controller │ │ │ │ ├── BookController.java │ │ │ │ └── exception │ │ │ │ │ ├── BookAlreadyExistsException.java │ │ │ │ │ └── BookNotFoundException.java │ │ │ │ ├── model │ │ │ │ └── Book.java │ │ │ │ └── repository │ │ │ │ └── BookRepository.java │ │ │ └── resources │ │ │ ├── application.properties │ │ │ ├── banner.txt │ │ │ └── logback.xml │ │ └── pom.xml └── students │ └── restful │ ├── books │ ├── pom.xml │ └── src │ │ └── main │ │ ├── java │ │ └── pl │ │ │ └── edu │ │ │ └── amu │ │ │ └── dji │ │ │ └── jms │ │ │ └── lab10 │ │ │ └── books │ │ │ ├── BookConfiguration.java │ │ │ ├── model │ │ │ └── Book.java │ │ │ └── repository │ │ │ └── BookRepository.java │ │ └── resources │ │ ├── application.properties │ │ ├── banner.txt │ │ └── logback.xml │ └── pom.xml ├── 11-rest-web-service-advanced ├── README.md ├── solution │ └── restful │ │ ├── books │ │ ├── pom.xml │ │ └── src │ │ │ └── main │ │ │ ├── java │ │ │ └── pl │ │ │ │ └── edu │ │ │ │ └── amu │ │ │ │ └── dji │ │ │ │ └── jms │ │ │ │ └── lab11 │ │ │ │ └── books │ │ │ │ ├── BookConfiguration.java │ │ │ │ ├── controller │ │ │ │ ├── BookController.java │ │ │ │ └── exception │ │ │ │ │ ├── BookAlreadyExistsException.java │ │ │ │ │ └── BookNotFoundException.java │ │ │ │ ├── model │ │ │ │ ├── Book.java │ │ │ │ └── Review.java │ │ │ │ ├── repository │ │ │ │ └── BookRepository.java │ │ │ │ └── service │ │ │ │ ├── ReviewService.java │ │ │ │ └── ReviewServiceImpl.java │ │ │ └── resources │ │ │ ├── application.properties │ │ │ ├── banner.txt │ │ │ └── logback.xml │ │ ├── pom.xml │ │ └── review │ │ ├── pom.xml │ │ └── src │ │ └── main │ │ ├── java │ │ └── pl │ │ │ └── edu │ │ │ └── amu │ │ │ └── dji │ │ │ └── jms │ │ │ └── lab11 │ │ │ └── review │ │ │ ├── ReviewConfiguration.java │ │ │ ├── controller │ │ │ ├── ReviewController.java │ │ │ └── exception │ │ │ │ ├── ReviewNotFoundException.java │ │ │ │ └── ReviewWithInvalidIsbnException.java │ │ │ ├── model │ │ │ └── Review.java │ │ │ └── repository │ │ │ └── ReviewRepository.java │ │ └── resources │ │ ├── application.properties │ │ ├── banner.txt │ │ └── logback.xml └── students │ └── restful │ ├── books │ ├── pom.xml │ └── src │ │ └── main │ │ ├── java │ │ └── pl │ │ │ └── edu │ │ │ └── amu │ │ │ └── dji │ │ │ └── jms │ │ │ └── lab11 │ │ │ └── books │ │ │ ├── BookConfiguration.java │ │ │ ├── controller │ │ │ ├── BookController.java │ │ │ └── exception │ │ │ │ ├── BookAlreadyExistsException.java │ │ │ │ └── BookNotFoundException.java │ │ │ ├── model │ │ │ └── Book.java │ │ │ └── repository │ │ │ └── BookRepository.java │ │ └── resources │ │ ├── application.properties │ │ ├── banner.txt │ │ └── logback.xml │ ├── pom.xml │ └── review │ ├── pom.xml │ └── src │ └── main │ ├── java │ └── pl │ │ └── edu │ │ └── amu │ │ └── dji │ │ └── jms │ │ └── lab11 │ │ └── review │ │ ├── ReviewConfiguration.java │ │ ├── controller │ │ ├── ReviewController.java │ │ └── exception │ │ │ ├── ReviewNotFoundException.java │ │ │ └── ReviewWithInvalidIsbnException.java │ │ ├── model │ │ └── Review.java │ │ └── repository │ │ └── ReviewRepository.java │ └── resources │ ├── application.properties │ ├── banner.txt │ └── logback.xml ├── 12-akka-basics ├── README.md └── students │ └── akka-basics │ ├── pom.xml │ └── src │ └── main │ └── java │ └── pl │ └── edu │ └── amu │ └── dji │ └── jms │ └── lab12 │ └── akka │ ├── Greeting.java │ ├── GreetingActor.java │ └── GreetingApp.java ├── 13-akka-communication ├── README.md ├── solution │ ├── pom.xml │ └── src │ │ └── main │ │ └── java │ │ └── com │ │ └── uam │ │ └── akka │ │ ├── exercise1 │ │ ├── Main.java │ │ ├── actor │ │ │ ├── Receiver.java │ │ │ └── Sender.java │ │ └── message │ │ │ ├── Answer.java │ │ │ ├── Question.java │ │ │ └── Start.java │ │ ├── exercise2 │ │ ├── Main.java │ │ ├── actor │ │ │ ├── Receiver.java │ │ │ └── Sender.java │ │ └── message │ │ │ ├── Answer.java │ │ │ ├── Question.java │ │ │ └── Start.java │ │ └── exercise3 │ │ ├── PrimesFinder.java │ │ ├── actor │ │ ├── Master.java │ │ └── Worker.java │ │ ├── message │ │ ├── Input.java │ │ └── Output.java │ │ └── util │ │ ├── Interval.java │ │ └── PrimeChecker.java └── students │ ├── pom.xml │ └── src │ └── main │ └── java │ └── com │ └── uam │ └── akka │ ├── exercise1 │ ├── Main.java │ ├── actor │ │ ├── Receiver.java │ │ └── Sender.java │ └── message │ │ ├── Answer.java │ │ ├── Question.java │ │ └── Start.java │ ├── exercise2 │ ├── Main.java │ ├── actor │ │ ├── Receiver.java │ │ └── Sender.java │ └── message │ │ ├── Answer.java │ │ ├── Question.java │ │ └── Start.java │ └── exercise3 │ ├── PrimesFinder.java │ ├── actor │ ├── Master.java │ └── Worker.java │ ├── message │ ├── Input.java │ └── Output.java │ └── util │ ├── Interval.java │ └── PrimeChecker.java ├── 2-threads-synchronization ├── README.md ├── solution │ ├── pom.xml │ └── src │ │ ├── common │ │ ├── Counter.java │ │ ├── CountingRunner.java │ │ └── CountingTask.java │ │ ├── exercise1 │ │ ├── Main.java │ │ └── SynchronizedCounter.java │ │ ├── exercise2 │ │ ├── LockingCounter.java │ │ └── Main.java │ │ ├── exercise3 │ │ ├── AtomicCounter.java │ │ └── Main.java │ │ ├── exercise4 │ │ ├── EvenCheckingTask.java │ │ └── Main.java │ │ ├── exercise5 │ │ ├── Main.java │ │ └── VolatileTask.java │ │ ├── exercise6 │ │ ├── Main.java │ │ ├── equipment │ │ │ ├── Brush.java │ │ │ └── Paint.java │ │ └── painters │ │ │ ├── LeftHandedPainter.java │ │ │ ├── Painter.java │ │ │ └── RightHandedPainter.java │ │ └── exercise7 │ │ ├── Main.java │ │ ├── Painter.java │ │ └── equipment │ │ ├── Brushes.java │ │ └── Paints.java └── students │ ├── pom.xml │ └── src │ ├── common │ ├── Counter.java │ ├── CountingRunner.java │ └── CountingTask.java │ ├── exercise1 │ ├── Main.java │ └── SynchronizedCounter.java │ ├── exercise2 │ ├── LockingCounter.java │ └── Main.java │ ├── exercise3 │ ├── AtomicCounter.java │ └── Main.java │ ├── exercise4 │ ├── EvenCheckingTask.java │ └── Main.java │ ├── exercise5 │ ├── Main.java │ └── VolatileTask.java │ ├── exercise6 │ ├── Main.java │ ├── equipment │ │ ├── Brush.java │ │ └── Paint.java │ └── painters │ │ ├── LeftHandedPainter.java │ │ ├── Painter.java │ │ └── RightHandedPainter.java │ └── exercise7 │ ├── Main.java │ ├── Painter.java │ └── equipment │ ├── Brushes.java │ └── Paints.java ├── 3-threads-higher-abstractions ├── README.md ├── solution │ ├── pom.xml │ └── src │ │ ├── common │ │ ├── StringUtils.java │ │ └── html │ │ │ ├── GazetaHtmlDocument.java │ │ │ ├── HtmlDocument.java │ │ │ └── PudelekHtmlDocument.java │ │ ├── exercise1 │ │ ├── Main.java │ │ ├── Painter.java │ │ └── equipment │ │ │ ├── Brushes.java │ │ │ └── Paints.java │ │ ├── exercise2 │ │ ├── Main.java │ │ ├── Painter.java │ │ └── equipment │ │ │ ├── Brushes.java │ │ │ └── Paints.java │ │ ├── exercise3 │ │ ├── Main.java │ │ └── WordCounter.java │ │ └── exercise4 │ │ ├── Main.java │ │ ├── WebCrawlingTask.java │ │ └── WordCountingTask.java └── students │ ├── pom.xml │ └── src │ ├── common │ ├── StringUtils.java │ └── html │ │ ├── GazetaHtmlDocument.java │ │ ├── HtmlDocument.java │ │ └── PudelekHtmlDocument.java │ ├── exercise1 │ ├── Main.java │ ├── Painter.java │ └── equipment │ │ ├── Brushes.java │ │ └── Paints.java │ ├── exercise2 │ ├── Main.java │ ├── Painter.java │ └── equipment │ │ ├── Brushes.java │ │ └── Paints.java │ ├── exercise3 │ ├── Main.java │ └── WordCounter.java │ └── exercise4 │ ├── Main.java │ ├── WebCrawlingTask.java │ └── WordCountingTask.java ├── 4-JMS-basics ├── README.md ├── SayHello │ ├── Broker │ │ ├── pom.xml │ │ └── src │ │ │ └── main │ │ │ └── java │ │ │ └── pl │ │ │ └── edu │ │ │ └── amu │ │ │ └── dji │ │ │ └── jms │ │ │ └── lab1 │ │ │ └── Broker.java │ ├── Hello │ │ ├── pom.xml │ │ └── src │ │ │ └── main │ │ │ └── java │ │ │ └── pl │ │ │ └── edu │ │ │ └── amu │ │ │ └── dji │ │ │ └── jms │ │ │ └── lab1 │ │ │ └── HelloMain.java │ ├── Say │ │ ├── pom.xml │ │ └── src │ │ │ └── main │ │ │ └── java │ │ │ └── pl │ │ │ └── edu │ │ │ └── amu │ │ │ └── dji │ │ │ └── jms │ │ │ └── lab1 │ │ │ └── SayMain.java │ └── pom.xml └── Solution │ └── SayHello │ ├── Broker │ ├── pom.xml │ └── src │ │ └── main │ │ └── java │ │ └── pl │ │ └── edu │ │ └── amu │ │ └── dji │ │ └── jms │ │ └── lab1 │ │ └── Broker.java │ ├── Hello │ ├── pom.xml │ └── src │ │ └── main │ │ └── java │ │ └── pl │ │ └── edu │ │ └── amu │ │ └── dji │ │ └── jms │ │ └── lab1 │ │ └── HelloMain.java │ ├── Say │ ├── pom.xml │ └── src │ │ └── main │ │ └── java │ │ └── pl │ │ └── edu │ │ └── amu │ │ └── dji │ │ └── jms │ │ └── lab1 │ │ └── SayMain.java │ └── pom.xml ├── 5-spring-jms ├── README.md ├── solution │ └── spring-jms │ │ ├── jms-server │ │ ├── pom.xml │ │ └── src │ │ │ └── main │ │ │ └── java │ │ │ └── pl │ │ │ └── edu │ │ │ └── amu │ │ │ └── dji │ │ │ └── jms │ │ │ └── lab2 │ │ │ └── jmsserver │ │ │ └── JMSServer.java │ │ ├── pom.xml │ │ ├── retailer │ │ ├── pom.xml │ │ └── src │ │ │ └── main │ │ │ ├── java │ │ │ └── pl │ │ │ │ └── edu │ │ │ │ └── amu │ │ │ │ └── dji │ │ │ │ └── jms │ │ │ │ └── lab2 │ │ │ │ └── retailer │ │ │ │ ├── RetailerApp.java │ │ │ │ └── service │ │ │ │ └── BuyService.java │ │ │ └── resources │ │ │ └── context.xml │ │ └── wholesaler │ │ ├── pom.xml │ │ └── src │ │ └── main │ │ ├── java │ │ └── pl │ │ │ └── edu │ │ │ └── amu │ │ │ └── dji │ │ │ └── jms │ │ │ └── lab2 │ │ │ └── wholesaler │ │ │ ├── WholesalerApp.java │ │ │ └── service │ │ │ ├── OfferService.java │ │ │ └── OrderService.java │ │ └── resources │ │ └── context.xml └── students │ └── spring-jms │ ├── jms-server │ ├── pom.xml │ └── src │ │ └── main │ │ └── java │ │ └── pl │ │ └── edu │ │ └── amu │ │ └── dji │ │ └── jms │ │ └── lab2 │ │ └── jmsserver │ │ └── JMSServer.java │ ├── pom.xml │ ├── retailer │ ├── pom.xml │ └── src │ │ └── main │ │ ├── java │ │ └── pl │ │ │ └── edu │ │ │ └── amu │ │ │ └── dji │ │ │ └── jms │ │ │ └── lab2 │ │ │ └── retailer │ │ │ ├── RetailerApp.java │ │ │ └── service │ │ │ └── BuyService.java │ │ └── resources │ │ └── context.xml │ └── wholesaler │ ├── pom.xml │ └── src │ └── main │ ├── java │ └── pl │ │ └── edu │ │ └── amu │ │ └── dji │ │ └── jms │ │ └── lab2 │ │ └── wholesaler │ │ ├── WholesalerApp.java │ │ └── service │ │ ├── OfferService.java │ │ └── OrderService.java │ └── resources │ └── context.xml ├── 6-tx-spring-jms ├── README.md ├── solution │ └── spring-jms │ │ ├── jms-server │ │ ├── pom.xml │ │ └── src │ │ │ └── main │ │ │ └── java │ │ │ └── pl │ │ │ └── edu │ │ │ └── amu │ │ │ └── dji │ │ │ └── jms │ │ │ └── lab2 │ │ │ └── jmsserver │ │ │ └── JMSServer.java │ │ ├── pom.xml │ │ ├── retailer │ │ ├── pom.xml │ │ └── src │ │ │ └── main │ │ │ ├── java │ │ │ └── pl │ │ │ │ └── edu │ │ │ │ └── amu │ │ │ │ └── dji │ │ │ │ └── jms │ │ │ │ └── lab2 │ │ │ │ └── retailer │ │ │ │ ├── RetailerApp.java │ │ │ │ └── service │ │ │ │ └── BuyService.java │ │ │ └── resources │ │ │ └── context.xml │ │ └── wholesaler │ │ ├── pom.xml │ │ └── src │ │ └── main │ │ ├── java │ │ └── pl │ │ │ └── edu │ │ │ └── amu │ │ │ └── dji │ │ │ └── jms │ │ │ └── lab2 │ │ │ └── wholesaler │ │ │ ├── WholesalerApp.java │ │ │ └── service │ │ │ ├── OfferService.java │ │ │ ├── OrderService.java │ │ │ └── message │ │ │ ├── Offer.java │ │ │ ├── OfferConverter.java │ │ │ ├── Order.java │ │ │ └── OrderConverter.java │ │ └── resources │ │ └── context.xml └── students │ └── spring-jms │ ├── jms-server │ ├── pom.xml │ └── src │ │ └── main │ │ └── java │ │ └── pl │ │ └── edu │ │ └── amu │ │ └── dji │ │ └── jms │ │ └── lab2 │ │ └── jmsserver │ │ └── JMSServer.java │ ├── pom.xml │ ├── retailer │ ├── pom.xml │ └── src │ │ └── main │ │ ├── java │ │ └── pl │ │ │ └── edu │ │ │ └── amu │ │ │ └── dji │ │ │ └── jms │ │ │ └── lab2 │ │ │ └── retailer │ │ │ ├── RetailerApp.java │ │ │ └── service │ │ │ └── BuyService.java │ │ └── resources │ │ └── context.xml │ └── wholesaler │ ├── pom.xml │ └── src │ └── main │ ├── java │ └── pl │ │ └── edu │ │ └── amu │ │ └── dji │ │ └── jms │ │ └── lab2 │ │ └── wholesaler │ │ ├── WholesalerApp.java │ │ └── service │ │ ├── OfferService.java │ │ └── OrderService.java │ └── resources │ └── context.xml ├── 7-JMS-summary-and-project-kickoff ├── README.md └── students │ └── supermarket │ ├── analysis │ ├── pom.xml │ └── src │ │ └── main │ │ ├── java │ │ └── pl │ │ │ └── edu │ │ │ └── amu │ │ │ └── dji │ │ │ └── jms │ │ │ └── lab4 │ │ │ └── analysis │ │ │ └── AnalysisApp.java │ │ └── resources │ │ └── keep │ ├── common │ ├── pom.xml │ └── src │ │ └── main │ │ └── java │ │ └── pl │ │ └── edu │ │ └── amu │ │ └── dji │ │ └── jms │ │ └── lab4 │ │ └── CommonApp.java │ ├── jms-server │ ├── pom.xml │ └── src │ │ └── main │ │ └── java │ │ └── pl │ │ └── edu │ │ └── amu │ │ └── dji │ │ └── jms │ │ └── lab2 │ │ └── jmsserver │ │ └── JMSServer.java │ ├── pointofsales │ ├── pom.xml │ └── src │ │ └── main │ │ ├── java │ │ └── pl │ │ │ └── edu │ │ │ └── amu │ │ │ └── dji │ │ │ └── jms │ │ │ └── lab4 │ │ │ └── PointOfSalesApp.java │ │ └── resources │ │ └── keep │ ├── pom.xml │ ├── prototype │ ├── pom.xml │ └── src │ │ └── main │ │ └── java │ │ └── pl │ │ └── edu │ │ └── amu │ │ └── dji │ │ └── jms │ │ └── lab4 │ │ └── prototype │ │ ├── App.java │ │ ├── PointOfSale.java │ │ ├── Reporting.java │ │ └── Warehouse.java │ └── warehouse │ ├── pom.xml │ └── src │ └── main │ ├── java │ └── pl │ │ └── edu │ │ └── amu │ │ └── dji │ │ └── jms │ │ └── lab4 │ │ └── WarehouseApp.java │ └── resources │ └── keep ├── 8-rest-basics └── README.md ├── 9-rest-consumption ├── README.md ├── solution │ ├── pom.xml │ └── src │ │ ├── main │ │ ├── java │ │ │ └── com │ │ │ │ └── uam │ │ │ │ ├── Application.java │ │ │ │ └── model │ │ │ │ └── Book.java │ │ └── resources │ │ │ ├── banner.txt │ │ │ └── logback.xml │ │ └── test │ │ └── java │ │ └── com │ │ └── uam │ │ ├── exercise1 │ │ ├── BookStoreFirstIntegrationTest.java │ │ ├── BookStoreSecondIntegrationTest.java │ │ └── BookStoreThirdIntegrationTest.java │ │ └── exercise2 │ │ └── BookStoreExchangeIntegrationTest.java └── students │ ├── pom.xml │ └── src │ ├── main │ ├── java │ │ └── com │ │ │ └── uam │ │ │ ├── Application.java │ │ │ └── model │ │ │ └── Book.java │ └── resources │ │ ├── banner.txt │ │ └── logback.xml │ └── test │ └── java │ └── com │ └── uam │ ├── exercise1 │ ├── BookStoreFirstIntegrationTest.java │ ├── BookStoreSecondIntegrationTest.java │ └── BookStoreThirdIntegrationTest.java │ └── exercise2 │ └── BookStoreExchangeIntegrationTest.java ├── README.md └── projects └── 1-threads ├── README.md ├── solution ├── pom.xml └── src │ └── com │ └── bsodzik │ ├── Chairman.java │ ├── Donor.java │ ├── Item.java │ ├── Main.java │ ├── MarketManager.java │ └── Recipient.java └── students └── .gitignore /.gitignore: -------------------------------------------------------------------------------- 1 | target 2 | .idea 3 | *.iml 4 | -------------------------------------------------------------------------------- /0-introduction/students/README.md: -------------------------------------------------------------------------------- 1 | Place your work in this directory -------------------------------------------------------------------------------- /1-threads-basics/solution/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 4.0.0 6 | 7 | com.uam 8 | distibuted-java 9 | 1.0-SNAPSHOT 10 | 11 | 12 | src 13 | 14 | 15 | -------------------------------------------------------------------------------- /1-threads-basics/solution/src/exercise1/Main.java: -------------------------------------------------------------------------------- 1 | package exercise1; 2 | 3 | public class Main { 4 | 5 | public static void main(String[] args) { 6 | System.out.printf("Id: %s, Name: %s", Thread.currentThread().getId(), Thread.currentThread().getName()); 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /1-threads-basics/solution/src/exercise2/Main.java: -------------------------------------------------------------------------------- 1 | package exercise2; 2 | 3 | public class Main { 4 | 5 | public static void main(String[] args) { 6 | Thread[] threads = new Thread[4]; 7 | for (int i = 0; i < threads.length; i++) { 8 | threads[i] = new MyThread("Thread-" + i); 9 | } 10 | 11 | System.out.println("Single threaded output:"); 12 | for (Thread thread : threads) { 13 | thread.run(); // Invocation of run does not create new thread 14 | } 15 | 16 | System.out.println("Multi threaded output:"); 17 | for (Thread thread : threads) { 18 | thread.start(); // To create new thread start must be invoked 19 | } 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /1-threads-basics/solution/src/exercise2/MyThread.java: -------------------------------------------------------------------------------- 1 | package exercise2; 2 | 3 | public class MyThread extends Thread { 4 | 5 | public MyThread(String name) { 6 | super(name); 7 | } 8 | 9 | @Override 10 | public void run() { 11 | for (int i = 0; i < 10; i++) { 12 | System.out.println(getName() + " " + i); 13 | try { 14 | Thread.sleep(10); 15 | } catch (InterruptedException e) { 16 | e.printStackTrace(); 17 | } 18 | } 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /1-threads-basics/solution/src/exercise3/MyRunnable.java: -------------------------------------------------------------------------------- 1 | package exercise3; 2 | 3 | public class MyRunnable implements Runnable { 4 | 5 | @Override 6 | public void run() { 7 | for (int i = 0; i < 10; i++) { 8 | System.out.println(Thread.currentThread().getName() + " " + i); 9 | try { 10 | Thread.sleep(10); 11 | } catch (InterruptedException e) { 12 | e.printStackTrace(); 13 | } 14 | } 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /1-threads-basics/solution/src/exercise4/Main.java: -------------------------------------------------------------------------------- 1 | package exercise4; 2 | 3 | import java.util.concurrent.ExecutorService; 4 | import java.util.concurrent.Executors; 5 | import java.util.concurrent.TimeUnit; 6 | 7 | public class Main { 8 | 9 | public static void main(String[] args) throws InterruptedException { 10 | ExecutorService executorService = Executors.newFixedThreadPool(4); 11 | for (int i = 0; i < 4; i++) { 12 | executorService.execute(new MyRunnable()); 13 | } 14 | executorService.shutdown(); 15 | executorService.awaitTermination(5, TimeUnit.SECONDS); 16 | System.out.println("FINISHED"); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /1-threads-basics/solution/src/exercise4/MyRunnable.java: -------------------------------------------------------------------------------- 1 | package exercise4; 2 | 3 | public class MyRunnable implements Runnable { 4 | 5 | @Override 6 | public void run() { 7 | for (int i = 0; i < 10; i++) { 8 | System.out.println(Thread.currentThread().getName() + " " + i); 9 | try { 10 | Thread.sleep(10); 11 | } catch (InterruptedException e) { 12 | e.printStackTrace(); 13 | } 14 | } 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /1-threads-basics/students/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 4.0.0 6 | 7 | com.uam 8 | distibuted-java 9 | 1.0-SNAPSHOT 10 | 11 | 12 | src 13 | 14 | 15 | -------------------------------------------------------------------------------- /1-threads-basics/students/src/exercise1/Main.java: -------------------------------------------------------------------------------- 1 | package exercise1; 2 | 3 | public class Main { 4 | 5 | public static void main(String[] args) { 6 | // Write your code here 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /1-threads-basics/students/src/exercise2/Main.java: -------------------------------------------------------------------------------- 1 | package exercise2; 2 | 3 | public class Main { 4 | 5 | public static void main(String[] args) { 6 | 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /1-threads-basics/students/src/exercise2/MyThread.java: -------------------------------------------------------------------------------- 1 | package exercise2; 2 | 3 | public class MyThread { 4 | } 5 | -------------------------------------------------------------------------------- /1-threads-basics/students/src/exercise3/Main.java: -------------------------------------------------------------------------------- 1 | package exercise3; 2 | 3 | public class Main { 4 | 5 | public static void main(String[] args) { 6 | 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /1-threads-basics/students/src/exercise3/MyRunnable.java: -------------------------------------------------------------------------------- 1 | package exercise3; 2 | 3 | public class MyRunnable { 4 | } 5 | -------------------------------------------------------------------------------- /1-threads-basics/students/src/exercise4/Main.java: -------------------------------------------------------------------------------- 1 | package exercise4; 2 | 3 | public class Main { 4 | 5 | public static void main(String[] args) { 6 | 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /1-threads-basics/students/src/exercise4/MyRunnable.java: -------------------------------------------------------------------------------- 1 | package exercise4; 2 | 3 | public class MyRunnable { 4 | } 5 | -------------------------------------------------------------------------------- /10-rest-web-service/solution/restful/books/pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4 | restful 5 | pl.edu.amu.dji.jms.lab10 6 | 1.0-SNAPSHOT 7 | 8 | 4.0.0 9 | 10 | pl.edu.amu.dji.jms.lab10 11 | books 12 | jar 13 | 14 | books 15 | http://maven.apache.org 16 | 17 | 18 | UTF-8 19 | 20 | 21 | 22 | 23 | 24 | 25 | -------------------------------------------------------------------------------- /10-rest-web-service/solution/restful/books/src/main/java/pl/edu/amu/dji/jms/lab10/books/BookConfiguration.java: -------------------------------------------------------------------------------- 1 | package pl.edu.amu.dji.jms.lab10.books; 2 | 3 | import org.h2.server.web.WebServlet; 4 | import org.springframework.boot.SpringApplication; 5 | import org.springframework.boot.autoconfigure.EnableAutoConfiguration; 6 | import org.springframework.boot.context.embedded.ServletRegistrationBean; 7 | import org.springframework.context.annotation.Bean; 8 | import org.springframework.context.annotation.ComponentScan; 9 | import org.springframework.web.bind.annotation.RestController; 10 | import org.springframework.web.filter.ShallowEtagHeaderFilter; 11 | 12 | import javax.servlet.Filter; 13 | 14 | @ComponentScan 15 | @EnableAutoConfiguration 16 | @RestController 17 | public class BookConfiguration { 18 | 19 | @Bean 20 | public ServletRegistrationBean h2servletRegistration() { 21 | ServletRegistrationBean registration = new ServletRegistrationBean(new WebServlet()); 22 | registration.addUrlMappings("/console/*"); 23 | return registration; 24 | } 25 | 26 | @Bean 27 | public Filter shallowETagHeaderFilter() { 28 | return new ShallowEtagHeaderFilter(); 29 | } 30 | 31 | public static void main(String[] args) { 32 | SpringApplication.run(BookConfiguration.class, args); 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /10-rest-web-service/solution/restful/books/src/main/java/pl/edu/amu/dji/jms/lab10/books/controller/exception/BookAlreadyExistsException.java: -------------------------------------------------------------------------------- 1 | package pl.edu.amu.dji.jms.lab10.books.controller.exception; 2 | 3 | import org.springframework.http.HttpStatus; 4 | import org.springframework.web.bind.annotation.ResponseStatus; 5 | 6 | @ResponseStatus(value = HttpStatus.CONFLICT) 7 | public class BookAlreadyExistsException extends RuntimeException { 8 | public BookAlreadyExistsException() { 9 | super("Book already exists"); 10 | } 11 | } -------------------------------------------------------------------------------- /10-rest-web-service/solution/restful/books/src/main/java/pl/edu/amu/dji/jms/lab10/books/controller/exception/BookNotFoundException.java: -------------------------------------------------------------------------------- 1 | package pl.edu.amu.dji.jms.lab10.books.controller.exception; 2 | 3 | import org.springframework.http.HttpStatus; 4 | import org.springframework.web.bind.annotation.ResponseStatus; 5 | 6 | @ResponseStatus(value = HttpStatus.NOT_FOUND) 7 | public class BookNotFoundException extends RuntimeException { 8 | public BookNotFoundException() { 9 | super("Book has not been found"); 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /10-rest-web-service/solution/restful/books/src/main/java/pl/edu/amu/dji/jms/lab10/books/repository/BookRepository.java: -------------------------------------------------------------------------------- 1 | package pl.edu.amu.dji.jms.lab10.books.repository; 2 | 3 | import org.springframework.data.repository.CrudRepository; 4 | import org.springframework.stereotype.Repository; 5 | import pl.edu.amu.dji.jms.lab10.books.model.Book; 6 | 7 | import java.util.List; 8 | 9 | @Repository 10 | public interface BookRepository extends CrudRepository { 11 | 12 | List findByTitleLikeAllIgnoreCase(String title); 13 | 14 | List findByAuthorsLikeAllIgnoreCase(String author); 15 | 16 | List findByTitleLikeAndAuthorsLikeAllIgnoreCase(String title, String author); 17 | } 18 | -------------------------------------------------------------------------------- /10-rest-web-service/solution/restful/books/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | spring.datasource.url=jdbc:h2:mem:AZ;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE 2 | spring.datasource.driverClassName=org.h2.Driver 3 | spring.datasource.username=sa 4 | spring.datasource.password= 5 | spring.jpa.database-platform=org.hibernate.dialect.H2Dialect 6 | spring.jpa.hibernate.ddl-auto: create-drop 7 | spring.jpa.database: H2 8 | spring.jpa.show-sql: true 9 | server.port=${port:8080} -------------------------------------------------------------------------------- /10-rest-web-service/solution/restful/books/src/main/resources/banner.txt: -------------------------------------------------------------------------------- 1 | __ _____ __ ___ ____ _ __ _ __ __ __ __ 2 | / / / / | / |/ / / __ \(_)____/ /______(_) /_ __ __/ /____ ____/ / / /___ __ ______ _ 3 | / / / / /| | / /|_/ / / / / / / ___/ __/ ___/ / __ \/ / / / __/ _ \/ __ / __ / / __ `/ | / / __ `/ 4 | / /_/ / ___ |/ / / / / /_/ / (__ ) /_/ / / / /_/ / /_/ / /_/ __/ /_/ / / /_/ / /_/ /| |/ / /_/ / 5 | \____/_/ |_/_/ /_/ /_____/_/____/\__/_/ /_/_.___/\__,_/\__/\___/\__,_/ \____/\__,_/ |___/\__,_/ -------------------------------------------------------------------------------- /10-rest-web-service/solution/restful/books/src/main/resources/logback.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /10-rest-web-service/students/restful/books/pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4 | restful 5 | pl.edu.amu.dji.jms.lab10 6 | 1.0-SNAPSHOT 7 | 8 | 4.0.0 9 | 10 | pl.edu.amu.dji.jms.lab10 11 | books 12 | jar 13 | 14 | books 15 | http://maven.apache.org 16 | 17 | 18 | UTF-8 19 | 20 | 21 | 22 | 23 | 24 | 25 | -------------------------------------------------------------------------------- /10-rest-web-service/students/restful/books/src/main/java/pl/edu/amu/dji/jms/lab10/books/BookConfiguration.java: -------------------------------------------------------------------------------- 1 | package pl.edu.amu.dji.jms.lab10.books; 2 | 3 | import org.h2.server.web.WebServlet; 4 | import org.springframework.boot.SpringApplication; 5 | import org.springframework.boot.autoconfigure.EnableAutoConfiguration; 6 | import org.springframework.boot.context.embedded.ServletRegistrationBean; 7 | import org.springframework.context.annotation.Bean; 8 | import org.springframework.context.annotation.ComponentScan; 9 | import org.springframework.web.bind.annotation.RestController; 10 | 11 | @ComponentScan 12 | @EnableAutoConfiguration 13 | @RestController 14 | public class BookConfiguration { 15 | 16 | @Bean 17 | public ServletRegistrationBean h2servletRegistration() { 18 | ServletRegistrationBean registration = new ServletRegistrationBean(new WebServlet()); 19 | registration.addUrlMappings("/console/*"); 20 | return registration; 21 | } 22 | 23 | public static void main(String[] args) { 24 | SpringApplication.run(BookConfiguration.class, args); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /10-rest-web-service/students/restful/books/src/main/java/pl/edu/amu/dji/jms/lab10/books/repository/BookRepository.java: -------------------------------------------------------------------------------- 1 | package pl.edu.amu.dji.jms.lab10.books.repository; 2 | 3 | import org.springframework.data.repository.CrudRepository; 4 | import org.springframework.stereotype.Repository; 5 | import pl.edu.amu.dji.jms.lab10.books.model.Book; 6 | 7 | @Repository 8 | public interface BookRepository extends CrudRepository { 9 | } 10 | -------------------------------------------------------------------------------- /10-rest-web-service/students/restful/books/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | spring.datasource.url=jdbc:h2:mem:AZ;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE 2 | spring.datasource.driverClassName=org.h2.Driver 3 | spring.datasource.username=sa 4 | spring.datasource.password= 5 | spring.jpa.database-platform=org.hibernate.dialect.H2Dialect 6 | spring.jpa.hibernate.ddl-auto: create-drop 7 | spring.jpa.database: H2 8 | spring.jpa.show-sql: true 9 | server.port=${port:8080} -------------------------------------------------------------------------------- /10-rest-web-service/students/restful/books/src/main/resources/banner.txt: -------------------------------------------------------------------------------- 1 | __ _____ __ ___ ____ _ __ _ __ __ __ __ 2 | / / / / | / |/ / / __ \(_)____/ /______(_) /_ __ __/ /____ ____/ / / /___ __ ______ _ 3 | / / / / /| | / /|_/ / / / / / / ___/ __/ ___/ / __ \/ / / / __/ _ \/ __ / __ / / __ `/ | / / __ `/ 4 | / /_/ / ___ |/ / / / / /_/ / (__ ) /_/ / / / /_/ / /_/ / /_/ __/ /_/ / / /_/ / /_/ /| |/ / /_/ / 5 | \____/_/ |_/_/ /_/ /_____/_/____/\__/_/ /_/_.___/\__,_/\__/\___/\__,_/ \____/\__,_/ |___/\__,_/ -------------------------------------------------------------------------------- /10-rest-web-service/students/restful/books/src/main/resources/logback.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /11-rest-web-service-advanced/solution/restful/books/pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4 | restful 5 | pl.edu.amu.dji.jms.lab10 6 | 1.0-SNAPSHOT 7 | 8 | 4.0.0 9 | 10 | pl.edu.amu.dji.jms.lab10 11 | books 12 | jar 13 | 14 | books 15 | http://maven.apache.org 16 | 17 | 18 | UTF-8 19 | 20 | 21 | 22 | 23 | net.sf.ehcache 24 | ehcache 25 | 2.9.0 26 | 27 | 28 | org.springframework 29 | spring-context-support 30 | 31 | 32 | 33 | 34 | -------------------------------------------------------------------------------- /11-rest-web-service-advanced/solution/restful/books/src/main/java/pl/edu/amu/dji/jms/lab11/books/controller/exception/BookAlreadyExistsException.java: -------------------------------------------------------------------------------- 1 | package pl.edu.amu.dji.jms.lab11.books.controller.exception; 2 | 3 | import org.springframework.http.HttpStatus; 4 | import org.springframework.web.bind.annotation.ResponseStatus; 5 | 6 | @ResponseStatus(value = HttpStatus.CONFLICT) 7 | public class BookAlreadyExistsException extends RuntimeException { 8 | public BookAlreadyExistsException() { 9 | super("Book already exists"); 10 | } 11 | } -------------------------------------------------------------------------------- /11-rest-web-service-advanced/solution/restful/books/src/main/java/pl/edu/amu/dji/jms/lab11/books/controller/exception/BookNotFoundException.java: -------------------------------------------------------------------------------- 1 | package pl.edu.amu.dji.jms.lab11.books.controller.exception; 2 | 3 | import org.springframework.http.HttpStatus; 4 | import org.springframework.web.bind.annotation.ResponseStatus; 5 | 6 | @ResponseStatus(value = HttpStatus.NOT_FOUND) 7 | public class BookNotFoundException extends RuntimeException { 8 | public BookNotFoundException() { 9 | super("Book has not been found"); 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /11-rest-web-service-advanced/solution/restful/books/src/main/java/pl/edu/amu/dji/jms/lab11/books/model/Review.java: -------------------------------------------------------------------------------- 1 | package pl.edu.amu.dji.jms.lab11.books.model; 2 | 3 | import com.fasterxml.jackson.annotation.JsonIgnoreProperties; 4 | 5 | @JsonIgnoreProperties(ignoreUnknown = true) 6 | public class Review { 7 | 8 | private String isbn; 9 | 10 | private String title; 11 | 12 | private String text; 13 | 14 | public Review() { 15 | } 16 | 17 | 18 | public String getIsbn() { 19 | return isbn; 20 | } 21 | 22 | public void setIsbn(String isbn) { 23 | this.isbn = isbn; 24 | } 25 | 26 | public String getTitle() { 27 | return title; 28 | } 29 | 30 | public void setTitle(String title) { 31 | this.title = title; 32 | } 33 | 34 | public String getText() { 35 | return text; 36 | } 37 | 38 | public void setText(String text) { 39 | this.text = text; 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /11-rest-web-service-advanced/solution/restful/books/src/main/java/pl/edu/amu/dji/jms/lab11/books/repository/BookRepository.java: -------------------------------------------------------------------------------- 1 | package pl.edu.amu.dji.jms.lab11.books.repository; 2 | 3 | import org.springframework.data.repository.CrudRepository; 4 | import org.springframework.stereotype.Repository; 5 | import pl.edu.amu.dji.jms.lab11.books.model.Book; 6 | 7 | import java.util.List; 8 | 9 | @Repository 10 | public interface BookRepository extends CrudRepository { 11 | 12 | List findByTitleLikeAllIgnoreCase(String title); 13 | 14 | List findByAuthorsLikeAllIgnoreCase(String author); 15 | 16 | List findByTitleLikeAndAuthorsLikeAllIgnoreCase(String title, String author); 17 | } 18 | -------------------------------------------------------------------------------- /11-rest-web-service-advanced/solution/restful/books/src/main/java/pl/edu/amu/dji/jms/lab11/books/service/ReviewService.java: -------------------------------------------------------------------------------- 1 | package pl.edu.amu.dji.jms.lab11.books.service; 2 | 3 | import pl.edu.amu.dji.jms.lab11.books.model.Review; 4 | 5 | public interface ReviewService { 6 | Iterable getReviews(String isbn); 7 | } 8 | -------------------------------------------------------------------------------- /11-rest-web-service-advanced/solution/restful/books/src/main/java/pl/edu/amu/dji/jms/lab11/books/service/ReviewServiceImpl.java: -------------------------------------------------------------------------------- 1 | package pl.edu.amu.dji.jms.lab11.books.service; 2 | 3 | import com.google.common.base.Preconditions; 4 | import org.springframework.beans.factory.annotation.Autowired; 5 | import org.springframework.cache.annotation.Cacheable; 6 | import org.springframework.core.ParameterizedTypeReference; 7 | import org.springframework.http.HttpMethod; 8 | import org.springframework.http.HttpStatus; 9 | import org.springframework.http.ResponseEntity; 10 | import org.springframework.stereotype.Service; 11 | import org.springframework.web.client.RestTemplate; 12 | import pl.edu.amu.dji.jms.lab11.books.model.Review; 13 | 14 | @Service 15 | public class ReviewServiceImpl implements ReviewService { 16 | 17 | @Autowired 18 | private RestTemplate restTemplate; 19 | 20 | @Cacheable("reviews") 21 | @Override 22 | public Iterable getReviews(String isbn) { 23 | ParameterizedTypeReference> responseType = new ParameterizedTypeReference>() {}; 24 | 25 | ResponseEntity> entity = restTemplate.exchange("http://localhost:8090/reviews/{isbn}", HttpMethod.GET, null, responseType, isbn); 26 | Preconditions.checkState(entity.getStatusCode() == HttpStatus.OK); 27 | return entity.getBody(); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /11-rest-web-service-advanced/solution/restful/books/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | spring.datasource.url=jdbc:h2:mem:AZ;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE 2 | spring.datasource.driverClassName=org.h2.Driver 3 | spring.datasource.username=sa 4 | spring.datasource.password= 5 | spring.jpa.database-platform=org.hibernate.dialect.H2Dialect 6 | spring.jpa.hibernate.ddl-auto: create-drop 7 | spring.jpa.database: H2 8 | spring.jpa.show-sql: true 9 | server.port=${port:8080} -------------------------------------------------------------------------------- /11-rest-web-service-advanced/solution/restful/books/src/main/resources/banner.txt: -------------------------------------------------------------------------------- 1 | __ _____ __ ___ ____ _ __ _ __ __ __ __ 2 | / / / / | / |/ / / __ \(_)____/ /______(_) /_ __ __/ /____ ____/ / / /___ __ ______ _ 3 | / / / / /| | / /|_/ / / / / / / ___/ __/ ___/ / __ \/ / / / __/ _ \/ __ / __ / / __ `/ | / / __ `/ 4 | / /_/ / ___ |/ / / / / /_/ / (__ ) /_/ / / / /_/ / /_/ / /_/ __/ /_/ / / /_/ / /_/ /| |/ / /_/ / 5 | \____/_/ |_/_/ /_/ /_____/_/____/\__/_/ /_/_.___/\__,_/\__/\___/\__,_/ \____/\__,_/ |___/\__,_/ -------------------------------------------------------------------------------- /11-rest-web-service-advanced/solution/restful/books/src/main/resources/logback.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /11-rest-web-service-advanced/solution/restful/review/pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4 | restful 5 | pl.edu.amu.dji.jms.lab10 6 | 1.0-SNAPSHOT 7 | 8 | 4.0.0 9 | 10 | pl.edu.amu.dji.jms.lab10 11 | review 12 | jar 13 | 14 | review 15 | http://maven.apache.org 16 | 17 | 18 | UTF-8 19 | 20 | 21 | 22 | 23 | junit 24 | junit 25 | 3.8.1 26 | test 27 | 28 | 29 | 30 | -------------------------------------------------------------------------------- /11-rest-web-service-advanced/solution/restful/review/src/main/java/pl/edu/amu/dji/jms/lab11/review/ReviewConfiguration.java: -------------------------------------------------------------------------------- 1 | package pl.edu.amu.dji.jms.lab11.review; 2 | 3 | import org.h2.server.web.WebServlet; 4 | import org.springframework.boot.SpringApplication; 5 | import org.springframework.boot.autoconfigure.EnableAutoConfiguration; 6 | import org.springframework.boot.context.embedded.ServletRegistrationBean; 7 | import org.springframework.context.annotation.Bean; 8 | import org.springframework.context.annotation.ComponentScan; 9 | import org.springframework.web.bind.annotation.RestController; 10 | 11 | @ComponentScan 12 | @EnableAutoConfiguration 13 | @RestController 14 | public class ReviewConfiguration { 15 | 16 | @Bean 17 | public ServletRegistrationBean h2servletRegistration() { 18 | ServletRegistrationBean registration = new ServletRegistrationBean(new WebServlet()); 19 | registration.addUrlMappings("/console/*"); 20 | return registration; 21 | } 22 | 23 | public static void main(String[] args) { 24 | SpringApplication.run(ReviewConfiguration.class, args); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /11-rest-web-service-advanced/solution/restful/review/src/main/java/pl/edu/amu/dji/jms/lab11/review/controller/exception/ReviewNotFoundException.java: -------------------------------------------------------------------------------- 1 | package pl.edu.amu.dji.jms.lab11.review.controller.exception; 2 | 3 | import org.springframework.http.HttpStatus; 4 | import org.springframework.web.bind.annotation.ResponseStatus; 5 | 6 | @ResponseStatus(value = HttpStatus.NOT_FOUND) 7 | public class ReviewNotFoundException extends RuntimeException { 8 | public ReviewNotFoundException() { 9 | super("Review has not been found"); 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /11-rest-web-service-advanced/solution/restful/review/src/main/java/pl/edu/amu/dji/jms/lab11/review/controller/exception/ReviewWithInvalidIsbnException.java: -------------------------------------------------------------------------------- 1 | package pl.edu.amu.dji.jms.lab11.review.controller.exception; 2 | 3 | import org.springframework.http.HttpStatus; 4 | import org.springframework.web.bind.annotation.ResponseStatus; 5 | 6 | @ResponseStatus(value = HttpStatus.CONFLICT) 7 | public class ReviewWithInvalidIsbnException extends RuntimeException { 8 | public ReviewWithInvalidIsbnException() { 9 | super("Review with invalid ISBN"); 10 | } 11 | } -------------------------------------------------------------------------------- /11-rest-web-service-advanced/solution/restful/review/src/main/java/pl/edu/amu/dji/jms/lab11/review/repository/ReviewRepository.java: -------------------------------------------------------------------------------- 1 | package pl.edu.amu.dji.jms.lab11.review.repository; 2 | 3 | import org.springframework.data.repository.CrudRepository; 4 | import org.springframework.stereotype.Repository; 5 | import pl.edu.amu.dji.jms.lab11.review.model.Review; 6 | 7 | @Repository 8 | public interface ReviewRepository extends CrudRepository { 9 | 10 | Iterable findByIsbnAllIgnoreCase(String isbn); 11 | 12 | } 13 | -------------------------------------------------------------------------------- /11-rest-web-service-advanced/solution/restful/review/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | spring.datasource.url=jdbc:h2:mem:AZ;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE 2 | spring.datasource.driverClassName=org.h2.Driver 3 | spring.datasource.username=sa 4 | spring.datasource.password= 5 | spring.jpa.database-platform=org.hibernate.dialect.H2Dialect 6 | spring.jpa.hibernate.ddl-auto: create-drop 7 | spring.jpa.database: H2 8 | spring.jpa.show-sql: true 9 | server.port=${port:8090} -------------------------------------------------------------------------------- /11-rest-web-service-advanced/solution/restful/review/src/main/resources/banner.txt: -------------------------------------------------------------------------------- 1 | __ _____ __ ___ ____ _ __ _ __ __ __ __ 2 | / / / / | / |/ / / __ \(_)____/ /______(_) /_ __ __/ /____ ____/ / / /___ __ ______ _ 3 | / / / / /| | / /|_/ / / / / / / ___/ __/ ___/ / __ \/ / / / __/ _ \/ __ / __ / / __ `/ | / / __ `/ 4 | / /_/ / ___ |/ / / / / /_/ / (__ ) /_/ / / / /_/ / /_/ / /_/ __/ /_/ / / /_/ / /_/ /| |/ / /_/ / 5 | \____/_/ |_/_/ /_/ /_____/_/____/\__/_/ /_/_.___/\__,_/\__/\___/\__,_/ \____/\__,_/ |___/\__,_/ -------------------------------------------------------------------------------- /11-rest-web-service-advanced/solution/restful/review/src/main/resources/logback.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /11-rest-web-service-advanced/students/restful/books/pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4 | restful 5 | pl.edu.amu.dji.jms.lab10 6 | 1.0-SNAPSHOT 7 | 8 | 4.0.0 9 | 10 | pl.edu.amu.dji.jms.lab10 11 | books 12 | jar 13 | 14 | books 15 | http://maven.apache.org 16 | 17 | 18 | UTF-8 19 | 20 | 21 | 22 | 23 | net.sf.ehcache 24 | ehcache 25 | 2.9.0 26 | 27 | 28 | org.springframework 29 | spring-context-support 30 | 31 | 32 | 33 | 34 | -------------------------------------------------------------------------------- /11-rest-web-service-advanced/students/restful/books/src/main/java/pl/edu/amu/dji/jms/lab11/books/controller/exception/BookAlreadyExistsException.java: -------------------------------------------------------------------------------- 1 | package pl.edu.amu.dji.jms.lab11.books.controller.exception; 2 | 3 | import org.springframework.http.HttpStatus; 4 | import org.springframework.web.bind.annotation.ResponseStatus; 5 | 6 | @ResponseStatus(value = HttpStatus.CONFLICT) 7 | public class BookAlreadyExistsException extends RuntimeException { 8 | public BookAlreadyExistsException() { 9 | super("Book already exists"); 10 | } 11 | } -------------------------------------------------------------------------------- /11-rest-web-service-advanced/students/restful/books/src/main/java/pl/edu/amu/dji/jms/lab11/books/controller/exception/BookNotFoundException.java: -------------------------------------------------------------------------------- 1 | package pl.edu.amu.dji.jms.lab11.books.controller.exception; 2 | 3 | import org.springframework.http.HttpStatus; 4 | import org.springframework.web.bind.annotation.ResponseStatus; 5 | 6 | @ResponseStatus(value = HttpStatus.NOT_FOUND) 7 | public class BookNotFoundException extends RuntimeException { 8 | public BookNotFoundException() { 9 | super("Book has not been found"); 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /11-rest-web-service-advanced/students/restful/books/src/main/java/pl/edu/amu/dji/jms/lab11/books/repository/BookRepository.java: -------------------------------------------------------------------------------- 1 | package pl.edu.amu.dji.jms.lab11.books.repository; 2 | 3 | import org.springframework.data.repository.CrudRepository; 4 | import org.springframework.stereotype.Repository; 5 | import pl.edu.amu.dji.jms.lab11.books.model.Book; 6 | 7 | import java.util.List; 8 | 9 | @Repository 10 | public interface BookRepository extends CrudRepository { 11 | 12 | List findByTitleLikeAllIgnoreCase(String title); 13 | 14 | List findByAuthorsLikeAllIgnoreCase(String author); 15 | 16 | List findByTitleLikeAndAuthorsLikeAllIgnoreCase(String title, String author); 17 | } 18 | -------------------------------------------------------------------------------- /11-rest-web-service-advanced/students/restful/books/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | spring.datasource.url=jdbc:h2:mem:AZ;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE 2 | spring.datasource.driverClassName=org.h2.Driver 3 | spring.datasource.username=sa 4 | spring.datasource.password= 5 | spring.jpa.database-platform=org.hibernate.dialect.H2Dialect 6 | spring.jpa.hibernate.ddl-auto: create-drop 7 | spring.jpa.database: H2 8 | spring.jpa.show-sql: true 9 | server.port=${port:8080} -------------------------------------------------------------------------------- /11-rest-web-service-advanced/students/restful/books/src/main/resources/banner.txt: -------------------------------------------------------------------------------- 1 | __ _____ __ ___ ____ _ __ _ __ __ __ __ 2 | / / / / | / |/ / / __ \(_)____/ /______(_) /_ __ __/ /____ ____/ / / /___ __ ______ _ 3 | / / / / /| | / /|_/ / / / / / / ___/ __/ ___/ / __ \/ / / / __/ _ \/ __ / __ / / __ `/ | / / __ `/ 4 | / /_/ / ___ |/ / / / / /_/ / (__ ) /_/ / / / /_/ / /_/ / /_/ __/ /_/ / / /_/ / /_/ /| |/ / /_/ / 5 | \____/_/ |_/_/ /_/ /_____/_/____/\__/_/ /_/_.___/\__,_/\__/\___/\__,_/ \____/\__,_/ |___/\__,_/ -------------------------------------------------------------------------------- /11-rest-web-service-advanced/students/restful/books/src/main/resources/logback.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /11-rest-web-service-advanced/students/restful/review/pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4 | restful 5 | pl.edu.amu.dji.jms.lab10 6 | 1.0-SNAPSHOT 7 | 8 | 4.0.0 9 | 10 | pl.edu.amu.dji.jms.lab10 11 | review 12 | jar 13 | 14 | review 15 | http://maven.apache.org 16 | 17 | 18 | UTF-8 19 | 20 | 21 | 22 | 23 | junit 24 | junit 25 | 3.8.1 26 | test 27 | 28 | 29 | 30 | -------------------------------------------------------------------------------- /11-rest-web-service-advanced/students/restful/review/src/main/java/pl/edu/amu/dji/jms/lab11/review/ReviewConfiguration.java: -------------------------------------------------------------------------------- 1 | package pl.edu.amu.dji.jms.lab11.review; 2 | 3 | import org.h2.server.web.WebServlet; 4 | import org.springframework.boot.SpringApplication; 5 | import org.springframework.boot.autoconfigure.EnableAutoConfiguration; 6 | import org.springframework.boot.context.embedded.ServletRegistrationBean; 7 | import org.springframework.context.annotation.Bean; 8 | import org.springframework.context.annotation.ComponentScan; 9 | import org.springframework.web.bind.annotation.RestController; 10 | 11 | @ComponentScan 12 | @EnableAutoConfiguration 13 | @RestController 14 | public class ReviewConfiguration { 15 | 16 | @Bean 17 | public ServletRegistrationBean h2servletRegistration() { 18 | ServletRegistrationBean registration = new ServletRegistrationBean(new WebServlet()); 19 | registration.addUrlMappings("/console/*"); 20 | return registration; 21 | } 22 | 23 | public static void main(String[] args) { 24 | SpringApplication.run(ReviewConfiguration.class, args); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /11-rest-web-service-advanced/students/restful/review/src/main/java/pl/edu/amu/dji/jms/lab11/review/controller/ReviewController.java: -------------------------------------------------------------------------------- 1 | package pl.edu.amu.dji.jms.lab11.review.controller; 2 | 3 | 4 | public class ReviewController { 5 | } 6 | -------------------------------------------------------------------------------- /11-rest-web-service-advanced/students/restful/review/src/main/java/pl/edu/amu/dji/jms/lab11/review/controller/exception/ReviewNotFoundException.java: -------------------------------------------------------------------------------- 1 | package pl.edu.amu.dji.jms.lab11.review.controller.exception; 2 | 3 | import org.springframework.http.HttpStatus; 4 | import org.springframework.web.bind.annotation.ResponseStatus; 5 | 6 | @ResponseStatus(value = HttpStatus.NOT_FOUND) 7 | public class ReviewNotFoundException extends RuntimeException { 8 | public ReviewNotFoundException() { 9 | super("Review has not been found"); 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /11-rest-web-service-advanced/students/restful/review/src/main/java/pl/edu/amu/dji/jms/lab11/review/controller/exception/ReviewWithInvalidIsbnException.java: -------------------------------------------------------------------------------- 1 | package pl.edu.amu.dji.jms.lab11.review.controller.exception; 2 | 3 | import org.springframework.http.HttpStatus; 4 | import org.springframework.web.bind.annotation.ResponseStatus; 5 | 6 | @ResponseStatus(value = HttpStatus.CONFLICT) 7 | public class ReviewWithInvalidIsbnException extends RuntimeException { 8 | public ReviewWithInvalidIsbnException() { 9 | super("Review with invalid ISBN"); 10 | } 11 | } -------------------------------------------------------------------------------- /11-rest-web-service-advanced/students/restful/review/src/main/java/pl/edu/amu/dji/jms/lab11/review/repository/ReviewRepository.java: -------------------------------------------------------------------------------- 1 | package pl.edu.amu.dji.jms.lab11.review.repository; 2 | 3 | import org.springframework.data.repository.CrudRepository; 4 | import org.springframework.stereotype.Repository; 5 | import pl.edu.amu.dji.jms.lab11.review.model.Review; 6 | 7 | @Repository 8 | public interface ReviewRepository extends CrudRepository { 9 | 10 | Iterable findByIsbnAllIgnoreCase(String isbn); 11 | 12 | } 13 | -------------------------------------------------------------------------------- /11-rest-web-service-advanced/students/restful/review/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | spring.datasource.url=jdbc:h2:mem:AZ;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE 2 | spring.datasource.driverClassName=org.h2.Driver 3 | spring.datasource.username=sa 4 | spring.datasource.password= 5 | spring.jpa.database-platform=org.hibernate.dialect.H2Dialect 6 | spring.jpa.hibernate.ddl-auto: create-drop 7 | spring.jpa.database: H2 8 | spring.jpa.show-sql: true 9 | server.port=${port:8090} -------------------------------------------------------------------------------- /11-rest-web-service-advanced/students/restful/review/src/main/resources/banner.txt: -------------------------------------------------------------------------------- 1 | __ _____ __ ___ ____ _ __ _ __ __ __ __ 2 | / / / / | / |/ / / __ \(_)____/ /______(_) /_ __ __/ /____ ____/ / / /___ __ ______ _ 3 | / / / / /| | / /|_/ / / / / / / ___/ __/ ___/ / __ \/ / / / __/ _ \/ __ / __ / / __ `/ | / / __ `/ 4 | / /_/ / ___ |/ / / / / /_/ / (__ ) /_/ / / / /_/ / /_/ / /_/ __/ /_/ / / /_/ / /_/ /| |/ / /_/ / 5 | \____/_/ |_/_/ /_/ /_____/_/____/\__/_/ /_/_.___/\__,_/\__/\___/\__,_/ \____/\__,_/ |___/\__,_/ -------------------------------------------------------------------------------- /11-rest-web-service-advanced/students/restful/review/src/main/resources/logback.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /12-akka-basics/students/akka-basics/pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4.0.0 4 | 5 | pl.edu.amu.dji.jms.lab12.akka 6 | akka-basics 7 | 1.0-SNAPSHOT 8 | jar 9 | 10 | akka-basics 11 | http://maven.apache.org 12 | 13 | 14 | UTF-8 15 | 16 | 17 | 18 | 19 | com.typesafe.akka 20 | akka-actor_2.10 21 | 2.3.8 22 | 23 | 24 | 25 | -------------------------------------------------------------------------------- /12-akka-basics/students/akka-basics/src/main/java/pl/edu/amu/dji/jms/lab12/akka/Greeting.java: -------------------------------------------------------------------------------- 1 | package pl.edu.amu.dji.jms.lab12.akka; 2 | 3 | public final class Greeting { 4 | 5 | } 6 | -------------------------------------------------------------------------------- /12-akka-basics/students/akka-basics/src/main/java/pl/edu/amu/dji/jms/lab12/akka/GreetingActor.java: -------------------------------------------------------------------------------- 1 | package pl.edu.amu.dji.jms.lab12.akka; 2 | 3 | public class GreetingActor { 4 | 5 | } 6 | -------------------------------------------------------------------------------- /12-akka-basics/students/akka-basics/src/main/java/pl/edu/amu/dji/jms/lab12/akka/GreetingApp.java: -------------------------------------------------------------------------------- 1 | package pl.edu.amu.dji.jms.lab12.akka; 2 | 3 | public class GreetingApp { 4 | public static void main(String[] args) { 5 | 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /13-akka-communication/solution/pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4.0.0 4 | 5 | com.uam 6 | distibuted-java 7 | 1.0-SNAPSHOT 8 | 9 | 10 | UTF-8 11 | 12 | 13 | 14 | 15 | com.typesafe.akka 16 | akka-actor_2.10 17 | 2.3.8 18 | 19 | 20 | 21 | -------------------------------------------------------------------------------- /13-akka-communication/solution/src/main/java/com/uam/akka/exercise1/Main.java: -------------------------------------------------------------------------------- 1 | package com.uam.akka.exercise1; 2 | 3 | import akka.actor.ActorRef; 4 | import akka.actor.ActorSystem; 5 | import akka.actor.Props; 6 | import com.uam.akka.exercise1.actor.Receiver; 7 | import com.uam.akka.exercise1.actor.Sender; 8 | import com.uam.akka.exercise1.message.Start; 9 | 10 | public class Main { 11 | 12 | public static void main(String[] args) { 13 | ActorSystem system = ActorSystem.create("system"); 14 | 15 | ActorRef receiver = system.actorOf(Props.create(Receiver.class), "receiver"); 16 | ActorRef sender = system.actorOf(Props.create(Sender.class, receiver), "sender"); 17 | 18 | sender.tell(new Start(), ActorRef.noSender()); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /13-akka-communication/solution/src/main/java/com/uam/akka/exercise1/actor/Receiver.java: -------------------------------------------------------------------------------- 1 | package com.uam.akka.exercise1.actor; 2 | 3 | import akka.actor.UntypedActor; 4 | import akka.event.Logging; 5 | import akka.event.LoggingAdapter; 6 | import com.uam.akka.exercise1.message.Answer; 7 | import com.uam.akka.exercise1.message.Question; 8 | 9 | public class Receiver extends UntypedActor { 10 | 11 | private final LoggingAdapter log = Logging.getLogger(getContext().system(), this); 12 | 13 | @Override 14 | public void onReceive(Object o) throws Exception { 15 | if (o instanceof Question) { 16 | log.info("Received question: {}", o); 17 | getSender().tell(new Answer("Excellent!"), getSelf()); 18 | } 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /13-akka-communication/solution/src/main/java/com/uam/akka/exercise1/actor/Sender.java: -------------------------------------------------------------------------------- 1 | package com.uam.akka.exercise1.actor; 2 | 3 | import akka.actor.ActorRef; 4 | import akka.actor.UntypedActor; 5 | import akka.event.Logging; 6 | import akka.event.LoggingAdapter; 7 | import com.uam.akka.exercise1.message.Answer; 8 | import com.uam.akka.exercise1.message.Question; 9 | import com.uam.akka.exercise1.message.Start; 10 | 11 | public class Sender extends UntypedActor { 12 | 13 | private final LoggingAdapter log = Logging.getLogger(getContext().system(), this); 14 | private final ActorRef receiver; 15 | 16 | public Sender(ActorRef receiver) { 17 | this.receiver = receiver; 18 | } 19 | 20 | @Override 21 | public void onReceive(Object o) throws Exception { 22 | if (o instanceof Start) { 23 | receiver.tell(new Question("How are you doing?"), getSelf()); 24 | } else if (o instanceof Answer) { 25 | log.info("Receiver sent answer: " + o); 26 | getContext().system().shutdown(); 27 | } 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /13-akka-communication/solution/src/main/java/com/uam/akka/exercise1/message/Answer.java: -------------------------------------------------------------------------------- 1 | package com.uam.akka.exercise1.message; 2 | 3 | public class Answer { 4 | 5 | private final String text; 6 | 7 | public Answer(String text) { 8 | this.text = text; 9 | } 10 | 11 | public String getText() { 12 | return text; 13 | } 14 | 15 | @Override 16 | public String toString() { 17 | return text; 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /13-akka-communication/solution/src/main/java/com/uam/akka/exercise1/message/Question.java: -------------------------------------------------------------------------------- 1 | package com.uam.akka.exercise1.message; 2 | 3 | public class Question { 4 | 5 | private final String text; 6 | 7 | public Question(String text) { 8 | this.text = text; 9 | } 10 | 11 | public String getText() { 12 | return text; 13 | } 14 | 15 | @Override 16 | public String toString() { 17 | return text; 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /13-akka-communication/solution/src/main/java/com/uam/akka/exercise1/message/Start.java: -------------------------------------------------------------------------------- 1 | package com.uam.akka.exercise1.message; 2 | 3 | public class Start { 4 | } 5 | -------------------------------------------------------------------------------- /13-akka-communication/solution/src/main/java/com/uam/akka/exercise2/Main.java: -------------------------------------------------------------------------------- 1 | package com.uam.akka.exercise2; 2 | 3 | import akka.actor.ActorRef; 4 | import akka.actor.ActorSystem; 5 | import akka.actor.Props; 6 | import com.uam.akka.exercise2.actor.Sender; 7 | import com.uam.akka.exercise2.message.Start; 8 | 9 | public class Main { 10 | 11 | public static void main(String[] args) { 12 | ActorSystem system = ActorSystem.create("system"); 13 | 14 | ActorRef sender = system.actorOf(Props.create(Sender.class), "sender"); 15 | 16 | sender.tell(new Start(), ActorRef.noSender()); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /13-akka-communication/solution/src/main/java/com/uam/akka/exercise2/actor/Receiver.java: -------------------------------------------------------------------------------- 1 | package com.uam.akka.exercise2.actor; 2 | 3 | import akka.actor.UntypedActor; 4 | import akka.event.Logging; 5 | import akka.event.LoggingAdapter; 6 | import com.uam.akka.exercise2.message.Answer; 7 | import com.uam.akka.exercise2.message.Question; 8 | 9 | public class Receiver extends UntypedActor { 10 | 11 | private final LoggingAdapter log = Logging.getLogger(getContext().system(), this); 12 | 13 | @Override 14 | public void onReceive(Object o) throws Exception { 15 | if (o instanceof Question) { 16 | log.info("Received question: {}", o); 17 | getSender().tell(new Answer("Excellent!"), getSelf()); 18 | } 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /13-akka-communication/solution/src/main/java/com/uam/akka/exercise2/actor/Sender.java: -------------------------------------------------------------------------------- 1 | package com.uam.akka.exercise2.actor; 2 | 3 | import akka.actor.ActorRef; 4 | import akka.actor.Props; 5 | import akka.actor.UntypedActor; 6 | import akka.event.Logging; 7 | import akka.event.LoggingAdapter; 8 | import com.uam.akka.exercise2.message.Answer; 9 | import com.uam.akka.exercise2.message.Question; 10 | import com.uam.akka.exercise2.message.Start; 11 | 12 | public class Sender extends UntypedActor { 13 | 14 | private final LoggingAdapter log = Logging.getLogger(getContext().system(), this); 15 | 16 | public Sender() { 17 | getContext().actorOf(Props.create(Receiver.class), "receiver"); 18 | } 19 | 20 | @Override 21 | public void onReceive(Object o) throws Exception { 22 | if (o instanceof Start) { 23 | ActorRef receiver = getContext().children().iterator().next(); 24 | receiver.tell(new Question("How are you doing?"), getSelf()); 25 | } else if (o instanceof Answer) { 26 | log.info("Receiver sent answer: " + o); 27 | getContext().system().shutdown(); 28 | } 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /13-akka-communication/solution/src/main/java/com/uam/akka/exercise2/message/Answer.java: -------------------------------------------------------------------------------- 1 | package com.uam.akka.exercise2.message; 2 | 3 | public class Answer { 4 | 5 | private final String text; 6 | 7 | public Answer(String text) { 8 | this.text = text; 9 | } 10 | 11 | public String getText() { 12 | return text; 13 | } 14 | 15 | @Override 16 | public String toString() { 17 | return text; 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /13-akka-communication/solution/src/main/java/com/uam/akka/exercise2/message/Question.java: -------------------------------------------------------------------------------- 1 | package com.uam.akka.exercise2.message; 2 | 3 | public class Question { 4 | 5 | private final String text; 6 | 7 | public Question(String text) { 8 | this.text = text; 9 | } 10 | 11 | public String getText() { 12 | return text; 13 | } 14 | 15 | @Override 16 | public String toString() { 17 | return text; 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /13-akka-communication/solution/src/main/java/com/uam/akka/exercise2/message/Start.java: -------------------------------------------------------------------------------- 1 | package com.uam.akka.exercise2.message; 2 | 3 | public class Start { 4 | } 5 | -------------------------------------------------------------------------------- /13-akka-communication/solution/src/main/java/com/uam/akka/exercise3/PrimesFinder.java: -------------------------------------------------------------------------------- 1 | package com.uam.akka.exercise3; 2 | 3 | import akka.actor.ActorRef; 4 | import akka.actor.ActorSystem; 5 | import akka.actor.Props; 6 | import com.uam.akka.exercise3.actor.Master; 7 | import com.uam.akka.exercise3.message.Input; 8 | import com.uam.akka.exercise3.util.Interval; 9 | 10 | public class PrimesFinder { 11 | 12 | public static void main(String[] args) { 13 | 14 | int numberOfWorkers = 8; 15 | Interval interval = new Interval(1000000L, 9999999L); 16 | 17 | ActorSystem system = ActorSystem.create("system"); 18 | ActorRef master = system.actorOf(Props.create(Master.class, numberOfWorkers), "master"); 19 | 20 | master.tell(new Input(interval), ActorRef.noSender()); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /13-akka-communication/solution/src/main/java/com/uam/akka/exercise3/actor/Worker.java: -------------------------------------------------------------------------------- 1 | package com.uam.akka.exercise3.actor; 2 | 3 | import java.util.HashSet; 4 | import java.util.Set; 5 | 6 | import akka.actor.UntypedActor; 7 | import com.uam.akka.exercise3.message.Input; 8 | import com.uam.akka.exercise3.message.Output; 9 | import com.uam.akka.exercise3.util.Interval; 10 | import com.uam.akka.exercise3.util.PrimeChecker; 11 | 12 | public class Worker extends UntypedActor { 13 | 14 | private final Set primes = new HashSet(); 15 | 16 | @Override 17 | public void onReceive(Object o) throws Exception { 18 | 19 | if (o instanceof Input) { 20 | Interval interval = ((Input) o).getInterval(); 21 | 22 | for (long number = interval.from(); number <= interval.to(); ++number) { 23 | if (PrimeChecker.isPrime(number)) { 24 | primes.add(number); 25 | } 26 | } 27 | getSender().tell(new Output(primes), getSelf()); 28 | } 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /13-akka-communication/solution/src/main/java/com/uam/akka/exercise3/message/Input.java: -------------------------------------------------------------------------------- 1 | package com.uam.akka.exercise3.message; 2 | 3 | import com.uam.akka.exercise3.util.Interval; 4 | 5 | public final class Input { 6 | 7 | private final Interval interval; 8 | 9 | public Input(Interval interval) { 10 | this.interval = interval; 11 | } 12 | 13 | public Interval getInterval() { 14 | return interval; 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /13-akka-communication/solution/src/main/java/com/uam/akka/exercise3/message/Output.java: -------------------------------------------------------------------------------- 1 | package com.uam.akka.exercise3.message; 2 | 3 | import java.util.Collections; 4 | import java.util.Set; 5 | 6 | public class Output { 7 | 8 | private final Set primes; 9 | 10 | public Output(Set primes) { 11 | this.primes = Collections.unmodifiableSet(primes); 12 | } 13 | 14 | public Set getPrimes() { 15 | return primes; 16 | } 17 | 18 | @Override 19 | public String toString() { 20 | return "Primes: " + primes; 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /13-akka-communication/solution/src/main/java/com/uam/akka/exercise3/util/Interval.java: -------------------------------------------------------------------------------- 1 | package com.uam.akka.exercise3.util; 2 | 3 | import java.util.ArrayList; 4 | import java.util.Collections; 5 | import java.util.List; 6 | 7 | public final class Interval { 8 | 9 | private final long from; 10 | private final long to; 11 | 12 | public Interval(long from, long to) { 13 | if (from <= 0 || from > to) { 14 | throw new IllegalArgumentException(); 15 | } 16 | this.from = from; 17 | this.to = to; 18 | } 19 | 20 | public long from() { 21 | return from; 22 | } 23 | 24 | public long to() { 25 | return to; 26 | } 27 | 28 | public List divide(int n) { 29 | final List result = new ArrayList(n); 30 | 31 | long distance = to - from + 1; 32 | if (n >= distance) { 33 | for (int i = 0; i < distance; ++i) { 34 | result.add(new Interval(from + i, from + i)); 35 | } 36 | } else { 37 | long size = distance / n; 38 | long extending = distance % n; 39 | 40 | long pointer = from; 41 | for (int i = 0; i < n; ++i) { 42 | long newPointer = pointer + size + (extending-- > 0 ? 1 : 0); 43 | result.add(new Interval(pointer, newPointer - 1)); 44 | pointer = newPointer; 45 | } 46 | } 47 | return Collections.unmodifiableList(result); 48 | } 49 | 50 | @Override 51 | public String toString() { 52 | return "<" + from + ", " + to + ">"; 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /13-akka-communication/solution/src/main/java/com/uam/akka/exercise3/util/PrimeChecker.java: -------------------------------------------------------------------------------- 1 | package com.uam.akka.exercise3.util; 2 | 3 | public class PrimeChecker { 4 | 5 | public static boolean isPrime(long number) { 6 | if (number <= 1) return false; // 1 is not a prime number.. 7 | long i = 2; 8 | while (i * i <= number) { 9 | if (number % i == 0) { 10 | return false; 11 | } 12 | i += 1; 13 | } 14 | return true; 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /13-akka-communication/students/pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4.0.0 4 | 5 | com.uam 6 | distibuted-java 7 | 1.0-SNAPSHOT 8 | 9 | 10 | UTF-8 11 | 12 | 13 | 14 | 15 | com.typesafe.akka 16 | akka-actor_2.10 17 | 2.3.8 18 | 19 | 20 | 21 | -------------------------------------------------------------------------------- /13-akka-communication/students/src/main/java/com/uam/akka/exercise1/Main.java: -------------------------------------------------------------------------------- 1 | package com.uam.akka.exercise1; 2 | 3 | import akka.actor.ActorSystem; 4 | 5 | public class Main { 6 | 7 | public static void main(String[] args) { 8 | ActorSystem system = ActorSystem.create("system"); 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /13-akka-communication/students/src/main/java/com/uam/akka/exercise1/actor/Receiver.java: -------------------------------------------------------------------------------- 1 | package com.uam.akka.exercise1.actor; 2 | 3 | import akka.actor.UntypedActor; 4 | import akka.event.Logging; 5 | import akka.event.LoggingAdapter; 6 | 7 | public class Receiver extends UntypedActor { 8 | 9 | private final LoggingAdapter log = Logging.getLogger(getContext().system(), this); 10 | 11 | @Override 12 | public void onReceive(Object o) throws Exception { 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /13-akka-communication/students/src/main/java/com/uam/akka/exercise1/actor/Sender.java: -------------------------------------------------------------------------------- 1 | package com.uam.akka.exercise1.actor; 2 | 3 | import akka.actor.UntypedActor; 4 | import akka.event.Logging; 5 | import akka.event.LoggingAdapter; 6 | 7 | public class Sender extends UntypedActor { 8 | 9 | private final LoggingAdapter log = Logging.getLogger(getContext().system(), this); 10 | 11 | @Override 12 | public void onReceive(Object o) throws Exception { 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /13-akka-communication/students/src/main/java/com/uam/akka/exercise1/message/Answer.java: -------------------------------------------------------------------------------- 1 | package com.uam.akka.exercise1.message; 2 | 3 | public class Answer { 4 | 5 | private final String text; 6 | 7 | public Answer(String text) { 8 | this.text = text; 9 | } 10 | 11 | public String getText() { 12 | return text; 13 | } 14 | 15 | @Override 16 | public String toString() { 17 | return text; 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /13-akka-communication/students/src/main/java/com/uam/akka/exercise1/message/Question.java: -------------------------------------------------------------------------------- 1 | package com.uam.akka.exercise1.message; 2 | 3 | public class Question { 4 | 5 | private final String text; 6 | 7 | public Question(String text) { 8 | this.text = text; 9 | } 10 | 11 | public String getText() { 12 | return text; 13 | } 14 | 15 | @Override 16 | public String toString() { 17 | return text; 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /13-akka-communication/students/src/main/java/com/uam/akka/exercise1/message/Start.java: -------------------------------------------------------------------------------- 1 | package com.uam.akka.exercise1.message; 2 | 3 | public class Start { 4 | } 5 | -------------------------------------------------------------------------------- /13-akka-communication/students/src/main/java/com/uam/akka/exercise2/Main.java: -------------------------------------------------------------------------------- 1 | package com.uam.akka.exercise2; 2 | 3 | import akka.actor.ActorSystem; 4 | 5 | public class Main { 6 | 7 | public static void main(String[] args) { 8 | ActorSystem system = ActorSystem.create("system"); 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /13-akka-communication/students/src/main/java/com/uam/akka/exercise2/actor/Receiver.java: -------------------------------------------------------------------------------- 1 | package com.uam.akka.exercise2.actor; 2 | 3 | import akka.actor.UntypedActor; 4 | import akka.event.Logging; 5 | import akka.event.LoggingAdapter; 6 | 7 | public class Receiver extends UntypedActor { 8 | 9 | private final LoggingAdapter log = Logging.getLogger(getContext().system(), this); 10 | 11 | @Override 12 | public void onReceive(Object o) throws Exception { 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /13-akka-communication/students/src/main/java/com/uam/akka/exercise2/actor/Sender.java: -------------------------------------------------------------------------------- 1 | package com.uam.akka.exercise2.actor; 2 | 3 | import akka.actor.UntypedActor; 4 | import akka.event.Logging; 5 | import akka.event.LoggingAdapter; 6 | 7 | public class Sender extends UntypedActor { 8 | 9 | private final LoggingAdapter log = Logging.getLogger(getContext().system(), this); 10 | 11 | @Override 12 | public void onReceive(Object o) throws Exception { 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /13-akka-communication/students/src/main/java/com/uam/akka/exercise2/message/Answer.java: -------------------------------------------------------------------------------- 1 | package com.uam.akka.exercise2.message; 2 | 3 | public class Answer { 4 | 5 | private final String text; 6 | 7 | public Answer(String text) { 8 | this.text = text; 9 | } 10 | 11 | public String getText() { 12 | return text; 13 | } 14 | 15 | @Override 16 | public String toString() { 17 | return text; 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /13-akka-communication/students/src/main/java/com/uam/akka/exercise2/message/Question.java: -------------------------------------------------------------------------------- 1 | package com.uam.akka.exercise2.message; 2 | 3 | public class Question { 4 | 5 | private final String text; 6 | 7 | public Question(String text) { 8 | this.text = text; 9 | } 10 | 11 | public String getText() { 12 | return text; 13 | } 14 | 15 | @Override 16 | public String toString() { 17 | return text; 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /13-akka-communication/students/src/main/java/com/uam/akka/exercise2/message/Start.java: -------------------------------------------------------------------------------- 1 | package com.uam.akka.exercise2.message; 2 | 3 | public class Start { 4 | } 5 | -------------------------------------------------------------------------------- /13-akka-communication/students/src/main/java/com/uam/akka/exercise3/PrimesFinder.java: -------------------------------------------------------------------------------- 1 | package com.uam.akka.exercise3; 2 | 3 | import com.uam.akka.exercise3.util.Interval; 4 | 5 | public class PrimesFinder { 6 | 7 | public static void main(String[] args) { 8 | int numberOfWorkers = 4; 9 | Interval interval = new Interval(1000000L, 9999999L); 10 | 11 | // TODO: Find all prime numbers in interval using Akka 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /13-akka-communication/students/src/main/java/com/uam/akka/exercise3/actor/Master.java: -------------------------------------------------------------------------------- 1 | package com.uam.akka.exercise3.actor; 2 | 3 | import akka.actor.UntypedActor; 4 | 5 | public class Master extends UntypedActor { 6 | 7 | private final int numberOfWorkers; 8 | 9 | public Master(int numberOfWorkers) { 10 | this.numberOfWorkers = numberOfWorkers; 11 | } 12 | 13 | @Override 14 | public void onReceive(Object o) throws Exception { 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /13-akka-communication/students/src/main/java/com/uam/akka/exercise3/actor/Worker.java: -------------------------------------------------------------------------------- 1 | package com.uam.akka.exercise3.actor; 2 | 3 | import akka.actor.UntypedActor; 4 | 5 | public class Worker extends UntypedActor { 6 | 7 | @Override 8 | public void onReceive(Object o) throws Exception { 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /13-akka-communication/students/src/main/java/com/uam/akka/exercise3/message/Input.java: -------------------------------------------------------------------------------- 1 | package com.uam.akka.exercise3.message; 2 | 3 | import com.uam.akka.exercise3.util.Interval; 4 | 5 | public final class Input { 6 | 7 | private final Interval interval; 8 | 9 | public Input(Interval interval) { 10 | this.interval = interval; 11 | } 12 | 13 | public Interval getInterval() { 14 | return interval; 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /13-akka-communication/students/src/main/java/com/uam/akka/exercise3/message/Output.java: -------------------------------------------------------------------------------- 1 | package com.uam.akka.exercise3.message; 2 | 3 | import java.util.Collections; 4 | import java.util.Set; 5 | 6 | public class Output { 7 | 8 | private final Set primes; 9 | 10 | public Output(Set primes) { 11 | this.primes = Collections.unmodifiableSet(primes); 12 | } 13 | 14 | public Set getPrimes() { 15 | return primes; 16 | } 17 | 18 | @Override 19 | public String toString() { 20 | return "Primes: " + primes; 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /13-akka-communication/students/src/main/java/com/uam/akka/exercise3/util/Interval.java: -------------------------------------------------------------------------------- 1 | package com.uam.akka.exercise3.util; 2 | 3 | import java.util.ArrayList; 4 | import java.util.Collections; 5 | import java.util.List; 6 | 7 | public final class Interval { 8 | 9 | private final long from; 10 | private final long to; 11 | 12 | public Interval(long from, long to) { 13 | if (from <= 0 || from > to) { 14 | throw new IllegalArgumentException(); 15 | } 16 | this.from = from; 17 | this.to = to; 18 | } 19 | 20 | public long from() { 21 | return from; 22 | } 23 | 24 | public long to() { 25 | return to; 26 | } 27 | 28 | public List divide(int n) { 29 | final List result = new ArrayList(n); 30 | 31 | long distance = to - from + 1; 32 | if (n >= distance) { 33 | for (int i = 0; i < distance; ++i) { 34 | result.add(new Interval(from + i, from + i)); 35 | } 36 | } else { 37 | long size = distance / n; 38 | long extending = distance % n; 39 | 40 | long pointer = from; 41 | for (int i = 0; i < n; ++i) { 42 | long newPointer = pointer + size + (extending-- > 0 ? 1 : 0); 43 | result.add(new Interval(pointer, newPointer - 1)); 44 | pointer = newPointer; 45 | } 46 | } 47 | return Collections.unmodifiableList(result); 48 | } 49 | 50 | @Override 51 | public String toString() { 52 | return "<" + from + ", " + to + ">"; 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /13-akka-communication/students/src/main/java/com/uam/akka/exercise3/util/PrimeChecker.java: -------------------------------------------------------------------------------- 1 | package com.uam.akka.exercise3.util; 2 | 3 | public class PrimeChecker { 4 | 5 | public static boolean isPrime(long number) { 6 | if (number <= 1) return false; // 1 is not a prime number.. 7 | long i = 2; 8 | while (i * i <= number) { 9 | if (number % i == 0) { 10 | return false; 11 | } 12 | i += 1; 13 | } 14 | return true; 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /2-threads-synchronization/solution/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 4.0.0 6 | 7 | com.uam 8 | distibuted-java 9 | 1.0-SNAPSHOT 10 | 11 | 12 | src 13 | 14 | 15 | org.codehaus.mojo 16 | exec-maven-plugin 17 | 1.3.2 18 | 19 | 20 | 21 | java 22 | 23 | test 24 | 25 | 26 | 27 | exercise1.Main 28 | 29 | 30 | 31 | 32 | 33 | -------------------------------------------------------------------------------- /2-threads-synchronization/solution/src/common/Counter.java: -------------------------------------------------------------------------------- 1 | package common; 2 | 3 | public interface Counter { 4 | 5 | void increment(); 6 | 7 | long getValue(); 8 | } 9 | -------------------------------------------------------------------------------- /2-threads-synchronization/solution/src/common/CountingRunner.java: -------------------------------------------------------------------------------- 1 | package common; 2 | 3 | import java.util.concurrent.ExecutorService; 4 | import java.util.concurrent.Executors; 5 | import java.util.concurrent.TimeUnit; 6 | 7 | public class CountingRunner { 8 | 9 | public static final int numberOfThreads = 8; 10 | public static final int numberOfIterations = 1000000; 11 | 12 | public void execute(Counter counter) throws InterruptedException { 13 | ExecutorService executors = Executors.newCachedThreadPool(); 14 | for (int i = 0; i < numberOfThreads; ++i) { 15 | executors.execute(new CountingTask(counter, numberOfIterations)); 16 | } 17 | executors.shutdown(); 18 | executors.awaitTermination(30, TimeUnit.SECONDS); 19 | 20 | System.out.println("Actual: " + counter.getValue() + ", Expected: " + (numberOfThreads * numberOfIterations)); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /2-threads-synchronization/solution/src/common/CountingTask.java: -------------------------------------------------------------------------------- 1 | package common; 2 | 3 | public class CountingTask implements Runnable { 4 | 5 | private final Counter counter; 6 | private final int numberOfIterations; 7 | 8 | public CountingTask(Counter counter, int numberOfIterations) { 9 | this.counter = counter; 10 | this.numberOfIterations = numberOfIterations; 11 | } 12 | 13 | @Override 14 | public void run() { 15 | for (int i = 0; i < numberOfIterations; ++i) { 16 | counter.increment(); 17 | } 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /2-threads-synchronization/solution/src/exercise1/Main.java: -------------------------------------------------------------------------------- 1 | package exercise1; 2 | 3 | import common.CountingRunner; 4 | 5 | public class Main { 6 | 7 | public static void main(String[] args) throws InterruptedException { 8 | SynchronizedCounter counter = new SynchronizedCounter(); 9 | CountingRunner runner = new CountingRunner(); 10 | runner.execute(counter); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /2-threads-synchronization/solution/src/exercise1/SynchronizedCounter.java: -------------------------------------------------------------------------------- 1 | package exercise1; 2 | 3 | import common.Counter; 4 | 5 | public class SynchronizedCounter implements Counter { 6 | 7 | private long value = 0; 8 | 9 | @Override 10 | public synchronized void increment() { 11 | value += 1; 12 | } 13 | 14 | @Override 15 | public long getValue() { 16 | return value; 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /2-threads-synchronization/solution/src/exercise2/LockingCounter.java: -------------------------------------------------------------------------------- 1 | package exercise2; 2 | 3 | import common.Counter; 4 | 5 | import java.util.concurrent.locks.Lock; 6 | import java.util.concurrent.locks.ReentrantLock; 7 | 8 | public class LockingCounter implements Counter { 9 | 10 | private final Lock lock = new ReentrantLock(); 11 | private long value = 0; 12 | 13 | @Override 14 | public void increment() { 15 | lock.lock(); 16 | try { 17 | value += 1; 18 | } finally { 19 | lock.unlock(); 20 | } 21 | } 22 | 23 | @Override 24 | public long getValue() { 25 | return value; 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /2-threads-synchronization/solution/src/exercise2/Main.java: -------------------------------------------------------------------------------- 1 | package exercise2; 2 | 3 | import common.CountingRunner; 4 | 5 | public class Main { 6 | 7 | public static void main(String[] args) throws InterruptedException { 8 | LockingCounter counter = new LockingCounter(); 9 | CountingRunner runner = new CountingRunner(); 10 | runner.execute(counter); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /2-threads-synchronization/solution/src/exercise3/AtomicCounter.java: -------------------------------------------------------------------------------- 1 | package exercise3; 2 | 3 | import common.Counter; 4 | 5 | import java.util.concurrent.atomic.AtomicLong; 6 | 7 | public class AtomicCounter implements Counter { 8 | 9 | private final AtomicLong value = new AtomicLong(); 10 | 11 | @Override 12 | public void increment() { 13 | value.incrementAndGet(); 14 | } 15 | 16 | @Override 17 | public long getValue() { 18 | return value.longValue(); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /2-threads-synchronization/solution/src/exercise3/Main.java: -------------------------------------------------------------------------------- 1 | package exercise3; 2 | 3 | import common.CountingRunner; 4 | 5 | public class Main { 6 | 7 | public static void main(String[] args) throws InterruptedException { 8 | AtomicCounter counter = new AtomicCounter(); 9 | CountingRunner runner = new CountingRunner(); 10 | runner.execute(counter); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /2-threads-synchronization/solution/src/exercise4/EvenCheckingTask.java: -------------------------------------------------------------------------------- 1 | package exercise4; 2 | 3 | import common.Counter; 4 | 5 | public class EvenCheckingTask implements Runnable { 6 | 7 | private final Counter counter; 8 | private final int numberOfIterations; 9 | 10 | public EvenCheckingTask(Counter counter, int numberOfIterations) { 11 | this.counter = counter; 12 | this.numberOfIterations = numberOfIterations; 13 | } 14 | 15 | @Override 16 | public void run() { 17 | for (int i = 0; i < numberOfIterations; ++i) { 18 | synchronized (counter) { 19 | counter.increment(); 20 | counter.increment(); 21 | if (counter.getValue() % 2 != 0) { 22 | System.out.println("Value is not even!"); 23 | break; 24 | } 25 | } 26 | } 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /2-threads-synchronization/solution/src/exercise4/Main.java: -------------------------------------------------------------------------------- 1 | package exercise4; 2 | 3 | import common.Counter; 4 | import exercise3.AtomicCounter; 5 | 6 | import java.util.concurrent.ExecutorService; 7 | import java.util.concurrent.Executors; 8 | import java.util.concurrent.TimeUnit; 9 | 10 | import static common.CountingRunner.numberOfIterations; 11 | import static common.CountingRunner.numberOfThreads; 12 | 13 | public class Main { 14 | 15 | public static void main(String[] args) throws InterruptedException { 16 | Counter counter = new AtomicCounter(); 17 | execute(counter); 18 | } 19 | 20 | private static void execute(Counter counter) throws InterruptedException { 21 | ExecutorService executors = Executors.newCachedThreadPool(); 22 | for (int i = 0; i < numberOfThreads; ++i) { 23 | executors.execute(new EvenCheckingTask(counter, numberOfIterations)); 24 | } 25 | executors.shutdown(); 26 | executors.awaitTermination(30, TimeUnit.SECONDS); 27 | System.out.println("Actual: " + counter.getValue() + ", Expected: " + (2 * numberOfThreads * numberOfIterations)); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /2-threads-synchronization/solution/src/exercise5/Main.java: -------------------------------------------------------------------------------- 1 | package exercise5; 2 | 3 | public class Main { 4 | 5 | public static void main(String[] args) throws InterruptedException { 6 | VolatileTask task = new VolatileTask(); 7 | Thread thread = new Thread(task); 8 | thread.start(); 9 | Thread.sleep(100); 10 | task.end(); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /2-threads-synchronization/solution/src/exercise5/VolatileTask.java: -------------------------------------------------------------------------------- 1 | package exercise5; 2 | 3 | public class VolatileTask implements Runnable { 4 | 5 | private volatile boolean isRunning = true; 6 | 7 | @Override 8 | public void run() { 9 | long counter = 0; 10 | while (isRunning) { 11 | counter += 1; 12 | } 13 | System.out.println("Counter: " + counter); 14 | } 15 | 16 | public void end() { 17 | isRunning = false; 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /2-threads-synchronization/solution/src/exercise6/Main.java: -------------------------------------------------------------------------------- 1 | package exercise6; 2 | 3 | import exercise6.equipment.Brush; 4 | import exercise6.equipment.Paint; 5 | import exercise6.painters.LeftHandedPainter; 6 | import exercise6.painters.RightHandedPainter; 7 | 8 | import java.util.concurrent.ExecutorService; 9 | import java.util.concurrent.Executors; 10 | import java.util.concurrent.TimeUnit; 11 | 12 | public class Main { 13 | 14 | public static void main(String[] args) throws InterruptedException { 15 | Paint paint = new Paint(); 16 | Brush brush = new Brush(); 17 | 18 | ExecutorService executors = Executors.newCachedThreadPool(); 19 | executors.execute(new LeftHandedPainter(paint, brush)); 20 | executors.execute(new RightHandedPainter(paint, brush)); 21 | 22 | executors.shutdown(); 23 | executors.awaitTermination(10, TimeUnit.SECONDS); 24 | if (!executors.isTerminated()) { 25 | System.out.println("Some threads did not finish in 10 seconds!"); 26 | System.out.println("Probably you have a deadlock in your code!"); 27 | } 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /2-threads-synchronization/solution/src/exercise6/equipment/Brush.java: -------------------------------------------------------------------------------- 1 | package exercise6.equipment; 2 | 3 | public class Brush { 4 | 5 | public String takeBrush() { 6 | return "RegularBrush"; 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /2-threads-synchronization/solution/src/exercise6/equipment/Paint.java: -------------------------------------------------------------------------------- 1 | package exercise6.equipment; 2 | 3 | public class Paint { 4 | 5 | public String takePaint() { 6 | return "YellowPaint"; 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /2-threads-synchronization/solution/src/exercise6/painters/LeftHandedPainter.java: -------------------------------------------------------------------------------- 1 | package exercise6.painters; 2 | 3 | import exercise6.equipment.Brush; 4 | import exercise6.equipment.Paint; 5 | 6 | public class LeftHandedPainter extends Painter { 7 | 8 | public LeftHandedPainter(Paint paint, Brush brush) { 9 | super(paint, brush); 10 | } 11 | 12 | @Override 13 | public void run() { 14 | try { 15 | synchronized (paint) { 16 | String takenPaint = paint.takePaint(); 17 | Thread.sleep(100); 18 | 19 | synchronized (brush) { 20 | String takenBrush = this.brush.takeBrush(); 21 | Thread.sleep(100); 22 | 23 | System.out.printf("Left hand painter painting with %s and %s\n", takenPaint, takenBrush); 24 | } 25 | } 26 | } catch (InterruptedException e) { 27 | e.printStackTrace(); 28 | } 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /2-threads-synchronization/solution/src/exercise6/painters/Painter.java: -------------------------------------------------------------------------------- 1 | package exercise6.painters; 2 | 3 | import exercise6.equipment.Brush; 4 | import exercise6.equipment.Paint; 5 | 6 | public abstract class Painter implements Runnable { 7 | 8 | protected final Paint paint; 9 | protected final Brush brush; 10 | 11 | public Painter(Paint paint, Brush brush) { 12 | this.paint = paint; 13 | this.brush = brush; 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /2-threads-synchronization/solution/src/exercise6/painters/RightHandedPainter.java: -------------------------------------------------------------------------------- 1 | package exercise6.painters; 2 | 3 | import exercise6.equipment.Brush; 4 | import exercise6.equipment.Paint; 5 | 6 | public class RightHandedPainter extends Painter { 7 | 8 | public RightHandedPainter(Paint paint, Brush brush) { 9 | super(paint, brush); 10 | } 11 | 12 | @Override 13 | public void run() { 14 | try { 15 | synchronized (paint) { 16 | String takenPaint = paint.takePaint(); 17 | Thread.sleep(100); 18 | 19 | synchronized (brush) { 20 | String takenBrush = brush.takeBrush(); 21 | Thread.sleep(100); 22 | 23 | System.out.printf("Right hand painter painting with %s and %s\n", takenPaint, takenBrush); 24 | } 25 | } 26 | } catch (InterruptedException e) { 27 | e.printStackTrace(); 28 | } 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /2-threads-synchronization/solution/src/exercise7/Main.java: -------------------------------------------------------------------------------- 1 | package exercise7; 2 | 3 | import exercise7.equipment.Brushes; 4 | import exercise7.equipment.Paints; 5 | 6 | import java.util.concurrent.ExecutorService; 7 | import java.util.concurrent.Executors; 8 | import java.util.concurrent.TimeUnit; 9 | 10 | public class Main { 11 | 12 | public static void main(String[] args) throws InterruptedException { 13 | Paints paints = new Paints(); 14 | Brushes brushes = new Brushes(); 15 | 16 | ExecutorService executors = Executors.newCachedThreadPool(); 17 | for (int i = 0; i < 12; ++i) { 18 | executors.execute(new Painter(paints, brushes)); 19 | } 20 | executors.shutdown(); 21 | executors.awaitTermination(30, TimeUnit.SECONDS); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /2-threads-synchronization/solution/src/exercise7/Painter.java: -------------------------------------------------------------------------------- 1 | package exercise7; 2 | 3 | import exercise7.equipment.Brushes; 4 | import exercise7.equipment.Paints; 5 | 6 | public class Painter implements Runnable { 7 | 8 | private final Paints paints; 9 | private final Brushes brushes; 10 | 11 | public Painter(Paints paints, Brushes brushes) { 12 | this.paints = paints; 13 | this.brushes = brushes; 14 | } 15 | 16 | @Override 17 | public void run() { 18 | try { 19 | paints.takePaint(); 20 | Thread.sleep(100); 21 | brushes.takeBrush(); 22 | Thread.sleep(100); 23 | 24 | System.out.println("Painter " + Thread.currentThread().getName() + " is painting!"); 25 | Thread.sleep(500); 26 | } catch (InterruptedException e) { 27 | e.printStackTrace(); 28 | } finally { 29 | paints.returnPaint(); 30 | brushes.returnBrush(); 31 | } 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /2-threads-synchronization/solution/src/exercise7/equipment/Brushes.java: -------------------------------------------------------------------------------- 1 | package exercise7.equipment; 2 | 3 | public class Brushes { 4 | 5 | private int available = 3; 6 | 7 | public synchronized void takeBrush() throws InterruptedException { 8 | while (available == 0) { 9 | wait(); 10 | } 11 | if (available == 0) { 12 | throw new IllegalStateException("There are no more brushes!"); 13 | } 14 | available -= 1; 15 | } 16 | 17 | public synchronized void returnBrush() { 18 | available += 1; 19 | notify(); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /2-threads-synchronization/solution/src/exercise7/equipment/Paints.java: -------------------------------------------------------------------------------- 1 | package exercise7.equipment; 2 | 3 | public class Paints { 4 | 5 | private int available = 3; 6 | 7 | public synchronized void takePaint() throws InterruptedException { 8 | while (available == 0) { 9 | wait(); 10 | } 11 | if (available == 0) { 12 | throw new IllegalStateException("There are no more paints!"); 13 | } 14 | available -= 1; 15 | } 16 | 17 | public synchronized void returnPaint() { 18 | available += 1; 19 | notify(); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /2-threads-synchronization/students/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 4.0.0 6 | 7 | com.uam 8 | distibuted-java 9 | 1.0-SNAPSHOT 10 | 11 | 12 | src 13 | 14 | 15 | org.codehaus.mojo 16 | exec-maven-plugin 17 | 1.3.2 18 | 19 | 20 | 21 | java 22 | 23 | test 24 | 25 | 26 | 27 | exercise1.Main 28 | 29 | 30 | 31 | 32 | 33 | -------------------------------------------------------------------------------- /2-threads-synchronization/students/src/common/Counter.java: -------------------------------------------------------------------------------- 1 | package common; 2 | 3 | public interface Counter { 4 | 5 | void increment(); 6 | 7 | long getValue(); 8 | } 9 | -------------------------------------------------------------------------------- /2-threads-synchronization/students/src/common/CountingRunner.java: -------------------------------------------------------------------------------- 1 | package common; 2 | 3 | import java.util.concurrent.ExecutorService; 4 | import java.util.concurrent.Executors; 5 | import java.util.concurrent.TimeUnit; 6 | 7 | public class CountingRunner { 8 | 9 | public static final int numberOfThreads = 8; 10 | public static final int numberOfIterations = 1000000; 11 | 12 | public void execute(Counter counter) throws InterruptedException { 13 | ExecutorService executors = Executors.newCachedThreadPool(); 14 | for (int i = 0; i < numberOfThreads; ++i) { 15 | executors.execute(new CountingTask(counter, numberOfIterations)); 16 | } 17 | executors.shutdown(); 18 | executors.awaitTermination(30, TimeUnit.SECONDS); 19 | 20 | System.out.println("Actual: " + counter.getValue() + ", Expected: " + (numberOfThreads * numberOfIterations)); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /2-threads-synchronization/students/src/common/CountingTask.java: -------------------------------------------------------------------------------- 1 | package common; 2 | 3 | public class CountingTask implements Runnable { 4 | 5 | private final Counter counter; 6 | private final int numberOfIterations; 7 | 8 | public CountingTask(Counter counter, int numberOfIterations) { 9 | this.counter = counter; 10 | this.numberOfIterations = numberOfIterations; 11 | } 12 | 13 | @Override 14 | public void run() { 15 | for (int i = 0; i < numberOfIterations; ++i) { 16 | counter.increment(); 17 | } 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /2-threads-synchronization/students/src/exercise1/Main.java: -------------------------------------------------------------------------------- 1 | package exercise1; 2 | 3 | import common.CountingRunner; 4 | 5 | public class Main { 6 | 7 | public static void main(String[] args) throws InterruptedException { 8 | SynchronizedCounter counter = new SynchronizedCounter(); 9 | CountingRunner runner = new CountingRunner(); 10 | runner.execute(counter); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /2-threads-synchronization/students/src/exercise1/SynchronizedCounter.java: -------------------------------------------------------------------------------- 1 | package exercise1; 2 | 3 | import common.Counter; 4 | 5 | public class SynchronizedCounter implements Counter { 6 | 7 | @Override 8 | public void increment() { 9 | 10 | } 11 | 12 | @Override 13 | public long getValue() { 14 | return 0; 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /2-threads-synchronization/students/src/exercise2/LockingCounter.java: -------------------------------------------------------------------------------- 1 | package exercise2; 2 | 3 | import common.Counter; 4 | 5 | public class LockingCounter implements Counter { 6 | 7 | @Override 8 | public void increment() { 9 | 10 | } 11 | 12 | @Override 13 | public long getValue() { 14 | return 0; 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /2-threads-synchronization/students/src/exercise2/Main.java: -------------------------------------------------------------------------------- 1 | package exercise2; 2 | 3 | import common.CountingRunner; 4 | 5 | public class Main { 6 | 7 | public static void main(String[] args) throws InterruptedException { 8 | LockingCounter counter = new LockingCounter(); 9 | CountingRunner runner = new CountingRunner(); 10 | runner.execute(counter); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /2-threads-synchronization/students/src/exercise3/AtomicCounter.java: -------------------------------------------------------------------------------- 1 | package exercise3; 2 | 3 | import common.Counter; 4 | 5 | public class AtomicCounter implements Counter { 6 | 7 | @Override 8 | public void increment() { 9 | 10 | } 11 | 12 | @Override 13 | public long getValue() { 14 | return 0; 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /2-threads-synchronization/students/src/exercise3/Main.java: -------------------------------------------------------------------------------- 1 | package exercise3; 2 | 3 | import common.CountingRunner; 4 | 5 | public class Main { 6 | 7 | public static void main(String[] args) throws InterruptedException { 8 | AtomicCounter counter = new AtomicCounter(); 9 | CountingRunner runner = new CountingRunner(); 10 | runner.execute(counter); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /2-threads-synchronization/students/src/exercise4/EvenCheckingTask.java: -------------------------------------------------------------------------------- 1 | package exercise4; 2 | 3 | import common.Counter; 4 | 5 | public class EvenCheckingTask implements Runnable { 6 | 7 | private final Counter counter; 8 | private final int numberOfIterations; 9 | 10 | public EvenCheckingTask(Counter counter, int numberOfIterations) { 11 | this.counter = counter; 12 | this.numberOfIterations = numberOfIterations; 13 | } 14 | 15 | @Override 16 | public void run() { 17 | for (int i = 0; i < numberOfIterations; ++i) { 18 | counter.increment(); 19 | counter.increment(); 20 | if (counter.getValue() % 2 != 0) { 21 | System.out.println("Value is not even!"); 22 | break; 23 | } 24 | } 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /2-threads-synchronization/students/src/exercise4/Main.java: -------------------------------------------------------------------------------- 1 | package exercise4; 2 | 3 | import common.Counter; 4 | 5 | import java.util.concurrent.ExecutorService; 6 | import java.util.concurrent.Executors; 7 | import java.util.concurrent.TimeUnit; 8 | 9 | import static common.CountingRunner.numberOfIterations; 10 | import static common.CountingRunner.numberOfThreads; 11 | 12 | public class Main { 13 | 14 | public static void main(String[] args) throws InterruptedException { 15 | Counter counter = null; // TODO: Provide counter implementation 16 | execute(counter); 17 | } 18 | 19 | private static void execute(Counter counter) throws InterruptedException { 20 | ExecutorService executors = Executors.newCachedThreadPool(); 21 | for (int i = 0; i < numberOfThreads; ++i) { 22 | executors.execute(new EvenCheckingTask(counter, numberOfIterations)); 23 | } 24 | executors.shutdown(); 25 | executors.awaitTermination(30, TimeUnit.SECONDS); 26 | System.out.println("Actual: " + counter.getValue() + ", Expected: " + (2 * numberOfThreads * numberOfIterations)); 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /2-threads-synchronization/students/src/exercise5/Main.java: -------------------------------------------------------------------------------- 1 | package exercise5; 2 | 3 | public class Main { 4 | 5 | public static void main(String[] args) throws InterruptedException { 6 | VolatileTask task = new VolatileTask(); 7 | Thread thread = new Thread(task); 8 | thread.start(); 9 | Thread.sleep(100); 10 | task.end(); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /2-threads-synchronization/students/src/exercise5/VolatileTask.java: -------------------------------------------------------------------------------- 1 | package exercise5; 2 | 3 | public class VolatileTask implements Runnable { 4 | 5 | private boolean isRunning = true; 6 | 7 | @Override 8 | public void run() { 9 | long counter = 0; 10 | while (isRunning) { 11 | counter += 1; 12 | } 13 | System.out.println("Counter: " + counter); 14 | } 15 | 16 | public void end() { 17 | isRunning = false; 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /2-threads-synchronization/students/src/exercise6/Main.java: -------------------------------------------------------------------------------- 1 | package exercise6; 2 | 3 | import exercise6.equipment.Brush; 4 | import exercise6.equipment.Paint; 5 | import exercise6.painters.LeftHandedPainter; 6 | import exercise6.painters.RightHandedPainter; 7 | 8 | import java.util.concurrent.ExecutorService; 9 | import java.util.concurrent.Executors; 10 | import java.util.concurrent.TimeUnit; 11 | 12 | public class Main { 13 | 14 | public static void main(String[] args) throws InterruptedException { 15 | Paint paint = new Paint(); 16 | Brush brush = new Brush(); 17 | 18 | ExecutorService executors = Executors.newCachedThreadPool(); 19 | executors.execute(new LeftHandedPainter(paint, brush)); 20 | executors.execute(new RightHandedPainter(paint, brush)); 21 | 22 | executors.shutdown(); 23 | executors.awaitTermination(10, TimeUnit.SECONDS); 24 | if (!executors.isTerminated()) { 25 | System.out.println("Some threads did not finish in 10 seconds!"); 26 | System.out.println("Probably you have a deadlock in your code!"); 27 | } 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /2-threads-synchronization/students/src/exercise6/equipment/Brush.java: -------------------------------------------------------------------------------- 1 | package exercise6.equipment; 2 | 3 | public class Brush { 4 | 5 | public String takeBrush() { 6 | return "RegularBrush"; 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /2-threads-synchronization/students/src/exercise6/equipment/Paint.java: -------------------------------------------------------------------------------- 1 | package exercise6.equipment; 2 | 3 | public class Paint { 4 | 5 | public String takePaint() { 6 | return "YellowPaint"; 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /2-threads-synchronization/students/src/exercise6/painters/LeftHandedPainter.java: -------------------------------------------------------------------------------- 1 | package exercise6.painters; 2 | 3 | import exercise6.equipment.Brush; 4 | import exercise6.equipment.Paint; 5 | 6 | public class LeftHandedPainter extends Painter { 7 | 8 | public LeftHandedPainter(Paint paint, Brush brush) { 9 | super(paint, brush); 10 | } 11 | 12 | @Override 13 | public void run() { 14 | try { 15 | synchronized (paint) { 16 | String takenPaint = paint.takePaint(); 17 | Thread.sleep(100); 18 | 19 | synchronized (brush) { 20 | String takenBrush = this.brush.takeBrush(); 21 | Thread.sleep(100); 22 | 23 | System.out.printf("Left hand painter painting with %s and %s\n", takenPaint, takenBrush); 24 | } 25 | } 26 | } catch (InterruptedException e) { 27 | e.printStackTrace(); 28 | } 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /2-threads-synchronization/students/src/exercise6/painters/Painter.java: -------------------------------------------------------------------------------- 1 | package exercise6.painters; 2 | 3 | import exercise6.equipment.Brush; 4 | import exercise6.equipment.Paint; 5 | 6 | public abstract class Painter implements Runnable { 7 | 8 | protected final Paint paint; 9 | protected final Brush brush; 10 | 11 | public Painter(Paint paint, Brush brush) { 12 | this.paint = paint; 13 | this.brush = brush; 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /2-threads-synchronization/students/src/exercise6/painters/RightHandedPainter.java: -------------------------------------------------------------------------------- 1 | package exercise6.painters; 2 | 3 | import exercise6.equipment.Brush; 4 | import exercise6.equipment.Paint; 5 | 6 | public class RightHandedPainter extends Painter { 7 | 8 | public RightHandedPainter(Paint paint, Brush brush) { 9 | super(paint, brush); 10 | } 11 | 12 | @Override 13 | public void run() { 14 | try { 15 | synchronized (brush) { 16 | String takenBrush = brush.takeBrush(); 17 | Thread.sleep(100); 18 | 19 | synchronized (paint) { 20 | String takenPaint = paint.takePaint(); 21 | Thread.sleep(100); 22 | 23 | System.out.printf("Right hand painter painting with %s and %s\n", takenPaint, takenBrush); 24 | } 25 | } 26 | } catch (InterruptedException e) { 27 | e.printStackTrace(); 28 | } 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /2-threads-synchronization/students/src/exercise7/Main.java: -------------------------------------------------------------------------------- 1 | package exercise7; 2 | 3 | import exercise7.equipment.Brushes; 4 | import exercise7.equipment.Paints; 5 | 6 | import java.util.concurrent.ExecutorService; 7 | import java.util.concurrent.Executors; 8 | import java.util.concurrent.TimeUnit; 9 | 10 | public class Main { 11 | 12 | public static void main(String[] args) throws InterruptedException { 13 | Paints paints = new Paints(); 14 | Brushes brushes = new Brushes(); 15 | 16 | ExecutorService executors = Executors.newCachedThreadPool(); 17 | for (int i = 0; i < 12; ++i) { 18 | executors.execute(new Painter(paints, brushes)); 19 | } 20 | executors.shutdown(); 21 | executors.awaitTermination(30, TimeUnit.SECONDS); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /2-threads-synchronization/students/src/exercise7/Painter.java: -------------------------------------------------------------------------------- 1 | package exercise7; 2 | 3 | import exercise7.equipment.Brushes; 4 | import exercise7.equipment.Paints; 5 | 6 | public class Painter implements Runnable { 7 | 8 | private final Paints paints; 9 | private final Brushes brushes; 10 | 11 | public Painter(Paints paints, Brushes brushes) { 12 | this.paints = paints; 13 | this.brushes = brushes; 14 | } 15 | 16 | @Override 17 | public void run() { 18 | try { 19 | paints.takePaint(); 20 | Thread.sleep(100); 21 | brushes.takeBrush(); 22 | Thread.sleep(100); 23 | 24 | System.out.println("Painter " + Thread.currentThread().getName() + " is painting!"); 25 | Thread.sleep(500); 26 | } catch (InterruptedException e) { 27 | e.printStackTrace(); 28 | } finally { 29 | paints.returnPaint(); 30 | brushes.returnBrush(); 31 | } 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /2-threads-synchronization/students/src/exercise7/equipment/Brushes.java: -------------------------------------------------------------------------------- 1 | package exercise7.equipment; 2 | 3 | public class Brushes { 4 | 5 | private int available = 3; 6 | 7 | public synchronized void takeBrush() throws InterruptedException { 8 | if (available == 0) { 9 | throw new IllegalStateException("There are no more brushes!"); 10 | } 11 | available -= 1; 12 | } 13 | 14 | public synchronized void returnBrush() { 15 | available += 1; 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /2-threads-synchronization/students/src/exercise7/equipment/Paints.java: -------------------------------------------------------------------------------- 1 | package exercise7.equipment; 2 | 3 | public class Paints { 4 | 5 | private int available = 3; 6 | 7 | public synchronized void takePaint() throws InterruptedException { 8 | if (available == 0) { 9 | throw new IllegalStateException("There are no more paints!"); 10 | } 11 | available -= 1; 12 | } 13 | 14 | public synchronized void returnPaint() { 15 | available += 1; 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /3-threads-higher-abstractions/solution/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 4.0.0 6 | 7 | com.uam 8 | distibuted-java 9 | 1.0-SNAPSHOT 10 | 11 | 12 | src 13 | 14 | 15 | org.codehaus.mojo 16 | exec-maven-plugin 17 | 1.3.2 18 | 19 | 20 | 21 | java 22 | 23 | test 24 | 25 | 26 | 27 | exercise1.Main 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | org.jsoup 36 | jsoup 37 | 1.8.1 38 | 39 | 40 | -------------------------------------------------------------------------------- /3-threads-higher-abstractions/solution/src/common/StringUtils.java: -------------------------------------------------------------------------------- 1 | package common; 2 | 3 | public class StringUtils { 4 | 5 | public static int countOccurrences(String content, String pattern) { 6 | if (content == null || content.isEmpty() || pattern == null || pattern.isEmpty()) { 7 | return 0; 8 | } 9 | int count = 0; 10 | int fromIdx = 0; 11 | int idx; 12 | while ((idx = content.indexOf(pattern, fromIdx)) >= 0) { 13 | fromIdx = idx + pattern.length(); 14 | count += 1; 15 | } 16 | return count; 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /3-threads-higher-abstractions/solution/src/common/html/GazetaHtmlDocument.java: -------------------------------------------------------------------------------- 1 | package common.html; 2 | 3 | public class GazetaHtmlDocument extends HtmlDocument { 4 | 5 | public GazetaHtmlDocument(String documentUrl) { 6 | super(documentUrl); 7 | } 8 | 9 | @Override 10 | protected String getLinkPrefix() { 11 | return "http://wiadomosci.gazeta.pl/wiadomosci"; 12 | } 13 | 14 | @Override 15 | protected String getContentSelector() { 16 | return "#article"; 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /3-threads-higher-abstractions/solution/src/common/html/PudelekHtmlDocument.java: -------------------------------------------------------------------------------- 1 | package common.html; 2 | 3 | public class PudelekHtmlDocument extends HtmlDocument { 4 | 5 | public PudelekHtmlDocument(String documentUrl) { 6 | super(documentUrl); 7 | } 8 | 9 | @Override 10 | protected String getLinkPrefix() { 11 | return "http://www.pudelek.pl/artykul"; 12 | } 13 | 14 | @Override 15 | protected String getContentSelector() { 16 | return ".single-entry"; 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /3-threads-higher-abstractions/solution/src/exercise1/Main.java: -------------------------------------------------------------------------------- 1 | package exercise1; 2 | 3 | import exercise1.equipment.Brushes; 4 | import exercise1.equipment.Paints; 5 | 6 | import java.util.concurrent.ExecutorService; 7 | import java.util.concurrent.Executors; 8 | import java.util.concurrent.TimeUnit; 9 | 10 | public class Main { 11 | 12 | public static void main(String[] args) throws InterruptedException { 13 | Paints paints = new Paints(); 14 | Brushes brushes = new Brushes(); 15 | 16 | ExecutorService executors = Executors.newCachedThreadPool(); 17 | for (int i = 0; i < 12; ++i) { 18 | executors.execute(new Painter(paints, brushes)); 19 | } 20 | executors.shutdown(); 21 | executors.awaitTermination(30, TimeUnit.SECONDS); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /3-threads-higher-abstractions/solution/src/exercise1/Painter.java: -------------------------------------------------------------------------------- 1 | package exercise1; 2 | 3 | import exercise1.equipment.Brushes; 4 | import exercise1.equipment.Paints; 5 | 6 | public class Painter implements Runnable { 7 | 8 | private final Paints paints; 9 | private final Brushes brushes; 10 | 11 | public Painter(Paints paints, Brushes brushes) { 12 | this.paints = paints; 13 | this.brushes = brushes; 14 | } 15 | 16 | @Override 17 | public void run() { 18 | try { 19 | paints.takePaint(); 20 | Thread.sleep(100); 21 | brushes.takeBrush(); 22 | Thread.sleep(100); 23 | 24 | System.out.println("Painter " + Thread.currentThread().getName() + " is painting!"); 25 | Thread.sleep(500); 26 | } catch (InterruptedException e) { 27 | e.printStackTrace(); 28 | } finally { 29 | paints.returnPaint(); 30 | brushes.returnBrush(); 31 | } 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /3-threads-higher-abstractions/solution/src/exercise1/equipment/Brushes.java: -------------------------------------------------------------------------------- 1 | package exercise1.equipment; 2 | 3 | import java.util.concurrent.locks.Condition; 4 | import java.util.concurrent.locks.Lock; 5 | import java.util.concurrent.locks.ReentrantLock; 6 | 7 | public class Brushes { 8 | 9 | private final Lock lock = new ReentrantLock(); 10 | private final Condition condition = lock.newCondition(); 11 | 12 | private int available = 3; 13 | 14 | public void takeBrush() throws InterruptedException { 15 | lock.lock(); 16 | try { 17 | while (available == 0) { 18 | condition.await(); 19 | } 20 | if (available == 0) { 21 | throw new IllegalStateException("There are no more brushes!"); 22 | } 23 | available -= 1; 24 | } finally { 25 | lock.unlock(); 26 | } 27 | } 28 | 29 | public void returnBrush() { 30 | lock.lock(); 31 | try { 32 | available += 1; 33 | condition.signal(); 34 | } finally { 35 | lock.unlock(); 36 | } 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /3-threads-higher-abstractions/solution/src/exercise1/equipment/Paints.java: -------------------------------------------------------------------------------- 1 | package exercise1.equipment; 2 | 3 | import java.util.concurrent.locks.Condition; 4 | import java.util.concurrent.locks.Lock; 5 | import java.util.concurrent.locks.ReentrantLock; 6 | 7 | public class Paints { 8 | 9 | private final Lock lock = new ReentrantLock(); 10 | private final Condition condition = lock.newCondition(); 11 | 12 | private int available = 3; 13 | 14 | public void takePaint() throws InterruptedException { 15 | lock.lock(); 16 | try { 17 | while (available == 0) { 18 | condition.await(); 19 | } 20 | if (available == 0) { 21 | throw new IllegalStateException("There are no more paints!"); 22 | } 23 | available -= 1; 24 | } finally { 25 | lock.unlock(); 26 | } 27 | } 28 | 29 | public void returnPaint() { 30 | lock.lock(); 31 | try { 32 | available += 1; 33 | condition.signal(); 34 | } finally { 35 | lock.unlock(); 36 | } 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /3-threads-higher-abstractions/solution/src/exercise2/Main.java: -------------------------------------------------------------------------------- 1 | package exercise2; 2 | 3 | import exercise2.equipment.Brushes; 4 | import exercise2.equipment.Paints; 5 | 6 | import java.util.concurrent.ExecutorService; 7 | import java.util.concurrent.Executors; 8 | import java.util.concurrent.TimeUnit; 9 | 10 | public class Main { 11 | 12 | public static void main(String[] args) throws InterruptedException { 13 | Paints paints = new Paints(); 14 | Brushes brushes = new Brushes(); 15 | 16 | ExecutorService executors = Executors.newCachedThreadPool(); 17 | for (int i = 0; i < 32; ++i) { 18 | executors.execute(new Painter(paints, brushes)); 19 | } 20 | executors.shutdown(); 21 | executors.awaitTermination(30, TimeUnit.SECONDS); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /3-threads-higher-abstractions/solution/src/exercise2/Painter.java: -------------------------------------------------------------------------------- 1 | package exercise2; 2 | 3 | import exercise2.equipment.Brushes; 4 | import exercise2.equipment.Paints; 5 | 6 | import java.util.concurrent.ThreadLocalRandom; 7 | 8 | public class Painter implements Runnable { 9 | 10 | private final Paints paints; 11 | private final Brushes brushes; 12 | 13 | public Painter(Paints paints, Brushes brushes) { 14 | this.paints = paints; 15 | this.brushes = brushes; 16 | } 17 | 18 | @Override 19 | public void run() { 20 | String paint = null; 21 | String brush = null; 22 | 23 | try { 24 | Thread.sleep(ThreadLocalRandom.current().nextInt(200)); 25 | paint = paints.takePaint(); 26 | 27 | Thread.sleep(ThreadLocalRandom.current().nextInt(200)); 28 | brush = brushes.takeBrush(); 29 | 30 | Thread.sleep(ThreadLocalRandom.current().nextInt(1000)); 31 | System.out.printf("Painter %s is painting with %s paint and %s brush\n", 32 | Thread.currentThread().getName(), paint, brush); 33 | 34 | } catch (InterruptedException e) { 35 | e.printStackTrace(); 36 | } finally { 37 | if (paint != null) { 38 | paints.returnPaint(paint); 39 | } 40 | if (brush != null) { 41 | brushes.returnBrush(brush); 42 | } 43 | } 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /3-threads-higher-abstractions/solution/src/exercise2/equipment/Brushes.java: -------------------------------------------------------------------------------- 1 | package exercise2.equipment; 2 | 3 | import java.util.concurrent.ArrayBlockingQueue; 4 | import java.util.concurrent.BlockingQueue; 5 | 6 | public class Brushes { 7 | 8 | private final BlockingQueue queue = new ArrayBlockingQueue(3); 9 | 10 | public Brushes() { 11 | queue.offer("regular"); 12 | queue.offer("triangular"); 13 | queue.offer("spectacular"); 14 | } 15 | 16 | public String takeBrush() throws InterruptedException { 17 | return queue.take(); 18 | } 19 | 20 | public void returnBrush(String brush) { 21 | queue.offer(brush); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /3-threads-higher-abstractions/solution/src/exercise2/equipment/Paints.java: -------------------------------------------------------------------------------- 1 | package exercise2.equipment; 2 | 3 | import java.util.concurrent.ArrayBlockingQueue; 4 | import java.util.concurrent.BlockingQueue; 5 | 6 | public class Paints { 7 | 8 | private final BlockingQueue queue = new ArrayBlockingQueue(3); 9 | 10 | public Paints() { 11 | queue.offer("red"); 12 | queue.offer("green"); 13 | queue.offer("blue"); 14 | } 15 | 16 | public String takePaint() throws InterruptedException { 17 | return queue.take(); 18 | } 19 | 20 | public void returnPaint(String paint) { 21 | queue.offer(paint); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /3-threads-higher-abstractions/solution/src/exercise3/Main.java: -------------------------------------------------------------------------------- 1 | package exercise3; 2 | 3 | import common.html.GazetaHtmlDocument; 4 | import common.html.HtmlDocument; 5 | 6 | import java.util.ArrayList; 7 | import java.util.List; 8 | import java.util.Set; 9 | import java.util.concurrent.ExecutorService; 10 | import java.util.concurrent.Executors; 11 | import java.util.concurrent.Future; 12 | 13 | public class Main { 14 | 15 | public static void main(String[] args) throws Exception { 16 | HtmlDocument rootDocument = new GazetaHtmlDocument("http://wiadomosci.gazeta.pl/"); 17 | Set links = rootDocument.getLinks(); 18 | String wordToFound = "sikorski"; 19 | 20 | ExecutorService executorService = Executors.newCachedThreadPool(); 21 | 22 | List> futures = new ArrayList>(links.size()); 23 | for (String link : links) { 24 | WordCounter wordCounter = new WordCounter(link, wordToFound); 25 | Future future = executorService.submit(wordCounter); 26 | futures.add(future); 27 | } 28 | 29 | executorService.shutdown(); 30 | 31 | int numberOfWords = 0; 32 | for (Future future : futures) { 33 | numberOfWords += future.get(); 34 | } 35 | System.out.printf("Number of words '%s': %d", wordToFound, numberOfWords); 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /3-threads-higher-abstractions/solution/src/exercise3/WordCounter.java: -------------------------------------------------------------------------------- 1 | package exercise3; 2 | 3 | import common.StringUtils; 4 | import common.html.GazetaHtmlDocument; 5 | import common.html.HtmlDocument; 6 | 7 | import java.util.concurrent.Callable; 8 | 9 | public class WordCounter implements Callable { 10 | 11 | private final String documentUrl; 12 | private final String wordToCount; 13 | 14 | public WordCounter(String documentUrl, String wordToCount) { 15 | this.documentUrl = documentUrl; 16 | this.wordToCount = wordToCount; 17 | } 18 | 19 | @Override 20 | public Integer call() throws Exception { 21 | System.out.printf("Looking for words '%s' in article %s\n", wordToCount, documentUrl); 22 | HtmlDocument document = new GazetaHtmlDocument(documentUrl); 23 | String content = document.getContent().toLowerCase(); 24 | 25 | return StringUtils.countOccurrences(content, wordToCount); 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /3-threads-higher-abstractions/solution/src/exercise4/Main.java: -------------------------------------------------------------------------------- 1 | package exercise4; 2 | 3 | import java.util.concurrent.ForkJoinPool; 4 | 5 | public class Main { 6 | 7 | public static void main(String[] args) { 8 | String rootUrl = "http://wiadomosci.gazeta.pl/"; 9 | String wordToFound = "sikorski"; 10 | 11 | ForkJoinPool forkJoinPool = new ForkJoinPool(); 12 | WebCrawlingTask webCrawlingTask = new WebCrawlingTask(rootUrl, wordToFound); 13 | Integer numberOfWords = forkJoinPool.invoke(webCrawlingTask); 14 | 15 | System.out.printf("Number of words '%s': %d", wordToFound, numberOfWords); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /3-threads-higher-abstractions/solution/src/exercise4/WordCountingTask.java: -------------------------------------------------------------------------------- 1 | package exercise4; 2 | 3 | import common.StringUtils; 4 | 5 | import java.util.concurrent.RecursiveTask; 6 | 7 | public class WordCountingTask extends RecursiveTask { 8 | 9 | private final String content; 10 | private final String wordToCount; 11 | 12 | public WordCountingTask(String content, String wordToCount) { 13 | this.content = content.toLowerCase(); 14 | this.wordToCount = wordToCount; 15 | } 16 | 17 | @Override 18 | protected Integer compute() { 19 | return StringUtils.countOccurrences(content, wordToCount); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /3-threads-higher-abstractions/students/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 4.0.0 6 | 7 | com.uam 8 | distibuted-java 9 | 1.0-SNAPSHOT 10 | 11 | 12 | src 13 | 14 | 15 | org.codehaus.mojo 16 | exec-maven-plugin 17 | 1.3.2 18 | 19 | 20 | 21 | java 22 | 23 | test 24 | 25 | 26 | 27 | exercise1.Main 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | org.jsoup 36 | jsoup 37 | 1.8.1 38 | 39 | 40 | -------------------------------------------------------------------------------- /3-threads-higher-abstractions/students/src/common/StringUtils.java: -------------------------------------------------------------------------------- 1 | package common; 2 | 3 | public class StringUtils { 4 | 5 | public static int countOccurrences(String content, String pattern) { 6 | if (content == null || content.isEmpty() || pattern == null || pattern.isEmpty()) { 7 | return 0; 8 | } 9 | int count = 0; 10 | int fromIdx = 0; 11 | int idx; 12 | while ((idx = content.indexOf(pattern, fromIdx)) >= 0) { 13 | fromIdx = idx + pattern.length(); 14 | count += 1; 15 | } 16 | return count; 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /3-threads-higher-abstractions/students/src/common/html/GazetaHtmlDocument.java: -------------------------------------------------------------------------------- 1 | package common.html; 2 | 3 | public class GazetaHtmlDocument extends HtmlDocument { 4 | 5 | public GazetaHtmlDocument(String documentUrl) { 6 | super(documentUrl); 7 | } 8 | 9 | @Override 10 | protected String getLinkPrefix() { 11 | return "http://wiadomosci.gazeta.pl/wiadomosci"; 12 | } 13 | 14 | @Override 15 | protected String getContentSelector() { 16 | return "#article"; 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /3-threads-higher-abstractions/students/src/common/html/PudelekHtmlDocument.java: -------------------------------------------------------------------------------- 1 | package common.html; 2 | 3 | public class PudelekHtmlDocument extends HtmlDocument { 4 | 5 | public PudelekHtmlDocument(String documentUrl) { 6 | super(documentUrl); 7 | } 8 | 9 | @Override 10 | protected String getLinkPrefix() { 11 | return "http://www.pudelek.pl/artykul"; 12 | } 13 | 14 | @Override 15 | protected String getContentSelector() { 16 | return ".single-entry"; 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /3-threads-higher-abstractions/students/src/exercise1/Main.java: -------------------------------------------------------------------------------- 1 | package exercise1; 2 | 3 | import exercise1.equipment.Brushes; 4 | import exercise1.equipment.Paints; 5 | 6 | import java.util.concurrent.ExecutorService; 7 | import java.util.concurrent.Executors; 8 | import java.util.concurrent.TimeUnit; 9 | 10 | public class Main { 11 | 12 | public static void main(String[] args) throws InterruptedException { 13 | Paints paints = new Paints(); 14 | Brushes brushes = new Brushes(); 15 | 16 | ExecutorService executors = Executors.newCachedThreadPool(); 17 | for (int i = 0; i < 12; ++i) { 18 | executors.execute(new Painter(paints, brushes)); 19 | } 20 | executors.shutdown(); 21 | executors.awaitTermination(30, TimeUnit.SECONDS); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /3-threads-higher-abstractions/students/src/exercise1/Painter.java: -------------------------------------------------------------------------------- 1 | package exercise1; 2 | 3 | import exercise1.equipment.Brushes; 4 | import exercise1.equipment.Paints; 5 | 6 | public class Painter implements Runnable { 7 | 8 | private final Paints paints; 9 | private final Brushes brushes; 10 | 11 | public Painter(Paints paints, Brushes brushes) { 12 | this.paints = paints; 13 | this.brushes = brushes; 14 | } 15 | 16 | @Override 17 | public void run() { 18 | try { 19 | paints.takePaint(); 20 | Thread.sleep(100); 21 | brushes.takeBrush(); 22 | Thread.sleep(100); 23 | 24 | System.out.println("Painter " + Thread.currentThread().getName() + " is painting!"); 25 | Thread.sleep(500); 26 | } catch (InterruptedException e) { 27 | e.printStackTrace(); 28 | } finally { 29 | paints.returnPaint(); 30 | brushes.returnBrush(); 31 | } 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /3-threads-higher-abstractions/students/src/exercise1/equipment/Brushes.java: -------------------------------------------------------------------------------- 1 | package exercise1.equipment; 2 | 3 | public class Brushes { 4 | private int available = 3; 5 | 6 | public void takeBrush() throws InterruptedException { 7 | if (available == 0) { 8 | throw new IllegalStateException("There are no more brushes!"); 9 | } 10 | available -= 1; 11 | } 12 | 13 | public void returnBrush() { 14 | available += 1; 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /3-threads-higher-abstractions/students/src/exercise1/equipment/Paints.java: -------------------------------------------------------------------------------- 1 | package exercise1.equipment; 2 | 3 | public class Paints { 4 | private int available = 3; 5 | 6 | public void takePaint() throws InterruptedException { 7 | if (available == 0) { 8 | throw new IllegalStateException("There are no more paints!"); 9 | } 10 | available -= 1; 11 | } 12 | 13 | public void returnPaint() { 14 | available += 1; 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /3-threads-higher-abstractions/students/src/exercise2/Main.java: -------------------------------------------------------------------------------- 1 | package exercise2; 2 | 3 | import exercise2.equipment.Brushes; 4 | import exercise2.equipment.Paints; 5 | 6 | import java.util.concurrent.ExecutorService; 7 | import java.util.concurrent.Executors; 8 | import java.util.concurrent.TimeUnit; 9 | 10 | public class Main { 11 | 12 | public static void main(String[] args) throws InterruptedException { 13 | Paints paints = new Paints(); 14 | Brushes brushes = new Brushes(); 15 | 16 | ExecutorService executors = Executors.newCachedThreadPool(); 17 | for (int i = 0; i < 32; ++i) { 18 | executors.execute(new Painter(paints, brushes)); 19 | } 20 | executors.shutdown(); 21 | executors.awaitTermination(30, TimeUnit.SECONDS); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /3-threads-higher-abstractions/students/src/exercise2/Painter.java: -------------------------------------------------------------------------------- 1 | package exercise2; 2 | 3 | import exercise2.equipment.Brushes; 4 | import exercise2.equipment.Paints; 5 | 6 | import java.util.concurrent.ThreadLocalRandom; 7 | 8 | public class Painter implements Runnable { 9 | 10 | private final Paints paints; 11 | private final Brushes brushes; 12 | 13 | public Painter(Paints paints, Brushes brushes) { 14 | this.paints = paints; 15 | this.brushes = brushes; 16 | } 17 | 18 | @Override 19 | public void run() { 20 | String paint = null; 21 | String brush = null; 22 | 23 | try { 24 | Thread.sleep(ThreadLocalRandom.current().nextInt(200)); 25 | paint = paints.takePaint(); 26 | 27 | Thread.sleep(ThreadLocalRandom.current().nextInt(200)); 28 | brush = brushes.takeBrush(); 29 | 30 | Thread.sleep(ThreadLocalRandom.current().nextInt(1000)); 31 | System.out.printf("Painter %s is painting with %s paint and %s brush\n", 32 | Thread.currentThread().getName(), paint, brush); 33 | 34 | } catch (InterruptedException e) { 35 | e.printStackTrace(); 36 | } finally { 37 | if (paint != null) { 38 | paints.returnPaint(paint); 39 | } 40 | if (brush != null) { 41 | brushes.returnBrush(brush); 42 | } 43 | } 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /3-threads-higher-abstractions/students/src/exercise2/equipment/Brushes.java: -------------------------------------------------------------------------------- 1 | package exercise2.equipment; 2 | 3 | public class Brushes { 4 | 5 | public String takeBrush() throws InterruptedException { 6 | return null; 7 | } 8 | 9 | public void returnBrush(String brush) { 10 | 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /3-threads-higher-abstractions/students/src/exercise2/equipment/Paints.java: -------------------------------------------------------------------------------- 1 | package exercise2.equipment; 2 | 3 | public class Paints { 4 | 5 | public String takePaint() throws InterruptedException { 6 | return null; 7 | } 8 | 9 | public void returnPaint(String paint) { 10 | 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /3-threads-higher-abstractions/students/src/exercise3/Main.java: -------------------------------------------------------------------------------- 1 | package exercise3; 2 | 3 | import common.html.GazetaHtmlDocument; 4 | import common.html.HtmlDocument; 5 | 6 | import java.util.Set; 7 | 8 | public class Main { 9 | 10 | public static void main(String[] args) throws Exception { 11 | HtmlDocument rootDocument = new GazetaHtmlDocument("http://wiadomosci.gazeta.pl/"); 12 | Set links = rootDocument.getLinks(); 13 | String wordToFound = "sikorski"; 14 | 15 | // TODO: Create ExecutorService 16 | 17 | // TODO: Create list of results of type List> 18 | 19 | for (String link : links) { 20 | // TODO: Create new WordCounter and submit it to executorService 21 | // TODO: Store Future object in list of results 22 | } 23 | 24 | // TODO: shutdown executor 25 | 26 | int numberOfWords = 0; 27 | // TODO: Iterate over list of results and for each Future invoke get() method 28 | // TODO: add value returned from get() method to numberOfWords variable 29 | 30 | System.out.printf("Number of words '%s': %d", wordToFound, numberOfWords); 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /3-threads-higher-abstractions/students/src/exercise3/WordCounter.java: -------------------------------------------------------------------------------- 1 | package exercise3; 2 | 3 | public class WordCounter { 4 | 5 | private final String documentUrl; 6 | private final String wordToCount; 7 | 8 | public WordCounter(String documentUrl, String wordToCount) { 9 | this.documentUrl = documentUrl; 10 | this.wordToCount = wordToCount; 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /3-threads-higher-abstractions/students/src/exercise4/Main.java: -------------------------------------------------------------------------------- 1 | package exercise4; 2 | 3 | public class Main { 4 | 5 | public static void main(String[] args) { 6 | String rootUrl = "http://wiadomosci.gazeta.pl/"; 7 | String wordToFound = "sikorski"; 8 | Integer numberOfWords = 0; 9 | 10 | // TODO: Create new ForkJoinPool object 11 | // TODO: Create new WebCrawlingTask for rootUrl and wordToFound 12 | // TODO: Invoke invoke method on ForkJoinPool object passing WebCrawlingTask 13 | // TODO: Assign result of invoke method to numberOfWords variable 14 | 15 | System.out.printf("Number of words '%s': %d", wordToFound, numberOfWords); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /3-threads-higher-abstractions/students/src/exercise4/WordCountingTask.java: -------------------------------------------------------------------------------- 1 | package exercise4; 2 | 3 | import common.StringUtils; 4 | 5 | import java.util.concurrent.RecursiveTask; 6 | 7 | public class WordCountingTask extends RecursiveTask { 8 | 9 | private final String content; 10 | private final String wordToCount; 11 | 12 | public WordCountingTask(String content, String wordToCount) { 13 | this.content = content.toLowerCase(); 14 | this.wordToCount = wordToCount; 15 | } 16 | 17 | @Override 18 | protected Integer compute() { 19 | return StringUtils.countOccurrences(content, wordToCount); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /4-JMS-basics/SayHello/Broker/pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4 | pl.edu.amu.dji.jms.lab4 5 | SayHello 6 | 1.0-SNAPSHOT 7 | 8 | 4.0.0 9 | 10 | Broker 11 | jar 12 | 13 | Broker 14 | http://maven.apache.org 15 | 16 | 17 | UTF-8 18 | 19 | 20 | -------------------------------------------------------------------------------- /4-JMS-basics/SayHello/Broker/src/main/java/pl/edu/amu/dji/jms/lab1/Broker.java: -------------------------------------------------------------------------------- 1 | package pl.edu.amu.dji.jms.lab1; 2 | 3 | import org.apache.activemq.broker.BrokerService; 4 | 5 | public class Broker { 6 | public static void main(String[] args) throws Exception { 7 | BrokerService brokerService = new BrokerService(); 8 | brokerService.addConnector("tcp://localhost:61616"); 9 | brokerService.start(); 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /4-JMS-basics/SayHello/Hello/pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4 | pl.edu.amu.dji.jms.lab4 5 | SayHello 6 | 1.0-SNAPSHOT 7 | 8 | 4.0.0 9 | 10 | Hello 11 | jar 12 | 13 | Hello 14 | http://maven.apache.org 15 | 16 | 17 | UTF-8 18 | 19 | 20 | -------------------------------------------------------------------------------- /4-JMS-basics/SayHello/Say/pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4 | pl.edu.amu.dji.jms.lab4 5 | SayHello 6 | 1.0-SNAPSHOT 7 | 8 | 4.0.0 9 | 10 | Say 11 | jar 12 | 13 | Say 14 | http://maven.apache.org 15 | 16 | 17 | UTF-8 18 | 19 | 20 | -------------------------------------------------------------------------------- /4-JMS-basics/SayHello/Say/src/main/java/pl/edu/amu/dji/jms/lab1/SayMain.java: -------------------------------------------------------------------------------- 1 | package pl.edu.amu.dji.jms.lab1; 2 | 3 | import org.apache.activemq.ActiveMQConnectionFactory; 4 | 5 | import javax.jms.*; 6 | import java.io.BufferedReader; 7 | import java.io.InputStreamReader; 8 | 9 | public class SayMain { 10 | 11 | public static final String EXIT = "exit"; 12 | 13 | public static void main(String[] args) throws Exception { 14 | ConnectionFactory connectionFactory = new ActiveMQConnectionFactory("tcp://localhost:61616"); 15 | Connection connection = connectionFactory.createConnection(); 16 | Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); 17 | Destination queue = session.createQueue("SayHelloQueue"); 18 | MessageProducer producer = session.createProducer(queue); 19 | 20 | producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT); 21 | 22 | connection.start(); 23 | BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in)); 24 | String in = ""; 25 | while (!in.equalsIgnoreCase(EXIT)) { 26 | System.out.print("Say hello to:"); 27 | in = bufferedReader.readLine(); 28 | TextMessage message = session.createTextMessage(in); 29 | producer.send(message); 30 | } 31 | 32 | session.close(); 33 | connection.close(); 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /4-JMS-basics/SayHello/pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4.0.0 4 | 5 | pl.edu.amu.dji.jms.lab4 6 | SayHello 7 | 1.0-SNAPSHOT 8 | 9 | Broker 10 | Say 11 | Hello 12 | 13 | pom 14 | 15 | SayHello 16 | http://maven.apache.org 17 | 18 | 19 | UTF-8 20 | 21 | 22 | 23 | 24 | org.apache.activemq 25 | activemq-broker 26 | 5.9.0 27 | compile 28 | 29 | 30 | org.apache.activemq 31 | activemq-kahadb-store 32 | 5.9.0 33 | compile 34 | 35 | 36 | 37 | -------------------------------------------------------------------------------- /4-JMS-basics/Solution/SayHello/Broker/pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4 | pl.edu.amu.dji.jms.lab4 5 | SayHello 6 | 1.0-SNAPSHOT 7 | 8 | 4.0.0 9 | 10 | Broker 11 | jar 12 | 13 | Broker 14 | http://maven.apache.org 15 | 16 | 17 | UTF-8 18 | 19 | 20 | -------------------------------------------------------------------------------- /4-JMS-basics/Solution/SayHello/Broker/src/main/java/pl/edu/amu/dji/jms/lab1/Broker.java: -------------------------------------------------------------------------------- 1 | package pl.edu.amu.dji.jms.lab1; 2 | 3 | import org.apache.activemq.broker.BrokerService; 4 | 5 | public class Broker { 6 | public static void main(String[] args) throws Exception { 7 | BrokerService brokerService = new BrokerService(); 8 | brokerService.addConnector("tcp://localhost:61616"); 9 | brokerService.start(); 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /4-JMS-basics/Solution/SayHello/Hello/pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4 | pl.edu.amu.dji.jms.lab4 5 | SayHello 6 | 1.0-SNAPSHOT 7 | 8 | 4.0.0 9 | 10 | Hello 11 | jar 12 | 13 | Hello 14 | http://maven.apache.org 15 | 16 | 17 | UTF-8 18 | 19 | 20 | -------------------------------------------------------------------------------- /4-JMS-basics/Solution/SayHello/Say/pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4 | pl.edu.amu.dji.jms.lab4 5 | SayHello 6 | 1.0-SNAPSHOT 7 | 8 | 4.0.0 9 | 10 | Say 11 | jar 12 | 13 | Say 14 | http://maven.apache.org 15 | 16 | 17 | UTF-8 18 | 19 | 20 | -------------------------------------------------------------------------------- /4-JMS-basics/Solution/SayHello/pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4.0.0 4 | 5 | pl.edu.amu.dji.jms.lab4 6 | SayHello 7 | 1.0-SNAPSHOT 8 | 9 | Broker 10 | Say 11 | Hello 12 | 13 | pom 14 | 15 | SayHello 16 | http://maven.apache.org 17 | 18 | 19 | UTF-8 20 | 21 | 22 | 23 | 24 | org.apache.activemq 25 | activemq-broker 26 | 5.9.0 27 | compile 28 | 29 | 30 | org.apache.activemq 31 | activemq-kahadb-store 32 | 5.9.0 33 | compile 34 | 35 | 36 | 37 | -------------------------------------------------------------------------------- /5-spring-jms/solution/spring-jms/jms-server/pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4 | spring-jms 5 | pl.edu.amu.dji.jms.lab2 6 | 1.0-SNAPSHOT 7 | 8 | 4.0.0 9 | 10 | pl.edu.amu.dji.jms.lab2.jmsserver 11 | jms-server 12 | jar 13 | 14 | jms-server 15 | http://maven.apache.org 16 | 17 | 18 | UTF-8 19 | 20 | 21 | -------------------------------------------------------------------------------- /5-spring-jms/solution/spring-jms/jms-server/src/main/java/pl/edu/amu/dji/jms/lab2/jmsserver/JMSServer.java: -------------------------------------------------------------------------------- 1 | package pl.edu.amu.dji.jms.lab2.jmsserver; 2 | 3 | import org.apache.activemq.broker.BrokerService; 4 | 5 | public class JMSServer { 6 | public static void main(String[] args) throws Exception { 7 | BrokerService brokerService = new BrokerService(); 8 | brokerService.addConnector("tcp://localhost:61616"); 9 | brokerService.start(); 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /5-spring-jms/solution/spring-jms/retailer/pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4 | spring-jms 5 | pl.edu.amu.dji.jms.lab2 6 | 1.0-SNAPSHOT 7 | 8 | 4.0.0 9 | 10 | pl.edu.amu.dji.jms.lab2.retailer 11 | retailer 12 | jar 13 | 14 | retailer 15 | http://maven.apache.org 16 | 17 | 18 | UTF-8 19 | 20 | 21 | -------------------------------------------------------------------------------- /5-spring-jms/solution/spring-jms/retailer/src/main/java/pl/edu/amu/dji/jms/lab2/retailer/RetailerApp.java: -------------------------------------------------------------------------------- 1 | package pl.edu.amu.dji.jms.lab2.retailer; 2 | 3 | import org.apache.xbean.spring.context.ClassPathXmlApplicationContext; 4 | 5 | public class RetailerApp { 6 | public static void main(String[] args) { 7 | new ClassPathXmlApplicationContext("/context.xml"); 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /5-spring-jms/solution/spring-jms/retailer/src/main/resources/context.xml: -------------------------------------------------------------------------------- 1 | 2 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | -------------------------------------------------------------------------------- /5-spring-jms/solution/spring-jms/wholesaler/pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4 | spring-jms 5 | pl.edu.amu.dji.jms.lab2 6 | 1.0-SNAPSHOT 7 | 8 | 4.0.0 9 | 10 | pl.edu.amu.dji.jms.lab2.wholesaler 11 | wholesaler 12 | jar 13 | 14 | wholesaler 15 | http://maven.apache.org 16 | 17 | 18 | UTF-8 19 | 20 | 21 | -------------------------------------------------------------------------------- /5-spring-jms/solution/spring-jms/wholesaler/src/main/java/pl/edu/amu/dji/jms/lab2/wholesaler/WholesalerApp.java: -------------------------------------------------------------------------------- 1 | package pl.edu.amu.dji.jms.lab2.wholesaler; 2 | 3 | import org.apache.commons.lang3.math.NumberUtils; 4 | import org.apache.xbean.spring.context.ClassPathXmlApplicationContext; 5 | import org.springframework.context.ApplicationContext; 6 | import pl.edu.amu.dji.jms.lab2.wholesaler.service.OfferService; 7 | 8 | import java.io.BufferedReader; 9 | import java.io.InputStreamReader; 10 | 11 | public class WholesalerApp { 12 | public static final String EXIT = "exit"; 13 | 14 | public static void main(String[] args) throws Exception{ 15 | ApplicationContext context = new ClassPathXmlApplicationContext("/context.xml"); 16 | OfferService offerService = (OfferService) context.getBean("offerService"); 17 | 18 | BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in)); 19 | String in = ""; 20 | while (!in.equalsIgnoreCase(EXIT)) { 21 | System.out.print("New price:"); 22 | in = bufferedReader.readLine(); 23 | 24 | if(NumberUtils.isNumber(in)){ 25 | offerService.sendOffer(Double.valueOf(in)); 26 | } 27 | } 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /5-spring-jms/solution/spring-jms/wholesaler/src/main/java/pl/edu/amu/dji/jms/lab2/wholesaler/service/OfferService.java: -------------------------------------------------------------------------------- 1 | package pl.edu.amu.dji.jms.lab2.wholesaler.service; 2 | 3 | import org.springframework.jms.core.JmsTemplate; 4 | import org.springframework.jms.core.MessageCreator; 5 | 6 | import javax.jms.*; 7 | 8 | public class OfferService { 9 | 10 | private JmsTemplate jmsTemplate; 11 | 12 | private Destination offerTopic; 13 | 14 | private Destination orderQueue; 15 | 16 | public void setJmsTemplate(JmsTemplate jmsTemplate) { 17 | this.jmsTemplate = jmsTemplate; 18 | } 19 | 20 | public void setOfferTopic(Destination offerTopic) { 21 | this.offerTopic = offerTopic; 22 | } 23 | 24 | public void setOrderQueue(Destination orderQueue) { 25 | this.orderQueue = orderQueue; 26 | } 27 | 28 | public void sendOffer(final Double price){ 29 | jmsTemplate.send(offerTopic, new MessageCreator() { 30 | @Override 31 | public Message createMessage(Session session) throws JMSException { 32 | MapMessage message = session.createMapMessage(); 33 | message.setDouble("price", price); 34 | message.setJMSReplyTo(orderQueue); 35 | 36 | return message; 37 | } 38 | }); 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /5-spring-jms/solution/spring-jms/wholesaler/src/main/java/pl/edu/amu/dji/jms/lab2/wholesaler/service/OrderService.java: -------------------------------------------------------------------------------- 1 | package pl.edu.amu.dji.jms.lab2.wholesaler.service; 2 | 3 | import com.google.common.base.Preconditions; 4 | 5 | import javax.jms.JMSException; 6 | import javax.jms.MapMessage; 7 | import javax.jms.Message; 8 | import javax.jms.MessageListener; 9 | 10 | public class OrderService implements MessageListener{ 11 | 12 | @Override 13 | public void onMessage(Message message) { 14 | try{ 15 | Preconditions.checkArgument(message instanceof MapMessage); 16 | 17 | MapMessage mapMessage = (MapMessage) message; 18 | int quantity = mapMessage.getInt("quantity"); 19 | String retailerID = mapMessage.getString("retailerID"); 20 | 21 | System.out.println("Ordered quantity: " + quantity + " by " + retailerID); 22 | } catch (JMSException ex){ 23 | throw new IllegalStateException(ex); 24 | } 25 | 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /5-spring-jms/students/spring-jms/jms-server/pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4 | spring-jms 5 | pl.edu.amu.dji.jms.lab2 6 | 1.0-SNAPSHOT 7 | 8 | 4.0.0 9 | 10 | pl.edu.amu.dji.jms.lab2.jmsserver 11 | jms-server 12 | jar 13 | 14 | jms-server 15 | http://maven.apache.org 16 | 17 | 18 | UTF-8 19 | 20 | 21 | -------------------------------------------------------------------------------- /5-spring-jms/students/spring-jms/jms-server/src/main/java/pl/edu/amu/dji/jms/lab2/jmsserver/JMSServer.java: -------------------------------------------------------------------------------- 1 | package pl.edu.amu.dji.jms.lab2.jmsserver; 2 | 3 | import org.apache.activemq.broker.BrokerService; 4 | 5 | public class JMSServer { 6 | public static void main(String[] args) throws Exception { 7 | BrokerService brokerService = new BrokerService(); 8 | brokerService.addConnector("tcp://localhost:61616"); 9 | brokerService.start(); 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /5-spring-jms/students/spring-jms/retailer/pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4 | spring-jms 5 | pl.edu.amu.dji.jms.lab2 6 | 1.0-SNAPSHOT 7 | 8 | 4.0.0 9 | 10 | pl.edu.amu.dji.jms.lab2.retailer 11 | retailer 12 | jar 13 | 14 | retailer 15 | http://maven.apache.org 16 | 17 | 18 | UTF-8 19 | 20 | 21 | -------------------------------------------------------------------------------- /5-spring-jms/students/spring-jms/retailer/src/main/java/pl/edu/amu/dji/jms/lab2/retailer/RetailerApp.java: -------------------------------------------------------------------------------- 1 | package pl.edu.amu.dji.jms.lab2.retailer; 2 | 3 | import org.apache.xbean.spring.context.ClassPathXmlApplicationContext; 4 | 5 | public class RetailerApp { 6 | public static void main(String[] args) { 7 | new ClassPathXmlApplicationContext("/context.xml"); 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /5-spring-jms/students/spring-jms/retailer/src/main/java/pl/edu/amu/dji/jms/lab2/retailer/service/BuyService.java: -------------------------------------------------------------------------------- 1 | package pl.edu.amu.dji.jms.lab2.retailer.service; 2 | 3 | import org.springframework.jms.core.JmsTemplate; 4 | 5 | import javax.jms.Message; 6 | import javax.jms.MessageListener; 7 | 8 | public class BuyService implements MessageListener { 9 | 10 | private JmsTemplate jmsTemplate; 11 | 12 | private Double maxPrice; 13 | 14 | public void setJmsTemplate(JmsTemplate jmsTemplate) { 15 | this.jmsTemplate = jmsTemplate; 16 | } 17 | 18 | public void setMaxPrice(Double maxPrice) { 19 | this.maxPrice = maxPrice; 20 | } 21 | 22 | @Override 23 | public void onMessage(Message message) { 24 | throw new UnsupportedOperationException(); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /5-spring-jms/students/spring-jms/retailer/src/main/resources/context.xml: -------------------------------------------------------------------------------- 1 | 2 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | -------------------------------------------------------------------------------- /5-spring-jms/students/spring-jms/wholesaler/pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4 | spring-jms 5 | pl.edu.amu.dji.jms.lab2 6 | 1.0-SNAPSHOT 7 | 8 | 4.0.0 9 | 10 | pl.edu.amu.dji.jms.lab2.wholesaler 11 | wholesaler 12 | jar 13 | 14 | wholesaler 15 | http://maven.apache.org 16 | 17 | 18 | UTF-8 19 | 20 | 21 | -------------------------------------------------------------------------------- /5-spring-jms/students/spring-jms/wholesaler/src/main/java/pl/edu/amu/dji/jms/lab2/wholesaler/WholesalerApp.java: -------------------------------------------------------------------------------- 1 | package pl.edu.amu.dji.jms.lab2.wholesaler; 2 | 3 | import org.apache.commons.lang3.math.NumberUtils; 4 | import org.apache.xbean.spring.context.ClassPathXmlApplicationContext; 5 | import org.springframework.context.ApplicationContext; 6 | import pl.edu.amu.dji.jms.lab2.wholesaler.service.OfferService; 7 | 8 | import java.io.BufferedReader; 9 | import java.io.InputStreamReader; 10 | 11 | public class WholesalerApp { 12 | public static final String EXIT = "exit"; 13 | 14 | public static void main(String[] args) throws Exception { 15 | ApplicationContext context = new ClassPathXmlApplicationContext("/context.xml"); 16 | OfferService offerService = (OfferService) context.getBean("offerService"); 17 | 18 | BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in)); 19 | String in = ""; 20 | while (!in.equalsIgnoreCase(EXIT)) { 21 | System.out.print("New price:"); 22 | in = bufferedReader.readLine(); 23 | 24 | if (NumberUtils.isNumber(in)) { 25 | offerService.sendOffer(Double.valueOf(in)); 26 | } 27 | } 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /5-spring-jms/students/spring-jms/wholesaler/src/main/java/pl/edu/amu/dji/jms/lab2/wholesaler/service/OfferService.java: -------------------------------------------------------------------------------- 1 | package pl.edu.amu.dji.jms.lab2.wholesaler.service; 2 | 3 | import org.springframework.jms.core.JmsTemplate; 4 | 5 | import javax.jms.Destination; 6 | 7 | public class OfferService { 8 | 9 | private JmsTemplate jmsTemplate; 10 | 11 | private Destination offerTopic; 12 | 13 | private Destination orderQueue; 14 | 15 | public void setJmsTemplate(JmsTemplate jmsTemplate) { 16 | this.jmsTemplate = jmsTemplate; 17 | } 18 | 19 | public void setOfferTopic(Destination offerTopic) { 20 | this.offerTopic = offerTopic; 21 | } 22 | 23 | public void setOrderQueue(Destination orderQueue) { 24 | this.orderQueue = orderQueue; 25 | } 26 | 27 | public void sendOffer(final Double price) { 28 | throw new UnsupportedOperationException(); 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /5-spring-jms/students/spring-jms/wholesaler/src/main/java/pl/edu/amu/dji/jms/lab2/wholesaler/service/OrderService.java: -------------------------------------------------------------------------------- 1 | package pl.edu.amu.dji.jms.lab2.wholesaler.service; 2 | 3 | import javax.jms.Message; 4 | import javax.jms.MessageListener; 5 | 6 | public class OrderService implements MessageListener { 7 | 8 | @Override 9 | public void onMessage(Message message) { 10 | throw new UnsupportedOperationException(); 11 | 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /5-spring-jms/students/spring-jms/wholesaler/src/main/resources/context.xml: -------------------------------------------------------------------------------- 1 | 2 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | -------------------------------------------------------------------------------- /6-tx-spring-jms/solution/spring-jms/jms-server/pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4 | spring-jms 5 | pl.edu.amu.dji.jms.lab2 6 | 1.0-SNAPSHOT 7 | 8 | 4.0.0 9 | 10 | pl.edu.amu.dji.jms.lab2.jmsserver 11 | jms-server 12 | jar 13 | 14 | jms-server 15 | http://maven.apache.org 16 | 17 | 18 | UTF-8 19 | 20 | 21 | -------------------------------------------------------------------------------- /6-tx-spring-jms/solution/spring-jms/jms-server/src/main/java/pl/edu/amu/dji/jms/lab2/jmsserver/JMSServer.java: -------------------------------------------------------------------------------- 1 | package pl.edu.amu.dji.jms.lab2.jmsserver; 2 | 3 | import org.apache.activemq.broker.BrokerService; 4 | 5 | public class JMSServer { 6 | public static void main(String[] args) throws Exception { 7 | BrokerService brokerService = new BrokerService(); 8 | brokerService.addConnector("tcp://localhost:61616"); 9 | brokerService.start(); 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /6-tx-spring-jms/solution/spring-jms/retailer/pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4 | spring-jms 5 | pl.edu.amu.dji.jms.lab2 6 | 1.0-SNAPSHOT 7 | 8 | 4.0.0 9 | 10 | pl.edu.amu.dji.jms.lab2.retailer 11 | retailer 12 | jar 13 | 14 | retailer 15 | http://maven.apache.org 16 | 17 | 18 | UTF-8 19 | 20 | 21 | -------------------------------------------------------------------------------- /6-tx-spring-jms/solution/spring-jms/retailer/src/main/java/pl/edu/amu/dji/jms/lab2/retailer/RetailerApp.java: -------------------------------------------------------------------------------- 1 | package pl.edu.amu.dji.jms.lab2.retailer; 2 | 3 | import org.apache.xbean.spring.context.ClassPathXmlApplicationContext; 4 | 5 | public class RetailerApp { 6 | public static void main(String[] args) { 7 | new ClassPathXmlApplicationContext("/context.xml"); 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /6-tx-spring-jms/solution/spring-jms/retailer/src/main/resources/context.xml: -------------------------------------------------------------------------------- 1 | 2 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | -------------------------------------------------------------------------------- /6-tx-spring-jms/solution/spring-jms/wholesaler/pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4 | spring-jms 5 | pl.edu.amu.dji.jms.lab2 6 | 1.0-SNAPSHOT 7 | 8 | 4.0.0 9 | 10 | pl.edu.amu.dji.jms.lab2.wholesaler 11 | wholesaler 12 | jar 13 | 14 | wholesaler 15 | http://maven.apache.org 16 | 17 | 18 | UTF-8 19 | 20 | 21 | -------------------------------------------------------------------------------- /6-tx-spring-jms/solution/spring-jms/wholesaler/src/main/java/pl/edu/amu/dji/jms/lab2/wholesaler/WholesalerApp.java: -------------------------------------------------------------------------------- 1 | package pl.edu.amu.dji.jms.lab2.wholesaler; 2 | 3 | import org.apache.commons.lang3.math.NumberUtils; 4 | import org.apache.xbean.spring.context.ClassPathXmlApplicationContext; 5 | import org.springframework.context.ApplicationContext; 6 | import pl.edu.amu.dji.jms.lab2.wholesaler.service.OfferService; 7 | 8 | import java.io.BufferedReader; 9 | import java.io.InputStreamReader; 10 | 11 | public class WholesalerApp { 12 | public static final String EXIT = "exit"; 13 | 14 | public static void main(String[] args) throws Exception{ 15 | ApplicationContext context = new ClassPathXmlApplicationContext("/context.xml"); 16 | OfferService offerService = (OfferService) context.getBean("offerService"); 17 | 18 | BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in)); 19 | String in = ""; 20 | while (!in.equalsIgnoreCase(EXIT)) { 21 | System.out.print("New price:"); 22 | in = bufferedReader.readLine(); 23 | 24 | if(NumberUtils.isNumber(in)){ 25 | try{ 26 | offerService.sendOffer(Double.valueOf(in)); 27 | } catch (IllegalStateException ex){ 28 | ex.printStackTrace(); 29 | } 30 | } 31 | } 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /6-tx-spring-jms/solution/spring-jms/wholesaler/src/main/java/pl/edu/amu/dji/jms/lab2/wholesaler/service/OfferService.java: -------------------------------------------------------------------------------- 1 | package pl.edu.amu.dji.jms.lab2.wholesaler.service; 2 | 3 | import org.springframework.beans.factory.annotation.Autowired; 4 | import org.springframework.beans.factory.annotation.Qualifier; 5 | import org.springframework.jms.core.JmsTemplate; 6 | import org.springframework.stereotype.Service; 7 | import org.springframework.transaction.annotation.Transactional; 8 | import pl.edu.amu.dji.jms.lab2.wholesaler.service.message.Offer; 9 | 10 | import javax.jms.Destination; 11 | 12 | @Service("offerService") 13 | public class OfferService { 14 | 15 | @Autowired 16 | @Qualifier("offerJmsTemplate") 17 | private JmsTemplate jmsTemplate; 18 | 19 | @Autowired 20 | @Qualifier("orderQueue") 21 | private Destination orderQueue; 22 | 23 | @Transactional 24 | public void sendOffer(final Double price){ 25 | Offer offer = new Offer(price, orderQueue); 26 | jmsTemplate.convertAndSend(offer); 27 | 28 | if(price < 5){ 29 | throw new IllegalStateException(); 30 | } 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /6-tx-spring-jms/solution/spring-jms/wholesaler/src/main/java/pl/edu/amu/dji/jms/lab2/wholesaler/service/OrderService.java: -------------------------------------------------------------------------------- 1 | package pl.edu.amu.dji.jms.lab2.wholesaler.service; 2 | 3 | import org.springframework.stereotype.Service; 4 | import org.springframework.transaction.annotation.Transactional; 5 | import pl.edu.amu.dji.jms.lab2.wholesaler.service.message.Order; 6 | 7 | @Service("orderService") 8 | public class OrderService { 9 | @Transactional 10 | public void order(Order order) { 11 | System.out.println("Ordered quantity: " + order.getQuantity() + " by " + order.getRetailerID()); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /6-tx-spring-jms/solution/spring-jms/wholesaler/src/main/java/pl/edu/amu/dji/jms/lab2/wholesaler/service/message/Offer.java: -------------------------------------------------------------------------------- 1 | package pl.edu.amu.dji.jms.lab2.wholesaler.service.message; 2 | 3 | import javax.jms.Destination; 4 | 5 | /** 6 | * User: mateuszjancy Date: 11/11/14 10:39 AM 7 | */ 8 | public class Offer { 9 | private Double price; 10 | private Destination replyTo; 11 | 12 | public Offer() { 13 | } 14 | 15 | public Offer(Double price, Destination replyTo) { 16 | this.price = price; 17 | this.replyTo = replyTo; 18 | } 19 | 20 | public Double getPrice() { 21 | return price; 22 | } 23 | 24 | public void setPrice(Double price) { 25 | this.price = price; 26 | } 27 | 28 | public Destination getReplyTo() { 29 | return replyTo; 30 | } 31 | 32 | public void setReplyTo(Destination replyTo) { 33 | this.replyTo = replyTo; 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /6-tx-spring-jms/solution/spring-jms/wholesaler/src/main/java/pl/edu/amu/dji/jms/lab2/wholesaler/service/message/OfferConverter.java: -------------------------------------------------------------------------------- 1 | package pl.edu.amu.dji.jms.lab2.wholesaler.service.message; 2 | 3 | import com.google.common.base.Preconditions; 4 | import org.springframework.jms.support.converter.MessageConversionException; 5 | import org.springframework.jms.support.converter.MessageConverter; 6 | import org.springframework.stereotype.Component; 7 | 8 | import javax.jms.JMSException; 9 | import javax.jms.MapMessage; 10 | import javax.jms.Message; 11 | import javax.jms.Session; 12 | 13 | @Component("offerConverter") 14 | public class OfferConverter implements MessageConverter { 15 | 16 | @Override 17 | public Message toMessage(Object object, Session session) throws JMSException, MessageConversionException { 18 | Preconditions.checkArgument(object instanceof Offer); 19 | 20 | Offer offer = (Offer) object; 21 | 22 | MapMessage message = session.createMapMessage(); 23 | message.setDouble("price", offer.getPrice()); 24 | message.setJMSReplyTo(offer.getReplyTo()); 25 | 26 | return message; 27 | } 28 | 29 | @Override 30 | public Object fromMessage(Message message) throws JMSException, MessageConversionException { 31 | throw new UnsupportedOperationException(); 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /6-tx-spring-jms/solution/spring-jms/wholesaler/src/main/java/pl/edu/amu/dji/jms/lab2/wholesaler/service/message/Order.java: -------------------------------------------------------------------------------- 1 | package pl.edu.amu.dji.jms.lab2.wholesaler.service.message; 2 | 3 | /** 4 | * User: mateuszjancy Date: 11/11/14 10:43 AM 5 | */ 6 | public class Order { 7 | private int quantity; 8 | private String retailerID; 9 | 10 | public Order() { 11 | } 12 | 13 | public Order(int quantity, String retailerID) { 14 | this.quantity = quantity; 15 | this.retailerID = retailerID; 16 | } 17 | 18 | public int getQuantity() { 19 | return quantity; 20 | } 21 | 22 | public void setQuantity(int quantity) { 23 | this.quantity = quantity; 24 | } 25 | 26 | public String getRetailerID() { 27 | return retailerID; 28 | } 29 | 30 | public void setRetailerID(String retailerID) { 31 | this.retailerID = retailerID; 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /6-tx-spring-jms/solution/spring-jms/wholesaler/src/main/java/pl/edu/amu/dji/jms/lab2/wholesaler/service/message/OrderConverter.java: -------------------------------------------------------------------------------- 1 | package pl.edu.amu.dji.jms.lab2.wholesaler.service.message; 2 | 3 | import com.google.common.base.Preconditions; 4 | import org.apache.commons.lang3.StringUtils; 5 | import org.springframework.jms.support.converter.MessageConversionException; 6 | import org.springframework.jms.support.converter.MessageConverter; 7 | import org.springframework.stereotype.Component; 8 | 9 | import javax.jms.JMSException; 10 | import javax.jms.MapMessage; 11 | import javax.jms.Message; 12 | import javax.jms.Session; 13 | 14 | @Component("orderConverter") 15 | public class OrderConverter implements MessageConverter { 16 | @Override 17 | public Message toMessage(Object object, Session session) throws JMSException, MessageConversionException { 18 | throw new UnsupportedOperationException(); 19 | } 20 | 21 | @Override 22 | public Order fromMessage(Message message) throws JMSException, MessageConversionException { 23 | Preconditions.checkArgument(message instanceof MapMessage); 24 | 25 | MapMessage mapMessage = (MapMessage) message; 26 | int quantity = mapMessage.getInt("quantity"); 27 | String retailerID = mapMessage.getString("retailerID"); 28 | 29 | Preconditions.checkState(quantity > 0); 30 | Preconditions.checkState(StringUtils.isNotEmpty(retailerID)); 31 | 32 | return new Order(quantity, retailerID); 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /6-tx-spring-jms/students/spring-jms/jms-server/pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4 | spring-jms 5 | pl.edu.amu.dji.jms.lab2 6 | 1.0-SNAPSHOT 7 | 8 | 4.0.0 9 | 10 | pl.edu.amu.dji.jms.lab2.jmsserver 11 | jms-server 12 | jar 13 | 14 | jms-server 15 | http://maven.apache.org 16 | 17 | 18 | UTF-8 19 | 20 | 21 | -------------------------------------------------------------------------------- /6-tx-spring-jms/students/spring-jms/jms-server/src/main/java/pl/edu/amu/dji/jms/lab2/jmsserver/JMSServer.java: -------------------------------------------------------------------------------- 1 | package pl.edu.amu.dji.jms.lab2.jmsserver; 2 | 3 | import org.apache.activemq.broker.BrokerService; 4 | 5 | public class JMSServer { 6 | public static void main(String[] args) throws Exception { 7 | BrokerService brokerService = new BrokerService(); 8 | brokerService.addConnector("tcp://localhost:61616"); 9 | brokerService.start(); 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /6-tx-spring-jms/students/spring-jms/retailer/pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4 | spring-jms 5 | pl.edu.amu.dji.jms.lab2 6 | 1.0-SNAPSHOT 7 | 8 | 4.0.0 9 | 10 | pl.edu.amu.dji.jms.lab2.retailer 11 | retailer 12 | jar 13 | 14 | retailer 15 | http://maven.apache.org 16 | 17 | 18 | UTF-8 19 | 20 | 21 | -------------------------------------------------------------------------------- /6-tx-spring-jms/students/spring-jms/retailer/src/main/java/pl/edu/amu/dji/jms/lab2/retailer/RetailerApp.java: -------------------------------------------------------------------------------- 1 | package pl.edu.amu.dji.jms.lab2.retailer; 2 | 3 | import org.apache.xbean.spring.context.ClassPathXmlApplicationContext; 4 | 5 | public class RetailerApp { 6 | public static void main(String[] args) { 7 | new ClassPathXmlApplicationContext("/context.xml"); 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /6-tx-spring-jms/students/spring-jms/retailer/src/main/resources/context.xml: -------------------------------------------------------------------------------- 1 | 2 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | -------------------------------------------------------------------------------- /6-tx-spring-jms/students/spring-jms/wholesaler/pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4 | spring-jms 5 | pl.edu.amu.dji.jms.lab2 6 | 1.0-SNAPSHOT 7 | 8 | 4.0.0 9 | 10 | pl.edu.amu.dji.jms.lab2.wholesaler 11 | wholesaler 12 | jar 13 | 14 | wholesaler 15 | http://maven.apache.org 16 | 17 | 18 | UTF-8 19 | 20 | 21 | -------------------------------------------------------------------------------- /6-tx-spring-jms/students/spring-jms/wholesaler/src/main/java/pl/edu/amu/dji/jms/lab2/wholesaler/WholesalerApp.java: -------------------------------------------------------------------------------- 1 | package pl.edu.amu.dji.jms.lab2.wholesaler; 2 | 3 | import org.apache.commons.lang3.math.NumberUtils; 4 | import org.apache.xbean.spring.context.ClassPathXmlApplicationContext; 5 | import org.springframework.context.ApplicationContext; 6 | import pl.edu.amu.dji.jms.lab2.wholesaler.service.OfferService; 7 | 8 | import java.io.BufferedReader; 9 | import java.io.InputStreamReader; 10 | 11 | public class WholesalerApp { 12 | public static final String EXIT = "exit"; 13 | 14 | public static void main(String[] args) throws Exception{ 15 | ApplicationContext context = new ClassPathXmlApplicationContext("/context.xml"); 16 | OfferService offerService = (OfferService) context.getBean("offerService"); 17 | 18 | BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in)); 19 | String in = ""; 20 | while (!in.equalsIgnoreCase(EXIT)) { 21 | System.out.print("New price:"); 22 | in = bufferedReader.readLine(); 23 | 24 | if(NumberUtils.isNumber(in)){ 25 | offerService.sendOffer(Double.valueOf(in)); 26 | } 27 | } 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /6-tx-spring-jms/students/spring-jms/wholesaler/src/main/java/pl/edu/amu/dji/jms/lab2/wholesaler/service/OfferService.java: -------------------------------------------------------------------------------- 1 | package pl.edu.amu.dji.jms.lab2.wholesaler.service; 2 | 3 | import org.springframework.jms.core.JmsTemplate; 4 | import org.springframework.jms.core.MessageCreator; 5 | 6 | import javax.jms.*; 7 | 8 | public class OfferService { 9 | 10 | private JmsTemplate jmsTemplate; 11 | 12 | private Destination offerTopic; 13 | 14 | private Destination orderQueue; 15 | 16 | public void setJmsTemplate(JmsTemplate jmsTemplate) { 17 | this.jmsTemplate = jmsTemplate; 18 | } 19 | 20 | public void setOfferTopic(Destination offerTopic) { 21 | this.offerTopic = offerTopic; 22 | } 23 | 24 | public void setOrderQueue(Destination orderQueue) { 25 | this.orderQueue = orderQueue; 26 | } 27 | 28 | public void sendOffer(final Double price){ 29 | jmsTemplate.send(offerTopic, new MessageCreator() { 30 | @Override 31 | public Message createMessage(Session session) throws JMSException { 32 | MapMessage message = session.createMapMessage(); 33 | message.setDouble("price", price); 34 | message.setJMSReplyTo(orderQueue); 35 | 36 | return message; 37 | } 38 | }); 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /6-tx-spring-jms/students/spring-jms/wholesaler/src/main/java/pl/edu/amu/dji/jms/lab2/wholesaler/service/OrderService.java: -------------------------------------------------------------------------------- 1 | package pl.edu.amu.dji.jms.lab2.wholesaler.service; 2 | 3 | import com.google.common.base.Preconditions; 4 | 5 | import javax.jms.JMSException; 6 | import javax.jms.MapMessage; 7 | import javax.jms.Message; 8 | import javax.jms.MessageListener; 9 | 10 | public class OrderService implements MessageListener{ 11 | 12 | @Override 13 | public void onMessage(Message message) { 14 | try{ 15 | Preconditions.checkArgument(message instanceof MapMessage); 16 | 17 | MapMessage mapMessage = (MapMessage) message; 18 | int quantity = mapMessage.getInt("quantity"); 19 | String retailerID = mapMessage.getString("retailerID"); 20 | 21 | System.out.println("Ordered quantity: " + quantity + " by " + retailerID); 22 | } catch (JMSException ex){ 23 | throw new IllegalStateException(ex); 24 | } 25 | 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /7-JMS-summary-and-project-kickoff/students/supermarket/analysis/pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4 | supermarket 5 | pl.edu.amu.dji.jms.lab4 6 | 1.0-SNAPSHOT 7 | 8 | 4.0.0 9 | 10 | pl.edu.amu.dji.jms.lab4 11 | analysis 12 | jar 13 | 14 | analysis 15 | http://maven.apache.org 16 | 17 | 18 | UTF-8 19 | 20 | 21 | 22 | 23 | pl.edu.amu.dji.jms.lab4 24 | common 25 | ${parent.version} 26 | 27 | 28 | 29 | -------------------------------------------------------------------------------- /7-JMS-summary-and-project-kickoff/students/supermarket/analysis/src/main/java/pl/edu/amu/dji/jms/lab4/analysis/AnalysisApp.java: -------------------------------------------------------------------------------- 1 | package pl.edu.amu.dji.jms.lab4.analysis; 2 | 3 | public class AnalysisApp { 4 | public static void main(String[] args) { 5 | System.out.println("Hello World!"); 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /7-JMS-summary-and-project-kickoff/students/supermarket/analysis/src/main/resources/keep: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bsodzik/distributed-java-intro/6ae83484ab6acd0336d2c64458bb78949d8d98a0/7-JMS-summary-and-project-kickoff/students/supermarket/analysis/src/main/resources/keep -------------------------------------------------------------------------------- /7-JMS-summary-and-project-kickoff/students/supermarket/common/pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4 | supermarket 5 | pl.edu.amu.dji.jms.lab4 6 | 1.0-SNAPSHOT 7 | 8 | 4.0.0 9 | 10 | pl.edu.amu.dji.jms.lab4 11 | common 12 | jar 13 | 14 | common 15 | http://maven.apache.org 16 | 17 | 18 | UTF-8 19 | 20 | 21 | -------------------------------------------------------------------------------- /7-JMS-summary-and-project-kickoff/students/supermarket/common/src/main/java/pl/edu/amu/dji/jms/lab4/CommonApp.java: -------------------------------------------------------------------------------- 1 | package pl.edu.amu.dji.jms.lab4; 2 | 3 | public class CommonApp { 4 | public static void main(String[] args) { 5 | System.out.println("Hello World!"); 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /7-JMS-summary-and-project-kickoff/students/supermarket/jms-server/pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4 | supermarket 5 | pl.edu.amu.dji.jms.lab4 6 | 1.0-SNAPSHOT 7 | 8 | 4.0.0 9 | 10 | pl.edu.amu.dji.jms.lab2.jmsserver 11 | jms-server 12 | jar 13 | 14 | jms-server 15 | http://maven.apache.org 16 | 17 | 18 | UTF-8 19 | 20 | 21 | -------------------------------------------------------------------------------- /7-JMS-summary-and-project-kickoff/students/supermarket/jms-server/src/main/java/pl/edu/amu/dji/jms/lab2/jmsserver/JMSServer.java: -------------------------------------------------------------------------------- 1 | package pl.edu.amu.dji.jms.lab2.jmsserver; 2 | 3 | import org.apache.activemq.broker.BrokerService; 4 | 5 | public class JMSServer { 6 | public static void main(String[] args) throws Exception { 7 | BrokerService brokerService = new BrokerService(); 8 | brokerService.addConnector("tcp://localhost:61616"); 9 | brokerService.start(); 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /7-JMS-summary-and-project-kickoff/students/supermarket/pointofsales/pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4 | supermarket 5 | pl.edu.amu.dji.jms.lab4 6 | 1.0-SNAPSHOT 7 | 8 | 4.0.0 9 | 10 | pl.edu.amu.dji.jms.lab4 11 | pointofsales 12 | jar 13 | 14 | pointofsales 15 | http://maven.apache.org 16 | 17 | 18 | UTF-8 19 | 20 | 21 | 22 | 23 | pl.edu.amu.dji.jms.lab4 24 | common 25 | ${parent.version} 26 | 27 | 28 | 29 | -------------------------------------------------------------------------------- /7-JMS-summary-and-project-kickoff/students/supermarket/pointofsales/src/main/java/pl/edu/amu/dji/jms/lab4/PointOfSalesApp.java: -------------------------------------------------------------------------------- 1 | package pl.edu.amu.dji.jms.lab4; 2 | 3 | public class PointOfSalesApp { 4 | public static void main(String[] args) { 5 | System.out.println("Hello World!"); 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /7-JMS-summary-and-project-kickoff/students/supermarket/pointofsales/src/main/resources/keep: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bsodzik/distributed-java-intro/6ae83484ab6acd0336d2c64458bb78949d8d98a0/7-JMS-summary-and-project-kickoff/students/supermarket/pointofsales/src/main/resources/keep -------------------------------------------------------------------------------- /7-JMS-summary-and-project-kickoff/students/supermarket/prototype/pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4 | supermarket 5 | pl.edu.amu.dji.jms.lab4 6 | 1.0-SNAPSHOT 7 | 8 | 4.0.0 9 | 10 | pl.edu.amu.dji.jms.lab4.prototype 11 | prototype 12 | jar 13 | 14 | prototype 15 | http://maven.apache.org 16 | 17 | 18 | UTF-8 19 | 20 | 21 | 22 | 23 | uk.com.robust-it 24 | cloning 25 | 1.9.1 26 | 27 | 28 | 29 | -------------------------------------------------------------------------------- /7-JMS-summary-and-project-kickoff/students/supermarket/prototype/src/main/java/pl/edu/amu/dji/jms/lab4/prototype/PointOfSale.java: -------------------------------------------------------------------------------- 1 | package pl.edu.amu.dji.jms.lab4.prototype; 2 | 3 | import com.google.common.base.Preconditions; 4 | 5 | import java.util.HashMap; 6 | import java.util.Map; 7 | 8 | public class PointOfSale { 9 | 10 | private Map products = new HashMap(); 11 | 12 | private Reporting reporting; 13 | 14 | public PointOfSale(Reporting reporting) { 15 | this.reporting = reporting; 16 | } 17 | 18 | public void initProducts(Map products) { 19 | this.products = products; 20 | } 21 | 22 | public void updatePrice(String name, Double price) { 23 | Preconditions.checkState(!products.isEmpty()); 24 | 25 | products.put(name.toUpperCase(), price); 26 | } 27 | 28 | public void sale(String name) { 29 | Preconditions.checkState(!products.isEmpty()); 30 | 31 | Double price = products.get(name.toUpperCase()); 32 | reporting.updateReport(name, price); 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /7-JMS-summary-and-project-kickoff/students/supermarket/prototype/src/main/java/pl/edu/amu/dji/jms/lab4/prototype/Reporting.java: -------------------------------------------------------------------------------- 1 | package pl.edu.amu.dji.jms.lab4.prototype; 2 | 3 | import java.util.HashMap; 4 | import java.util.Map; 5 | 6 | public class Reporting { 7 | public static final Map PRODUCTS = new HashMap(); 8 | 9 | public void updateReport(String name, Double price) { 10 | Double current = PRODUCTS.get(name); 11 | 12 | current = current != null ? current + price : price; 13 | 14 | PRODUCTS.put(name, current); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /7-JMS-summary-and-project-kickoff/students/supermarket/warehouse/pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4 | supermarket 5 | pl.edu.amu.dji.jms.lab4 6 | 1.0-SNAPSHOT 7 | 8 | 4.0.0 9 | 10 | pl.edu.amu.dji.jms.lab4 11 | warehouse 12 | jar 13 | 14 | warehouse 15 | http://maven.apache.org 16 | 17 | 18 | UTF-8 19 | 20 | 21 | 22 | 23 | pl.edu.amu.dji.jms.lab4 24 | common 25 | ${parent.version} 26 | 27 | 28 | 29 | -------------------------------------------------------------------------------- /7-JMS-summary-and-project-kickoff/students/supermarket/warehouse/src/main/java/pl/edu/amu/dji/jms/lab4/WarehouseApp.java: -------------------------------------------------------------------------------- 1 | package pl.edu.amu.dji.jms.lab4; 2 | 3 | public class WarehouseApp { 4 | public static void main(String[] args) { 5 | System.out.println("Hello World!"); 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /7-JMS-summary-and-project-kickoff/students/supermarket/warehouse/src/main/resources/keep: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bsodzik/distributed-java-intro/6ae83484ab6acd0336d2c64458bb78949d8d98a0/7-JMS-summary-and-project-kickoff/students/supermarket/warehouse/src/main/resources/keep -------------------------------------------------------------------------------- /9-rest-consumption/solution/src/main/java/com/uam/Application.java: -------------------------------------------------------------------------------- 1 | package com.uam; 2 | 3 | import java.util.Collection; 4 | 5 | import org.springframework.context.annotation.Bean; 6 | import org.springframework.context.annotation.ComponentScan; 7 | import org.springframework.http.converter.xml.Jaxb2CollectionHttpMessageConverter; 8 | import org.springframework.web.client.RestTemplate; 9 | 10 | @ComponentScan 11 | public class Application { 12 | 13 | @Bean 14 | public RestTemplate restTemplate() { 15 | RestTemplate restTemplate = new RestTemplate(); 16 | restTemplate.getMessageConverters().add(new Jaxb2CollectionHttpMessageConverter()); 17 | return restTemplate; 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /9-rest-consumption/solution/src/main/resources/banner.txt: -------------------------------------------------------------------------------- 1 | __ _____ __ ___ ____ _ __ _ __ __ __ __ 2 | / / / / | / |/ / / __ \(_)____/ /______(_) /_ __ __/ /____ ____/ / / /___ __ ______ _ 3 | / / / / /| | / /|_/ / / / / / / ___/ __/ ___/ / __ \/ / / / __/ _ \/ __ / __ / / __ `/ | / / __ `/ 4 | / /_/ / ___ |/ / / / / /_/ / (__ ) /_/ / / / /_/ / /_/ / /_/ __/ /_/ / / /_/ / /_/ /| |/ / /_/ / 5 | \____/_/ |_/_/ /_/ /_____/_/____/\__/_/ /_/_.___/\__,_/\__/\___/\__,_/ \____/\__,_/ |___/\__,_/ 6 | -------------------------------------------------------------------------------- /9-rest-consumption/solution/src/main/resources/logback.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /9-rest-consumption/solution/src/test/java/com/uam/exercise1/BookStoreFirstIntegrationTest.java: -------------------------------------------------------------------------------- 1 | package com.uam.exercise1; 2 | 3 | import static org.assertj.core.api.Assertions.assertThat; 4 | 5 | import java.util.List; 6 | 7 | import org.junit.Test; 8 | import org.junit.runner.RunWith; 9 | import org.springframework.beans.factory.annotation.Autowired; 10 | import org.springframework.boot.test.SpringApplicationConfiguration; 11 | import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; 12 | import org.springframework.web.client.RestTemplate; 13 | 14 | import com.uam.Application; 15 | import com.uam.model.Book; 16 | 17 | @RunWith(SpringJUnit4ClassRunner.class) 18 | @SpringApplicationConfiguration(classes = Application.class) 19 | public class BookStoreFirstIntegrationTest { 20 | 21 | @Autowired 22 | private RestTemplate restTemplate; 23 | 24 | @Test 25 | public void bookStoreShouldNotBeEmpty() { 26 | List result = restTemplate.getForObject(Book.URL, List.class); 27 | 28 | assertThat(result).isNotEmpty(); 29 | } 30 | 31 | @Test 32 | public void bookStoreShouldNotBeEmptyAndElementsShouldBeConverted() { 33 | // Conversion does not work for generic collections - arrays have to be used 34 | Book[] result = restTemplate.getForObject(Book.URL, Book[].class); 35 | 36 | assertThat(result).isNotEmpty(); 37 | assertThat(result).hasOnlyElementsOfType(Book.class); 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /9-rest-consumption/students/src/main/java/com/uam/Application.java: -------------------------------------------------------------------------------- 1 | package com.uam; 2 | 3 | import java.util.Collection; 4 | 5 | import org.springframework.context.annotation.Bean; 6 | import org.springframework.context.annotation.ComponentScan; 7 | import org.springframework.http.converter.xml.Jaxb2CollectionHttpMessageConverter; 8 | import org.springframework.web.client.RestTemplate; 9 | 10 | @ComponentScan 11 | public class Application { 12 | 13 | @Bean 14 | public RestTemplate restTemplate() { 15 | RestTemplate restTemplate = new RestTemplate(); 16 | restTemplate.getMessageConverters().add(new Jaxb2CollectionHttpMessageConverter()); 17 | return restTemplate; 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /9-rest-consumption/students/src/main/resources/banner.txt: -------------------------------------------------------------------------------- 1 | __ _____ __ ___ ____ _ __ _ __ __ __ __ 2 | / / / / | / |/ / / __ \(_)____/ /______(_) /_ __ __/ /____ ____/ / / /___ __ ______ _ 3 | / / / / /| | / /|_/ / / / / / / ___/ __/ ___/ / __ \/ / / / __/ _ \/ __ / __ / / __ `/ | / / __ `/ 4 | / /_/ / ___ |/ / / / / /_/ / (__ ) /_/ / / / /_/ / /_/ / /_/ __/ /_/ / / /_/ / /_/ /| |/ / /_/ / 5 | \____/_/ |_/_/ /_/ /_____/_/____/\__/_/ /_/_.___/\__,_/\__/\___/\__,_/ \____/\__,_/ |___/\__,_/ 6 | -------------------------------------------------------------------------------- /9-rest-consumption/students/src/main/resources/logback.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /9-rest-consumption/students/src/test/java/com/uam/exercise1/BookStoreFirstIntegrationTest.java: -------------------------------------------------------------------------------- 1 | package com.uam.exercise1; 2 | 3 | import org.junit.Test; 4 | import org.junit.runner.RunWith; 5 | import org.springframework.beans.factory.annotation.Autowired; 6 | import org.springframework.boot.test.SpringApplicationConfiguration; 7 | import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; 8 | import org.springframework.web.client.RestTemplate; 9 | 10 | import com.uam.Application; 11 | 12 | @RunWith(SpringJUnit4ClassRunner.class) 13 | @SpringApplicationConfiguration(classes = Application.class) 14 | public class BookStoreFirstIntegrationTest { 15 | 16 | @Autowired 17 | private RestTemplate restTemplate; 18 | 19 | @Test 20 | public void bookStoreShouldNotBeEmpty() { 21 | 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /9-rest-consumption/students/src/test/java/com/uam/exercise1/BookStoreSecondIntegrationTest.java: -------------------------------------------------------------------------------- 1 | package com.uam.exercise1; 2 | 3 | import java.util.concurrent.ThreadLocalRandom; 4 | 5 | import org.junit.Before; 6 | import org.junit.Test; 7 | import org.junit.runner.RunWith; 8 | import org.springframework.beans.factory.annotation.Autowired; 9 | import org.springframework.boot.test.SpringApplicationConfiguration; 10 | import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; 11 | import org.springframework.web.client.RestTemplate; 12 | 13 | import com.uam.Application; 14 | import com.uam.model.Book; 15 | 16 | @RunWith(SpringJUnit4ClassRunner.class) 17 | @SpringApplicationConfiguration(classes = Application.class) 18 | public class BookStoreSecondIntegrationTest { 19 | 20 | @Autowired 21 | private RestTemplate restTemplate; 22 | 23 | private String uniqueIsbn; 24 | private Book testBook; 25 | 26 | @Before 27 | public void setUp() { 28 | uniqueIsbn = Long.toString(ThreadLocalRandom.current().nextLong(Long.MAX_VALUE / 100, Long.MAX_VALUE)); 29 | testBook = new Book(uniqueIsbn, "testTitle", "testDescription", "testAuthor"); 30 | } 31 | 32 | @Test 33 | public void bookShouldBeCreatedAndDeleted() { 34 | 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /9-rest-consumption/students/src/test/java/com/uam/exercise1/BookStoreThirdIntegrationTest.java: -------------------------------------------------------------------------------- 1 | package com.uam.exercise1; 2 | 3 | import java.util.concurrent.ThreadLocalRandom; 4 | 5 | import org.junit.Before; 6 | import org.junit.Test; 7 | import org.junit.runner.RunWith; 8 | import org.springframework.beans.factory.annotation.Autowired; 9 | import org.springframework.boot.test.SpringApplicationConfiguration; 10 | import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; 11 | import org.springframework.web.client.RestTemplate; 12 | 13 | import com.uam.Application; 14 | import com.uam.model.Book; 15 | 16 | @RunWith(SpringJUnit4ClassRunner.class) 17 | @SpringApplicationConfiguration(classes = Application.class) 18 | public class BookStoreThirdIntegrationTest { 19 | 20 | @Autowired 21 | private RestTemplate restTemplate; 22 | 23 | private String uniqueIsbn; 24 | private Book testBook; 25 | 26 | @Before 27 | public void setUp() { 28 | uniqueIsbn = Long.toString(ThreadLocalRandom.current().nextLong(Long.MAX_VALUE / 100, Long.MAX_VALUE)); 29 | testBook = new Book(uniqueIsbn, "testTitle", "testDescription", "testAuthor"); 30 | } 31 | 32 | @Test 33 | public void bookShouldNotBeAddedTwice() { 34 | 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /9-rest-consumption/students/src/test/java/com/uam/exercise2/BookStoreExchangeIntegrationTest.java: -------------------------------------------------------------------------------- 1 | package com.uam.exercise2; 2 | 3 | import org.junit.Test; 4 | import org.junit.runner.RunWith; 5 | import org.springframework.beans.factory.annotation.Autowired; 6 | import org.springframework.boot.test.SpringApplicationConfiguration; 7 | import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; 8 | import org.springframework.web.client.RestTemplate; 9 | 10 | import com.uam.Application; 11 | 12 | @RunWith(SpringJUnit4ClassRunner.class) 13 | @SpringApplicationConfiguration(classes = Application.class) 14 | public class BookStoreExchangeIntegrationTest { 15 | 16 | @Autowired 17 | private RestTemplate restTemplate; 18 | 19 | @Test 20 | public void shouldReturnListOfBooksAsJsonString() { 21 | } 22 | 23 | @Test 24 | public void shouldReturnListOfBooksAsJsonObject() { 25 | } 26 | 27 | @Test 28 | public void shouldReturnListOfBooksAsXmlString() { 29 | } 30 | 31 | @Test 32 | public void shouldReturnListOfBooksAsXmlObject() { 33 | } 34 | } 35 | 36 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | distributed-java-intro 2 | ====================== 3 | 4 | Introduction to distributed programming in Java 5 | 6 | - Labs 0 - [introduction](0-introduction/README.md) 7 | - Labs 1 - [threads - basics](1-threads-basics/README.md) 8 | - Labs 2 - [threads - synchronization](2-threads-synchronization/README.md) 9 | - Labs 3 - [threads - higher abstractions](3-threads-higher-abstractions/README.md) 10 | - Labs 4 - [JMS - basics ](4-JMS-basics/README.md) 11 | - Labs 5 - [JMS - spring ](5-spring-jms/README.md) 12 | - Labs 6 - [Transactions and Spring JMS support](6-tx-spring-jms/README.md) 13 | - Labs 7 - [JMS - summary and project kickoff](7-JMS-summary-and-project-kickoff/README.md) 14 | - Labs 8 - [rest - basics](8-rest-basics/README.md) 15 | - Labs 9 - [rest - consumption](9-rest-consumption/README.md) 16 | - Labs 10 - [rest - web service](https://github.com/bsodzik/distributed-java-intro/blob/master/10-rest-web-service/README.md) 17 | - Labs 11 - [rest - web service advanced](https://github.com/bsodzik/distributed-java-intro/blob/master/11-rest-web-service-advanced/README.md) 18 | - Labs 12 - [Akka - basics](https://github.com/bsodzik/distributed-java-intro/blob/master/12-akka-basics/README.md) 19 | - Labs 13 - [Akka - communication](13-akka-communication/README.md) 20 | - Labs 14 - Students project status review, consultations 21 | - Labs 15 - Students project deadline, course credit 22 | - Project 1 - [threads - "Charity Flea Market"](projects/1-threads/README.md) 23 | - Project 2 - [JMS - "Supermarket"](7-JMS-summary-and-project-kickoff/README.md) 24 | 25 | -------------------------------------------------------------------------------- /projects/1-threads/solution/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 4.0.0 6 | 7 | com.bsodzik 8 | charity-flea-market 9 | 1.0-SNAPSHOT 10 | 11 | 12 | src 13 | 14 | 15 | org.codehaus.mojo 16 | exec-maven-plugin 17 | 1.3.2 18 | 19 | 20 | 21 | java 22 | 23 | test 24 | 25 | 26 | 27 | com.bsodzik.Main 28 | 29 | 30 | 31 | 32 | -------------------------------------------------------------------------------- /projects/1-threads/solution/src/com/bsodzik/Donor.java: -------------------------------------------------------------------------------- 1 | package com.bsodzik; 2 | 3 | import java.io.Closeable; 4 | import java.util.concurrent.ThreadLocalRandom; 5 | 6 | public class Donor implements Runnable, Closeable { 7 | 8 | private final String name; 9 | private final Chairman chairman; 10 | private volatile boolean isMarketOpen; 11 | 12 | public Donor(String name, Chairman chairman) { 13 | this.name = name; 14 | this.chairman = chairman; 15 | this.isMarketOpen = true; 16 | } 17 | 18 | @Override 19 | public void run() { 20 | while (isMarketOpen) { 21 | try { 22 | Thread.sleep(ThreadLocalRandom.current().nextInt(5000, 30000)); 23 | Item newItem = new Item(); 24 | 25 | while (isMarketOpen && !chairman.registerNewItem(newItem)) { 26 | Thread.sleep(ThreadLocalRandom.current().nextInt(5000)); 27 | } 28 | } catch (InterruptedException e) { 29 | e.printStackTrace(); 30 | } 31 | } 32 | System.out.println(name + " says good bye"); 33 | } 34 | 35 | @Override 36 | public void close() { 37 | isMarketOpen = false; 38 | } 39 | 40 | @Override 41 | public String toString() { 42 | return name; 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /projects/1-threads/solution/src/com/bsodzik/Item.java: -------------------------------------------------------------------------------- 1 | package com.bsodzik; 2 | 3 | import java.util.concurrent.atomic.AtomicInteger; 4 | 5 | public class Item { 6 | 7 | private static final AtomicInteger counter = new AtomicInteger(0); 8 | 9 | private final int id; 10 | 11 | public Item() { 12 | id = counter.incrementAndGet(); 13 | } 14 | 15 | @Override 16 | public String toString() { 17 | return "Item " + id; 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /projects/1-threads/solution/src/com/bsodzik/Main.java: -------------------------------------------------------------------------------- 1 | package com.bsodzik; 2 | 3 | import java.util.concurrent.ExecutorService; 4 | import java.util.concurrent.Executors; 5 | 6 | public class Main { 7 | 8 | public static void main(String[] args) { 9 | MarketManager marketManager = new MarketManager(); 10 | Chairman chairman = new Chairman(marketManager); 11 | 12 | for (int i = 0; i < 10; ++i) { 13 | marketManager.registerDonor(new Donor("Donor " + i, chairman)); 14 | } 15 | for (int i = 0; i < 50; ++i) { 16 | marketManager.registerRecipient(new Recipient("Recipient " + i, chairman)); 17 | } 18 | 19 | marketManager.openMarket(); 20 | ExecutorService e = Executors.newSingleThreadExecutor(); 21 | e.execute(chairman); 22 | e.shutdown(); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /projects/1-threads/solution/src/com/bsodzik/MarketManager.java: -------------------------------------------------------------------------------- 1 | package com.bsodzik; 2 | 3 | import java.io.Closeable; 4 | import java.io.IOException; 5 | import java.util.LinkedList; 6 | import java.util.List; 7 | import java.util.concurrent.ExecutorService; 8 | import java.util.concurrent.Executors; 9 | 10 | public class MarketManager { 11 | 12 | private final List donors = new LinkedList(); 13 | private final List recipients = new LinkedList(); 14 | 15 | public void registerDonor(Donor donor) { 16 | donors.add(donor); 17 | } 18 | 19 | public void registerRecipient(Recipient recipient) { 20 | recipients.add(recipient); 21 | } 22 | 23 | public void openMarket() { 24 | letUsersIn(donors); 25 | letUsersIn(recipients); 26 | } 27 | 28 | public void closeMarket() { 29 | letUsersOut(recipients); 30 | letUsersOut(donors); 31 | } 32 | 33 | private void letUsersIn(List users) { 34 | ExecutorService executorService = Executors.newCachedThreadPool(); 35 | for (Runnable user : users) { 36 | executorService.execute(user); 37 | } 38 | executorService.shutdown(); 39 | } 40 | 41 | private void letUsersOut(List users) { 42 | for (Closeable user : users) { 43 | synchronized (user) { 44 | try { 45 | user.close(); 46 | } catch (IOException e) { 47 | } finally { 48 | user.notify(); 49 | } 50 | } 51 | } 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /projects/1-threads/students/.gitignore: -------------------------------------------------------------------------------- 1 | target 2 | .idea 3 | *.iml 4 | --------------------------------------------------------------------------------