Skip to content

Commit 5c99b29

Browse files
danidemiKevinGilmore
authored andcommitted
BAEL-553 - Intro to Spring Remoting with HTTP Invokers (eugenp#998)
* 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()
1 parent 5cc5d7d commit 5c99b29

6 files changed

Lines changed: 19 additions & 34 deletions

File tree

‎spring-remoting/pom.xml‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@
1313
<maven-war-plugin.version>3.0.0</maven-war-plugin.version>
1414

1515
<servlet.version>3.1.0</servlet.version>
16-
<logback.version>1.1.7</logback.version>
17-
<spring.version>4.2.4.RELEASE</spring.version>
16+
<logback.version>1.1.8</logback.version>
17+
<spring.version>4.3.5.RELEASE</spring.version>
1818
</properties>
1919

2020
<dependencyManagement>

‎spring-remoting/remoting-http/api/src/main/java/com/baeldung/api/Booking.java‎

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
package com.baeldung.api;
22

33
import java.io.Serializable;
4-
import java.util.Date;
4+
import java.time.LocalDateTime;
55

66
public class Booking implements Serializable {
77

88
private int costInCent;
99
private int etaInSeconds;
1010
private String bookingCode;
11-
private Date pickUptime;
11+
private LocalDateTime pickUptime;
1212
private Address pickUpAddress;
1313
private Address dropOffAddress;
1414

15-
public Booking(Address pickUpAddress, Date pickUptime, Address dropOffAddress, int costInCent, int etaInSeconds, String bookingCode) {
15+
public Booking(Address pickUpAddress, LocalDateTime pickUptime, Address dropOffAddress, int costInCent, int etaInSeconds, String bookingCode) {
1616
this.costInCent = costInCent;
1717
this.etaInSeconds = etaInSeconds;
1818
this.bookingCode = bookingCode;
@@ -33,7 +33,7 @@ public String getBookingCode() {
3333
return bookingCode;
3434
}
3535

36-
public Date getPickUptime() {
36+
public LocalDateTime getPickUptime() {
3737
return pickUptime;
3838
}
3939

@@ -42,12 +42,11 @@ public Address getDropOffAddress() {
4242
}
4343

4444
@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);
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);
4646
}
4747

4848
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-
);
49+
System.out.println(new Booking(new Address("a", "b"),
50+
LocalDateTime.now(), new Address("c", "d"), 123_00, 600, "abc"));
5251
}
5352
}

‎spring-remoting/remoting-http/client/src/main/java/com/baeldug/client/CabBookingClient.java‎

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,11 @@ public CabBookingClient(CabBookingService cabService) {
1010
this.cabService = cabService;
1111
}
1212

13-
public void run() {
13+
public void run() throws BookingException {
1414

1515
Address pickUp = new Address("13 Seagate Blvd, Key Largo, FL 33037", "US");
1616
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-
}
17+
System.out.println( cabService.bookPickUp(pickUp, dropDown, 3) );
2218

2319
}
2420

‎spring-remoting/remoting-http/client/src/main/java/com/baeldug/client/Main.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public CabBookingClient client(CabBookingService service){
2222
return new CabBookingClient(service);
2323
}
2424

25-
public static void main(String[] args) throws InterruptedException {
25+
public static void main(String[] args) throws Exception {
2626
AnnotationConfigApplicationContext rootContext =
2727
new AnnotationConfigApplicationContext();
2828
rootContext.scan(Main.class.getPackage().getName());

‎spring-remoting/remoting-http/server/src/main/java/com/baeldung/server/CabBookingServiceImpl.java‎

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,23 @@
55
import com.baeldung.api.BookingException;
66
import com.baeldung.api.CabBookingService;
77

8-
import java.util.Date;
8+
import java.time.LocalDateTime;
99

1010
import static java.lang.Math.random;
11-
import static java.lang.System.currentTimeMillis;
11+
import static java.time.LocalDateTime.now;
12+
import static java.time.temporal.ChronoUnit.MINUTES;
1213
import static java.util.UUID.randomUUID;
1314

1415
public class CabBookingServiceImpl implements CabBookingService {
1516

1617
@Override public Booking bookPickUp(Address pickUpLocation, Address dropOffLocation, int pax) throws BookingException {
17-
if(random()<0.3){
18+
if (random() < 0.3) {
1819
throw new BookingException("Cab unavailable");
1920
}
2021
int tripTimeInMinutes = (int) (5 + random() * 15);
2122
int costInCent = 15_00 + tripTimeInMinutes * 5 * pax;
22-
Date pickUpDate = new Date((long) (currentTimeMillis() + (1000 * 60 * random() * 15)));
23-
return new Booking(pickUpLocation, pickUpDate, dropOffLocation, costInCent, tripTimeInMinutes * 60,
24-
randomUUID().toString());
23+
LocalDateTime pickUpDate = now().plus(15L * (long) (random() * 10), MINUTES);
24+
25+
return new Booking(pickUpLocation, pickUpDate, dropOffLocation, costInCent, tripTimeInMinutes * 60, randomUUID().toString());
2526
}
2627
}

‎spring-remoting/remoting-http/server/src/main/resources/logback.xml‎

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,23 +5,12 @@
55
value="%-30(%d{MMM dd YYYY HH:mm:ss.SSS} [%thread]) %-5level %logger{5} [%file:%line] - %msg%n"/>
66

77
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
8-
<!-- encoders are assigned the type
9-
ch.qos.logback.classic.encoder.PatternLayoutEncoder by default -->
108
<encoder>
119
<pattern>${logPattern}</pattern>
1210
</encoder>
1311
</appender>
14-
15-
<appender name="FILE" class="ch.qos.logback.core.FileAppender">
16-
<file>C:\Users\danidemi\tmp\baledung\app.log</file>
17-
<append>false</append>
18-
<encoder>
19-
<pattern>${logPattern}</pattern>
20-
</encoder>
21-
</appender>
2212

2313
<root level="DEBUG">
2414
<appender-ref ref="STDOUT"/>
25-
<appender-ref ref="FILE"/>
2615
</root>
2716
</configuration>

0 commit comments

Comments
 (0)