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.
This commit is contained in:
committed by
KevinGilmore
parent
9220584528
commit
673faede4c
@@ -8,58 +8,16 @@
|
||||
<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>
|
||||
|
||||
<description>Shows how to expose a service using Spring Remoting HTTP.</description>
|
||||
<dependencies>
|
||||
|
||||
<!-- spring -->
|
||||
|
||||
<!-- spring-webmvc is mainly needed to have access to the Spring's DispatcherServler -->
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-webmvc</artifactId>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</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>
|
||||
@@ -1,10 +0,0 @@
|
||||
package com.baeldung.server;
|
||||
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
@Configuration
|
||||
public class CabBookingConfig {
|
||||
|
||||
|
||||
|
||||
}
|
||||
@@ -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("/*");
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,16 +0,0 @@
|
||||
<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">
|
||||
<encoder>
|
||||
<pattern>${logPattern}</pattern>
|
||||
</encoder>
|
||||
</appender>
|
||||
|
||||
<root level="DEBUG">
|
||||
<appender-ref ref="STDOUT"/>
|
||||
</root>
|
||||
</configuration>
|
||||
Reference in New Issue
Block a user