diff --git a/spring-remoting/pom.xml b/spring-remoting/pom.xml new file mode 100644 index 0000000000..cfb4af9d85 --- /dev/null +++ b/spring-remoting/pom.xml @@ -0,0 +1,109 @@ + + + 4.0.0 + com.baeldung + spring-remoting + 1.0-SNAPSHOT + pom + + + 3.6.0 + 3.0.0 + + 3.1.0 + 1.1.7 + 4.2.4.RELEASE + + + + + + + + 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} + api + ${project.version} + + + + + + + + + + + + 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 + + + \ No newline at end of file diff --git a/spring-remoting/remoting-http/api/pom.xml b/spring-remoting/remoting-http/api/pom.xml new file mode 100644 index 0000000000..633217f7de --- /dev/null +++ b/spring-remoting/remoting-http/api/pom.xml @@ -0,0 +1,14 @@ + + + + spring-remoting-http + com.baeldung + 1.0-SNAPSHOT + + 4.0.0 + api + spring-remoting-http-api + API definition shared between client and server. + \ No newline at end of file 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 new file mode 100644 index 0000000000..f2382fabd9 --- /dev/null +++ b/spring-remoting/remoting-http/api/src/main/java/com/baeldung/api/Address.java @@ -0,0 +1,26 @@ +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 new file mode 100644 index 0000000000..0f52a7bfbd --- /dev/null +++ b/spring-remoting/remoting-http/api/src/main/java/com/baeldung/api/Booking.java @@ -0,0 +1,53 @@ +package com.baeldung.api; + +import java.io.Serializable; +import java.util.Date; + +public class Booking implements Serializable { + + private int costInCent; + private int etaInSeconds; + private String bookingCode; + private Date pickUptime; + private Address pickUpAddress; + private Address dropOffAddress; + + public Booking(Address pickUpAddress, Date 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 Date 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); + } + + public static void main(String[] args) throws InterruptedException { + System.out.println( + new Booking(new Address("a", "b"), new Date(), new Address("c", "d"), 123_00, 600, "abc") + ); + } +} diff --git a/spring-remoting/remoting-http/api/src/main/java/com/baeldung/api/BookingException.java b/spring-remoting/remoting-http/api/src/main/java/com/baeldung/api/BookingException.java new file mode 100644 index 0000000000..4099db2908 --- /dev/null +++ b/spring-remoting/remoting-http/api/src/main/java/com/baeldung/api/BookingException.java @@ -0,0 +1,7 @@ +package com.baeldung.api; + +public class BookingException extends Exception { + public BookingException(String message) { + super(message); + } +} 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 new file mode 100644 index 0000000000..25b27264e8 --- /dev/null +++ b/spring-remoting/remoting-http/api/src/main/java/com/baeldung/api/CabBookingService.java @@ -0,0 +1,5 @@ +package com.baeldung.api; + +public interface CabBookingService { + Booking bookPickUp(Address pickUpLocation, Address dropOffLocation, int pax) throws BookingException; +} diff --git a/spring-remoting/remoting-http/client/pom.xml b/spring-remoting/remoting-http/client/pom.xml new file mode 100644 index 0000000000..77891c106a --- /dev/null +++ b/spring-remoting/remoting-http/client/pom.xml @@ -0,0 +1,30 @@ + + + 4.0.0 + + + com.baeldung + spring-remoting-http + 1.0-SNAPSHOT + + + spring-remoting-http-client + Shows how to invoke a remote service using Spring Remoting. + + + + + org.springframework + spring-web + + + + + ${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 new file mode 100644 index 0000000000..7cc8e5183c --- /dev/null +++ b/spring-remoting/remoting-http/client/src/main/java/com/baeldug/client/CabBookingClient.java @@ -0,0 +1,25 @@ +package com.baeldug.client; + +import com.baeldung.api.*; + +public class CabBookingClient { + + private CabBookingService cabService; + + public CabBookingClient(CabBookingService cabService) { + this.cabService = cabService; + } + + public void run() { + + Address pickUp = new Address("13 Seagate Blvd, Key Largo, FL 33037", "US"); + Address dropDown = new Address("91831 Overseas Hwy, Tavernier, FL 33070", "US"); + try { + System.out.println( cabService.bookPickUp(pickUp, dropDown, 3) ); + } catch (BookingException e) { + e.printStackTrace(); + } + + } + +} 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 new file mode 100644 index 0000000000..0ddb37c508 --- /dev/null +++ b/spring-remoting/remoting-http/client/src/main/java/com/baeldug/client/Main.java @@ -0,0 +1,34 @@ +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 InterruptedException { + 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 new file mode 100644 index 0000000000..0d08779bd7 --- /dev/null +++ b/spring-remoting/remoting-http/pom.xml @@ -0,0 +1,20 @@ + + + 4.0.0 + + com.baeldung + spring-remoting + 1.0-SNAPSHOT + + spring-remoting-http + Parent for all modules related to HTTP Spring Remoting + pom + + + server + client + api + + \ No newline at end of file diff --git a/spring-remoting/remoting-http/server/pom.xml b/spring-remoting/remoting-http/server/pom.xml new file mode 100644 index 0000000000..32a99716a5 --- /dev/null +++ b/spring-remoting/remoting-http/server/pom.xml @@ -0,0 +1,65 @@ + + + 4.0.0 + + com.baeldung + spring-remoting-http + 1.0-SNAPSHOT + + war + + spring-remoting-http-server + Shows how to expose a service using Spring Remoting + + + 2.2 + + + + + + + + + org.springframework + spring-webmvc + + + + + 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/readme.md b/spring-remoting/remoting-http/server/readme.md new file mode 100644 index 0000000000..4a2abb5d03 --- /dev/null +++ b/spring-remoting/remoting-http/server/readme.md @@ -0,0 +1,12 @@ +Build and launch with the following command. + + mvn clean package tomcat7:run-war + +Exposed service is available at following URL. + + http://localhost:9090/spring-remoting-http-server/account + +## References + + + 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 new file mode 100644 index 0000000000..146d2ecadb --- /dev/null +++ b/spring-remoting/remoting-http/server/src/main/java/com/baeldung/server/CabBookingConfig.java @@ -0,0 +1,10 @@ +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 new file mode 100644 index 0000000000..53b3fd5faf --- /dev/null +++ b/spring-remoting/remoting-http/server/src/main/java/com/baeldung/server/CabBookingInitializer.java @@ -0,0 +1,34 @@ +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 new file mode 100644 index 0000000000..5f43c7e707 --- /dev/null +++ b/spring-remoting/remoting-http/server/src/main/java/com/baeldung/server/CabBookingServiceImpl.java @@ -0,0 +1,26 @@ +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.util.Date; + +import static java.lang.Math.random; +import static java.lang.System.currentTimeMillis; +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; + Date pickUpDate = new Date((long) (currentTimeMillis() + (1000 * 60 * random() * 15))); + return new Booking(pickUpLocation, pickUpDate, dropOffLocation, costInCent, tripTimeInMinutes * 60, + 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/DispatcherConfig.java new file mode 100644 index 0000000000..8f9391f8ac --- /dev/null +++ b/spring-remoting/remoting-http/server/src/main/java/com/baeldung/server/DispatcherConfig.java @@ -0,0 +1,18 @@ +package com.baeldung.server; + +import com.baeldung.api.CabBookingService; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.remoting.httpinvoker.HttpInvokerServiceExporter; + +@Configuration +public class DispatcherConfig { + + @Bean(name = "/booking") HttpInvokerServiceExporter accountService() { + HttpInvokerServiceExporter exporter = new HttpInvokerServiceExporter(); + exporter.setService( new CabBookingServiceImpl() ); + exporter.setServiceInterface( CabBookingService.class ); + return exporter; + } + +} diff --git a/spring-remoting/remoting-http/server/src/main/resources/logback.xml b/spring-remoting/remoting-http/server/src/main/resources/logback.xml new file mode 100644 index 0000000000..7a03574f40 --- /dev/null +++ b/spring-remoting/remoting-http/server/src/main/resources/logback.xml @@ -0,0 +1,27 @@ + + + + + + + + ${logPattern} + + + + + C:\Users\danidemi\tmp\baledung\app.log + false + + ${logPattern} + + + + + + + + \ No newline at end of file