From 673faede4cf84691b68ba5dff9f1edddeee5c854 Mon Sep 17 00:00:00 2001 From: Daniele Demichelis Date: Tue, 24 Jan 2017 05:00:58 +0100 Subject: [PATCH] baeldung-articlesBAEL-553 Intro to Spring Remoting with HTTP Invokers (#1034) * First test with log4j rolling appenders * small fix * Log4j rolling appender * First set up with rolling file on log4j 2 * Added logback code. * log4j2 more detailed example * log4j2 more detailed example * Improved names and examples * Fixed configurations * improved configs * formatted * Final fix * fixed formatting * Formatting fix * Fix sample apps to avoid try / catch * Fix request to replace files * Fix end lines * Log4j2 logger is shot down at the end. * Initial commit, the server starts launched from maven * made room for client and server projects * made room for client and server projects * base example works * poms restructured * packages restructured * packages restructured * Some renaming and a proper formatting string * Small renamings * Small renamings * Fixed invoked URL in client through fixing bean name * java.time.* instead of java.util.Date * java.time.* instead of java.util.Date * Removed log on file, kepts log on console * Updated Spring and Logback version to latest release. * Exception removed from run() * Code simplification. - Interfaces and objects simplified. - Spring Boot instead of plain Spring - No need for Tomcat plug in anymore. * Small updates to poms. * Unused classes removed. * Unused classes removed. --- spring-remoting/pom.xml | 84 ++----------------- .../main/java/com/baeldung/api/Address.java | 26 ------ .../main/java/com/baeldung/api/Booking.java | 45 ++-------- .../com/baeldung/api/CabBookingService.java | 2 +- spring-remoting/remoting-http/client/pom.xml | 18 ++-- .../com/baeldug/client/CabBookingClient.java | 21 ----- .../main/java/com/baeldug/client/Client.java | 28 +++++++ .../main/java/com/baeldug/client/Main.java | 34 -------- spring-remoting/remoting-http/pom.xml | 3 +- spring-remoting/remoting-http/server/pom.xml | 48 +---------- .../com/baeldung/server/CabBookingConfig.java | 10 --- .../server/CabBookingInitializer.java | 34 -------- .../server/CabBookingServiceImpl.java | 17 +--- .../{DispatcherConfig.java => Server.java} | 13 ++- .../server/src/main/resources/logback.xml | 16 ---- 15 files changed, 68 insertions(+), 331 deletions(-) delete mode 100644 spring-remoting/remoting-http/api/src/main/java/com/baeldung/api/Address.java delete mode 100644 spring-remoting/remoting-http/client/src/main/java/com/baeldug/client/CabBookingClient.java create mode 100644 spring-remoting/remoting-http/client/src/main/java/com/baeldug/client/Client.java delete mode 100644 spring-remoting/remoting-http/client/src/main/java/com/baeldug/client/Main.java delete mode 100644 spring-remoting/remoting-http/server/src/main/java/com/baeldung/server/CabBookingConfig.java delete mode 100644 spring-remoting/remoting-http/server/src/main/java/com/baeldung/server/CabBookingInitializer.java rename spring-remoting/remoting-http/server/src/main/java/com/baeldung/server/{DispatcherConfig.java => Server.java} (62%) delete mode 100644 spring-remoting/remoting-http/server/src/main/resources/logback.xml diff --git a/spring-remoting/pom.xml b/spring-remoting/pom.xml index 2e3b7e76f8..a43dd52a3e 100644 --- a/spring-remoting/pom.xml +++ b/spring-remoting/pom.xml @@ -3,65 +3,24 @@ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4.0.0 + + org.springframework.boot + spring-boot-starter-parent + 1.4.3.RELEASE + com.baeldung spring-remoting 1.0-SNAPSHOT + Parent for all projects related to Spring Remoting. pom - 3.6.0 - 3.0.0 - - 3.1.0 - 1.1.8 - 4.3.5.RELEASE + 1.8 - - - org.springframework - spring-core - ${spring.version} - - - org.springframework - spring-context - ${spring.version} - - - org.springframework - spring-web - ${spring.version} - - - org.springframework - spring-webmvc - ${spring.version} - - - - - ch.qos.logback - logback-core - ${logback.version} - - - ch.qos.logback - logback-classic - ${logback.version} - - - - - javax.servlet - javax.servlet-api - ${servlet.version} - provided - - ${project.groupId} @@ -73,35 +32,6 @@ - - - - - - maven-compiler-plugin - ${maven-compiler-plugin.version} - - true - true - 1.8 - 1.8 - UTF-8 - true - true - - - - - maven-war-plugin - 3.0.0 - - false - - - - - - remoting-http diff --git a/spring-remoting/remoting-http/api/src/main/java/com/baeldung/api/Address.java b/spring-remoting/remoting-http/api/src/main/java/com/baeldung/api/Address.java deleted file mode 100644 index f2382fabd9..0000000000 --- a/spring-remoting/remoting-http/api/src/main/java/com/baeldung/api/Address.java +++ /dev/null @@ -1,26 +0,0 @@ -package com.baeldung.api; - -import java.io.Serializable; - -public class Address implements Serializable{ - - private String address; - private String countryCode; - - public Address(String address, String countryCode) { - this.address = address; - this.countryCode = countryCode; - } - - public String getAddress() { - return address; - } - - public String getCountryCode() { - return countryCode; - } - - @Override public String toString() { - return address + " (" + countryCode + ")"; - } -} diff --git a/spring-remoting/remoting-http/api/src/main/java/com/baeldung/api/Booking.java b/spring-remoting/remoting-http/api/src/main/java/com/baeldung/api/Booking.java index 7bec4e162d..5d2d3683a1 100644 --- a/spring-remoting/remoting-http/api/src/main/java/com/baeldung/api/Booking.java +++ b/spring-remoting/remoting-http/api/src/main/java/com/baeldung/api/Booking.java @@ -1,52 +1,17 @@ package com.baeldung.api; import java.io.Serializable; -import java.time.LocalDateTime; + +import static java.lang.String.format; public class Booking implements Serializable { - - private int costInCent; - private int etaInSeconds; private String bookingCode; - private LocalDateTime pickUptime; - private Address pickUpAddress; - private Address dropOffAddress; - - public Booking(Address pickUpAddress, LocalDateTime pickUptime, Address dropOffAddress, int costInCent, int etaInSeconds, String bookingCode) { - this.costInCent = costInCent; - this.etaInSeconds = etaInSeconds; - this.bookingCode = bookingCode; - this.pickUptime = pickUptime; - this.pickUpAddress = pickUpAddress; - this.dropOffAddress = dropOffAddress; - } - - public int getCostInCent() { - return costInCent; - } - - public int getEtaInSeconds() { - return etaInSeconds; - } - - public String getBookingCode() { - return bookingCode; - } - - public LocalDateTime getPickUptime() { - return pickUptime; - } - - public Address getDropOffAddress() { - return dropOffAddress; - } @Override public String toString() { - return String.format("Booking: pick up @ %tr in %s, drop down in %s after %d minutes, %.2f $.", pickUptime, pickUpAddress, dropOffAddress, etaInSeconds / 60, costInCent / 100.0); + return format("Ride confirmed: code '%s'.", bookingCode); } - public static void main(String[] args) throws InterruptedException { - System.out.println(new Booking(new Address("a", "b"), - LocalDateTime.now(), new Address("c", "d"), 123_00, 600, "abc")); + public Booking(String bookingCode) { + this.bookingCode = bookingCode; } } diff --git a/spring-remoting/remoting-http/api/src/main/java/com/baeldung/api/CabBookingService.java b/spring-remoting/remoting-http/api/src/main/java/com/baeldung/api/CabBookingService.java index 25b27264e8..b554415e07 100644 --- a/spring-remoting/remoting-http/api/src/main/java/com/baeldung/api/CabBookingService.java +++ b/spring-remoting/remoting-http/api/src/main/java/com/baeldung/api/CabBookingService.java @@ -1,5 +1,5 @@ package com.baeldung.api; public interface CabBookingService { - Booking bookPickUp(Address pickUpLocation, Address dropOffLocation, int pax) throws BookingException; + Booking bookRide(String pickUpLocation) throws BookingException; } diff --git a/spring-remoting/remoting-http/client/pom.xml b/spring-remoting/remoting-http/client/pom.xml index 77891c106a..94a536fc18 100644 --- a/spring-remoting/remoting-http/client/pom.xml +++ b/spring-remoting/remoting-http/client/pom.xml @@ -9,22 +9,22 @@ spring-remoting-http 1.0-SNAPSHOT - spring-remoting-http-client - Shows how to invoke a remote service using Spring Remoting. - + Shows how to invoke a remote service using Spring Remoting HTTP. - - org.springframework - spring-web + org.springframework.boot + spring-boot-starter-web + + + org.springframework.boot + spring-boot-starter-tomcat + + - - ${project.groupId} api - \ No newline at end of file diff --git a/spring-remoting/remoting-http/client/src/main/java/com/baeldug/client/CabBookingClient.java b/spring-remoting/remoting-http/client/src/main/java/com/baeldug/client/CabBookingClient.java deleted file mode 100644 index 9669970403..0000000000 --- a/spring-remoting/remoting-http/client/src/main/java/com/baeldug/client/CabBookingClient.java +++ /dev/null @@ -1,21 +0,0 @@ -package com.baeldug.client; - -import com.baeldung.api.*; - -public class CabBookingClient { - - private CabBookingService cabService; - - public CabBookingClient(CabBookingService cabService) { - this.cabService = cabService; - } - - public void run() throws BookingException { - - Address pickUp = new Address("13 Seagate Blvd, Key Largo, FL 33037", "US"); - Address dropDown = new Address("91831 Overseas Hwy, Tavernier, FL 33070", "US"); - System.out.println( cabService.bookPickUp(pickUp, dropDown, 3) ); - - } - -} diff --git a/spring-remoting/remoting-http/client/src/main/java/com/baeldug/client/Client.java b/spring-remoting/remoting-http/client/src/main/java/com/baeldug/client/Client.java new file mode 100644 index 0000000000..90f6736a43 --- /dev/null +++ b/spring-remoting/remoting-http/client/src/main/java/com/baeldug/client/Client.java @@ -0,0 +1,28 @@ +package com.baeldug.client; + +import com.baeldung.api.BookingException; +import com.baeldung.api.CabBookingService; +import org.springframework.boot.SpringApplication; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.remoting.httpinvoker.HttpInvokerProxyFactoryBean; + +import static java.lang.System.out; + +@Configuration +public class Client { + + @Bean + public HttpInvokerProxyFactoryBean invoker() { + HttpInvokerProxyFactoryBean invoker = new HttpInvokerProxyFactoryBean(); + invoker.setServiceUrl("http://localhost:8080/booking"); + invoker.setServiceInterface(CabBookingService.class); + return invoker; + } + + public static void main(String[] args) throws BookingException { + CabBookingService service = SpringApplication.run(Client.class, args).getBean(CabBookingService.class); + out.println(service.bookRide("13 Seagate Blvd, Key Largo, FL 33037")); + } + +} diff --git a/spring-remoting/remoting-http/client/src/main/java/com/baeldug/client/Main.java b/spring-remoting/remoting-http/client/src/main/java/com/baeldug/client/Main.java deleted file mode 100644 index a5e3ccd912..0000000000 --- a/spring-remoting/remoting-http/client/src/main/java/com/baeldug/client/Main.java +++ /dev/null @@ -1,34 +0,0 @@ -package com.baeldug.client; - -import com.baeldung.api.CabBookingService; -import org.springframework.context.annotation.AnnotationConfigApplicationContext; -import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.Configuration; -import org.springframework.remoting.httpinvoker.HttpInvokerProxyFactoryBean; - -@Configuration -public class Main { - - @Bean - public HttpInvokerProxyFactoryBean invoker() { - HttpInvokerProxyFactoryBean invoker = new HttpInvokerProxyFactoryBean(); - invoker.setServiceUrl("http://localhost:9090/spring-remoting-http-server/booking"); - invoker.setServiceInterface(CabBookingService.class); - return invoker; - } - - @Bean - public CabBookingClient client(CabBookingService service){ - return new CabBookingClient(service); - } - - public static void main(String[] args) throws Exception { - AnnotationConfigApplicationContext rootContext = - new AnnotationConfigApplicationContext(); - rootContext.scan(Main.class.getPackage().getName()); - rootContext.refresh(); - CabBookingClient bean = rootContext.getBean(CabBookingClient.class); - bean.run(); - } - -} diff --git a/spring-remoting/remoting-http/pom.xml b/spring-remoting/remoting-http/pom.xml index 0d08779bd7..117d4ec295 100644 --- a/spring-remoting/remoting-http/pom.xml +++ b/spring-remoting/remoting-http/pom.xml @@ -9,9 +9,8 @@ 1.0-SNAPSHOT spring-remoting-http - Parent for all modules related to HTTP Spring Remoting + Parent for all modules related to HTTP Spring Remoting. pom - server client diff --git a/spring-remoting/remoting-http/server/pom.xml b/spring-remoting/remoting-http/server/pom.xml index 32a99716a5..517f4451f7 100644 --- a/spring-remoting/remoting-http/server/pom.xml +++ b/spring-remoting/remoting-http/server/pom.xml @@ -8,58 +8,16 @@ spring-remoting-http 1.0-SNAPSHOT - war - spring-remoting-http-server - Shows how to expose a service using Spring Remoting - - - 2.2 - - + Shows how to expose a service using Spring Remoting HTTP. - - - - - org.springframework - spring-webmvc + org.springframework.boot + spring-boot-starter-web - - - - javax.servlet - javax.servlet-api - - - ${project.groupId} api - - - - - - maven-compiler-plugin - - - - maven-war-plugin - - - - org.apache.tomcat.maven - tomcat7-maven-plugin - ${tomcat7-maven-plugin.version} - - 9090 - - - - - \ No newline at end of file diff --git a/spring-remoting/remoting-http/server/src/main/java/com/baeldung/server/CabBookingConfig.java b/spring-remoting/remoting-http/server/src/main/java/com/baeldung/server/CabBookingConfig.java deleted file mode 100644 index 146d2ecadb..0000000000 --- a/spring-remoting/remoting-http/server/src/main/java/com/baeldung/server/CabBookingConfig.java +++ /dev/null @@ -1,10 +0,0 @@ -package com.baeldung.server; - -import org.springframework.context.annotation.Configuration; - -@Configuration -public class CabBookingConfig { - - - -} diff --git a/spring-remoting/remoting-http/server/src/main/java/com/baeldung/server/CabBookingInitializer.java b/spring-remoting/remoting-http/server/src/main/java/com/baeldung/server/CabBookingInitializer.java deleted file mode 100644 index 53b3fd5faf..0000000000 --- a/spring-remoting/remoting-http/server/src/main/java/com/baeldung/server/CabBookingInitializer.java +++ /dev/null @@ -1,34 +0,0 @@ -package com.baeldung.server; - -import org.springframework.web.WebApplicationInitializer; -import org.springframework.web.context.ContextLoaderListener; -import org.springframework.web.context.support.AnnotationConfigWebApplicationContext; -import org.springframework.web.servlet.DispatcherServlet; - -import javax.servlet.ServletContext; -import javax.servlet.ServletRegistration; - -public class CabBookingInitializer implements WebApplicationInitializer { - - @Override - public void onStartup(ServletContext container) { - AnnotationConfigWebApplicationContext rootContext = - new AnnotationConfigWebApplicationContext(); - rootContext.register(CabBookingConfig.class); - - // Manage the lifecycle of the root application context - container.addListener(new ContextLoaderListener(rootContext)); - - // Create the dispatcher servlet's Spring application context - AnnotationConfigWebApplicationContext dispatcherContext = - new AnnotationConfigWebApplicationContext(); - dispatcherContext.register(DispatcherConfig.class); - - // Register and map the dispatcher servlet - ServletRegistration.Dynamic dispatcher = - container.addServlet("dispatcher", new DispatcherServlet(dispatcherContext)); - dispatcher.setLoadOnStartup(1); - dispatcher.addMapping("/*"); - } - -} diff --git a/spring-remoting/remoting-http/server/src/main/java/com/baeldung/server/CabBookingServiceImpl.java b/spring-remoting/remoting-http/server/src/main/java/com/baeldung/server/CabBookingServiceImpl.java index 8f3c099fe0..55ec9c5733 100644 --- a/spring-remoting/remoting-http/server/src/main/java/com/baeldung/server/CabBookingServiceImpl.java +++ b/spring-remoting/remoting-http/server/src/main/java/com/baeldung/server/CabBookingServiceImpl.java @@ -1,27 +1,16 @@ package com.baeldung.server; -import com.baeldung.api.Address; import com.baeldung.api.Booking; import com.baeldung.api.BookingException; import com.baeldung.api.CabBookingService; -import java.time.LocalDateTime; - import static java.lang.Math.random; -import static java.time.LocalDateTime.now; -import static java.time.temporal.ChronoUnit.MINUTES; import static java.util.UUID.randomUUID; public class CabBookingServiceImpl implements CabBookingService { - @Override public Booking bookPickUp(Address pickUpLocation, Address dropOffLocation, int pax) throws BookingException { - if (random() < 0.3) { - throw new BookingException("Cab unavailable"); - } - int tripTimeInMinutes = (int) (5 + random() * 15); - int costInCent = 15_00 + tripTimeInMinutes * 5 * pax; - LocalDateTime pickUpDate = now().plus(15L * (long) (random() * 10), MINUTES); - - return new Booking(pickUpLocation, pickUpDate, dropOffLocation, costInCent, tripTimeInMinutes * 60, randomUUID().toString()); + @Override public Booking bookRide(String pickUpLocation) throws BookingException { + if (random() < 0.3) throw new BookingException("Cab unavailable"); + return new Booking(randomUUID().toString()); } } diff --git a/spring-remoting/remoting-http/server/src/main/java/com/baeldung/server/DispatcherConfig.java b/spring-remoting/remoting-http/server/src/main/java/com/baeldung/server/Server.java similarity index 62% rename from spring-remoting/remoting-http/server/src/main/java/com/baeldung/server/DispatcherConfig.java rename to spring-remoting/remoting-http/server/src/main/java/com/baeldung/server/Server.java index 8f9391f8ac..c6b198507f 100644 --- a/spring-remoting/remoting-http/server/src/main/java/com/baeldung/server/DispatcherConfig.java +++ b/spring-remoting/remoting-http/server/src/main/java/com/baeldung/server/Server.java @@ -1,12 +1,17 @@ package com.baeldung.server; import com.baeldung.api.CabBookingService; +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; import org.springframework.remoting.httpinvoker.HttpInvokerServiceExporter; @Configuration -public class DispatcherConfig { +@ComponentScan +@EnableAutoConfiguration +public class Server { @Bean(name = "/booking") HttpInvokerServiceExporter accountService() { HttpInvokerServiceExporter exporter = new HttpInvokerServiceExporter(); @@ -15,4 +20,8 @@ public class DispatcherConfig { return exporter; } -} + public static void main(String[] args) { + SpringApplication.run(Server.class, args); + } + +} \ No newline at end of file diff --git a/spring-remoting/remoting-http/server/src/main/resources/logback.xml b/spring-remoting/remoting-http/server/src/main/resources/logback.xml deleted file mode 100644 index 566c44b448..0000000000 --- a/spring-remoting/remoting-http/server/src/main/resources/logback.xml +++ /dev/null @@ -1,16 +0,0 @@ - - - - - - - ${logPattern} - - - - - - - \ No newline at end of file