Skip to content

Commit a0de46e

Browse files
danidemiKevinGilmore
authored andcommitted
Intro to Spring Remoting with HTTP Invokers (eugenp#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
1 parent 6e9b667 commit a0de46e

17 files changed

Lines changed: 515 additions & 0 deletions

File tree

‎spring-remoting/pom.xml‎

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
<groupId>com.baeldung</groupId>
7+
<artifactId>spring-remoting</artifactId>
8+
<version>1.0-SNAPSHOT</version>
9+
<packaging>pom</packaging>
10+
11+
<properties>
12+
<maven-compiler-plugin.version>3.6.0</maven-compiler-plugin.version>
13+
<maven-war-plugin.version>3.0.0</maven-war-plugin.version>
14+
15+
<servlet.version>3.1.0</servlet.version>
16+
<logback.version>1.1.7</logback.version>
17+
<spring.version>4.2.4.RELEASE</spring.version>
18+
</properties>
19+
20+
<dependencyManagement>
21+
<dependencies>
22+
23+
<!-- spring -->
24+
<dependency>
25+
<groupId>org.springframework</groupId>
26+
<artifactId>spring-core</artifactId>
27+
<version>${spring.version}</version>
28+
</dependency>
29+
<dependency>
30+
<groupId>org.springframework</groupId>
31+
<artifactId>spring-context</artifactId>
32+
<version>${spring.version}</version>
33+
</dependency>
34+
<dependency>
35+
<groupId>org.springframework</groupId>
36+
<artifactId>spring-web</artifactId>
37+
<version>${spring.version}</version>
38+
</dependency>
39+
<dependency>
40+
<groupId>org.springframework</groupId>
41+
<artifactId>spring-webmvc</artifactId>
42+
<version>${spring.version}</version>
43+
</dependency>
44+
45+
<!-- logback -->
46+
<dependency>
47+
<groupId>ch.qos.logback</groupId>
48+
<artifactId>logback-core</artifactId>
49+
<version>${logback.version}</version>
50+
</dependency>
51+
<dependency>
52+
<groupId>ch.qos.logback</groupId>
53+
<artifactId>logback-classic</artifactId>
54+
<version>${logback.version}</version>
55+
</dependency>
56+
57+
<!-- servlet -->
58+
<dependency>
59+
<groupId>javax.servlet</groupId>
60+
<artifactId>javax.servlet-api</artifactId>
61+
<version>${servlet.version}</version>
62+
<scope>provided</scope>
63+
</dependency>
64+
65+
<!-- companion modules -->
66+
<dependency>
67+
<groupId>${project.groupId}</groupId>
68+
<artifactId>api</artifactId>
69+
<version>${project.version}</version>
70+
</dependency>
71+
72+
</dependencies>
73+
74+
</dependencyManagement>
75+
76+
77+
<build>
78+
<pluginManagement>
79+
<plugins>
80+
<plugin>
81+
<artifactId>maven-compiler-plugin</artifactId>
82+
<version>${maven-compiler-plugin.version}</version>
83+
<configuration>
84+
<debug>true</debug>
85+
<optimize>true</optimize>
86+
<source>1.8</source>
87+
<target>1.8</target>
88+
<encoding>UTF-8</encoding>
89+
<showDeprecation>true</showDeprecation>
90+
<showWarnings>true</showWarnings>
91+
</configuration>
92+
</plugin>
93+
<!-- 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. -->
94+
<plugin>
95+
<artifactId>maven-war-plugin</artifactId>
96+
<version>3.0.0</version>
97+
<configuration>
98+
<failOnMissingWebXml>false</failOnMissingWebXml>
99+
</configuration>
100+
</plugin>
101+
</plugins>
102+
</pluginManagement>
103+
</build>
104+
105+
<modules>
106+
<module>remoting-http</module>
107+
</modules>
108+
109+
</project>
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<parent>
6+
<artifactId>spring-remoting-http</artifactId>
7+
<groupId>com.baeldung</groupId>
8+
<version>1.0-SNAPSHOT</version>
9+
</parent>
10+
<modelVersion>4.0.0</modelVersion>
11+
<artifactId>api</artifactId>
12+
<name>spring-remoting-http-api</name>
13+
<description>API definition shared between client and server.</description>
14+
</project>
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.baeldung.api;
2+
3+
import java.io.Serializable;
4+
5+
public class Address implements Serializable{
6+
7+
private String address;
8+
private String countryCode;
9+
10+
public Address(String address, String countryCode) {
11+
this.address = address;
12+
this.countryCode = countryCode;
13+
}
14+
15+
public String getAddress() {
16+
return address;
17+
}
18+
19+
public String getCountryCode() {
20+
return countryCode;
21+
}
22+
23+
@Override public String toString() {
24+
return address + " (" + countryCode + ")";
25+
}
26+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package com.baeldung.api;
2+
3+
import java.io.Serializable;
4+
import java.util.Date;
5+
6+
public class Booking implements Serializable {
7+
8+
private int costInCent;
9+
private int etaInSeconds;
10+
private String bookingCode;
11+
private Date pickUptime;
12+
private Address pickUpAddress;
13+
private Address dropOffAddress;
14+
15+
public Booking(Address pickUpAddress, Date pickUptime, Address dropOffAddress, int costInCent, int etaInSeconds, String bookingCode) {
16+
this.costInCent = costInCent;
17+
this.etaInSeconds = etaInSeconds;
18+
this.bookingCode = bookingCode;
19+
this.pickUptime = pickUptime;
20+
this.pickUpAddress = pickUpAddress;
21+
this.dropOffAddress = dropOffAddress;
22+
}
23+
24+
public int getCostInCent() {
25+
return costInCent;
26+
}
27+
28+
public int getEtaInSeconds() {
29+
return etaInSeconds;
30+
}
31+
32+
public String getBookingCode() {
33+
return bookingCode;
34+
}
35+
36+
public Date getPickUptime() {
37+
return pickUptime;
38+
}
39+
40+
public Address getDropOffAddress() {
41+
return dropOffAddress;
42+
}
43+
44+
@Override public String toString() {
45+
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);
46+
}
47+
48+
public static void main(String[] args) throws InterruptedException {
49+
System.out.println(
50+
new Booking(new Address("a", "b"), new Date(), new Address("c", "d"), 123_00, 600, "abc")
51+
);
52+
}
53+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.baeldung.api;
2+
3+
public class BookingException extends Exception {
4+
public BookingException(String message) {
5+
super(message);
6+
}
7+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package com.baeldung.api;
2+
3+
public interface CabBookingService {
4+
Booking bookPickUp(Address pickUpLocation, Address dropOffLocation, int pax) throws BookingException;
5+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<parent>
8+
<groupId>com.baeldung</groupId>
9+
<artifactId>spring-remoting-http</artifactId>
10+
<version>1.0-SNAPSHOT</version>
11+
</parent>
12+
13+
<artifactId>spring-remoting-http-client</artifactId>
14+
<description>Shows how to invoke a remote service using Spring Remoting.</description>
15+
16+
<dependencies>
17+
<!-- spring -->
18+
<dependency>
19+
<groupId>org.springframework</groupId>
20+
<artifactId>spring-web</artifactId>
21+
</dependency>
22+
23+
<!-- companion modules -->
24+
<dependency>
25+
<groupId>${project.groupId}</groupId>
26+
<artifactId>api</artifactId>
27+
</dependency>
28+
</dependencies>
29+
30+
</project>
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.baeldug.client;
2+
3+
import com.baeldung.api.*;
4+
5+
public class CabBookingClient {
6+
7+
private CabBookingService cabService;
8+
9+
public CabBookingClient(CabBookingService cabService) {
10+
this.cabService = cabService;
11+
}
12+
13+
public void run() {
14+
15+
Address pickUp = new Address("13 Seagate Blvd, Key Largo, FL 33037", "US");
16+
Address dropDown = new Address("91831 Overseas Hwy, Tavernier, FL 33070", "US");
17+
try {
18+
System.out.println( cabService.bookPickUp(pickUp, dropDown, 3) );
19+
} catch (BookingException e) {
20+
e.printStackTrace();
21+
}
22+
23+
}
24+
25+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.baeldug.client;
2+
3+
import com.baeldung.api.CabBookingService;
4+
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
5+
import org.springframework.context.annotation.Bean;
6+
import org.springframework.context.annotation.Configuration;
7+
import org.springframework.remoting.httpinvoker.HttpInvokerProxyFactoryBean;
8+
9+
@Configuration
10+
public class Main {
11+
12+
@Bean
13+
public HttpInvokerProxyFactoryBean invoker() {
14+
HttpInvokerProxyFactoryBean invoker = new HttpInvokerProxyFactoryBean();
15+
invoker.setServiceUrl("http://localhost:9090/spring-remoting-http-server/booking");
16+
invoker.setServiceInterface(CabBookingService.class);
17+
return invoker;
18+
}
19+
20+
@Bean
21+
public CabBookingClient client(CabBookingService service){
22+
return new CabBookingClient(service);
23+
}
24+
25+
public static void main(String[] args) throws InterruptedException {
26+
AnnotationConfigApplicationContext rootContext =
27+
new AnnotationConfigApplicationContext();
28+
rootContext.scan(Main.class.getPackage().getName());
29+
rootContext.refresh();
30+
CabBookingClient bean = rootContext.getBean(CabBookingClient.class);
31+
bean.run();
32+
}
33+
34+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
<parent>
7+
<groupId>com.baeldung</groupId>
8+
<artifactId>spring-remoting</artifactId>
9+
<version>1.0-SNAPSHOT</version>
10+
</parent>
11+
<artifactId>spring-remoting-http</artifactId>
12+
<description>Parent for all modules related to HTTP Spring Remoting</description>
13+
<packaging>pom</packaging>
14+
15+
<modules>
16+
<module>server</module>
17+
<module>client</module>
18+
<module>api</module>
19+
</modules>
20+
</project>

0 commit comments

Comments
 (0)