Intro to Spring Remoting with HTTP Invokers (#964)

* 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
This commit is contained in:
Daniele Demichelis 2017-01-10 19:54:07 +01:00 committed by KevinGilmore
parent 6e9b667aef
commit a0de46efb9
17 changed files with 515 additions and 0 deletions

109
spring-remoting/pom.xml Normal file
View File

@ -0,0 +1,109 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
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">
<modelVersion>4.0.0</modelVersion>
<groupId>com.baeldung</groupId>
<artifactId>spring-remoting</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>pom</packaging>
<properties>
<maven-compiler-plugin.version>3.6.0</maven-compiler-plugin.version>
<maven-war-plugin.version>3.0.0</maven-war-plugin.version>
<servlet.version>3.1.0</servlet.version>
<logback.version>1.1.7</logback.version>
<spring.version>4.2.4.RELEASE</spring.version>
</properties>
<dependencyManagement>
<dependencies>
<!-- spring -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- logback -->
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-core</artifactId>
<version>${logback.version}</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>${logback.version}</version>
</dependency>
<!-- servlet -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>${servlet.version}</version>
<scope>provided</scope>
</dependency>
<!-- companion modules -->
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>api</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<pluginManagement>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>${maven-compiler-plugin.version}</version>
<configuration>
<debug>true</debug>
<optimize>true</optimize>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
<showDeprecation>true</showDeprecation>
<showWarnings>true</showWarnings>
</configuration>
</plugin>
<!-- This special setup for maven-war-plugin is needed to use Servlet 3 annotation based configuration, where a WEB-INF/web.xml is not strictl needed. -->
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>3.0.0</version>
<configuration>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
<modules>
<module>remoting-http</module>
</modules>
</project>

View File

@ -0,0 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
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">
<parent>
<artifactId>spring-remoting-http</artifactId>
<groupId>com.baeldung</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>api</artifactId>
<name>spring-remoting-http-api</name>
<description>API definition shared between client and server.</description>
</project>

View File

@ -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 + ")";
}
}

View File

@ -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")
);
}
}

View File

@ -0,0 +1,7 @@
package com.baeldung.api;
public class BookingException extends Exception {
public BookingException(String message) {
super(message);
}
}

View File

@ -0,0 +1,5 @@
package com.baeldung.api;
public interface CabBookingService {
Booking bookPickUp(Address pickUpLocation, Address dropOffLocation, int pax) throws BookingException;
}

View File

@ -0,0 +1,30 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
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">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.baeldung</groupId>
<artifactId>spring-remoting-http</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<artifactId>spring-remoting-http-client</artifactId>
<description>Shows how to invoke a remote service using Spring Remoting.</description>
<dependencies>
<!-- spring -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
</dependency>
<!-- companion modules -->
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>api</artifactId>
</dependency>
</dependencies>
</project>

View File

@ -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();
}
}
}

View File

@ -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();
}
}

View File

@ -0,0 +1,20 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
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">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.baeldung</groupId>
<artifactId>spring-remoting</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<artifactId>spring-remoting-http</artifactId>
<description>Parent for all modules related to HTTP Spring Remoting</description>
<packaging>pom</packaging>
<modules>
<module>server</module>
<module>client</module>
<module>api</module>
</modules>
</project>

View File

@ -0,0 +1,65 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
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">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.baeldung</groupId>
<artifactId>spring-remoting-http</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<packaging>war</packaging>
<artifactId>spring-remoting-http-server</artifactId>
<description>Shows how to expose a service using Spring Remoting</description>
<properties>
<tomcat7-maven-plugin.version>2.2</tomcat7-maven-plugin.version>
</properties>
<dependencies>
<!-- spring -->
<!-- spring-webmvc is mainly needed to have access to the Spring's DispatcherServler -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
</dependency>
<!-- servlet -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
</dependency>
<!-- companion modules -->
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>api</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
</plugin>
<plugin>
<artifactId>maven-war-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>${tomcat7-maven-plugin.version}</version>
<configuration>
<port>9090</port>
</configuration>
</plugin>
</plugins>
</build>
</project>

View File

@ -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
<http://docs.spring.io/spring/docs/current/spring-framework-reference/htmlsingle/#remoting-httpinvoker>

View File

@ -0,0 +1,10 @@
package com.baeldung.server;
import org.springframework.context.annotation.Configuration;
@Configuration
public class CabBookingConfig {
}

View File

@ -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("/*");
}
}

View File

@ -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());
}
}

View File

@ -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;
}
}

View File

@ -0,0 +1,27 @@
<configuration scan="true" scanPeriod="1 minute">
<variable
name="logPattern"
value="%-30(%d{MMM dd YYYY HH:mm:ss.SSS} [%thread]) %-5level %logger{5} [%file:%line] - %msg%n"/>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<!-- encoders are assigned the type
ch.qos.logback.classic.encoder.PatternLayoutEncoder by default -->
<encoder>
<pattern>${logPattern}</pattern>
</encoder>
</appender>
<appender name="FILE" class="ch.qos.logback.core.FileAppender">
<file>C:\Users\danidemi\tmp\baledung\app.log</file>
<append>false</append>
<encoder>
<pattern>${logPattern}</pattern>
</encoder>
</appender>
<root level="DEBUG">
<appender-ref ref="STDOUT"/>
<appender-ref ref="FILE"/>
</root>
</configuration>