Skip to content

Commit 5664513

Browse files
committed
JBTM-3803 re-enable narayana-spring-boot quickstart
1 parent e726f2a commit 5664513

File tree

13 files changed

+170
-148
lines changed

13 files changed

+170
-148
lines changed

spring/narayana-spring-boot/README.md

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,17 +25,15 @@ Usage
2525

2626
In your terminal navigate to the quickstart directory and execute one of the following scenarios:
2727

28-
* Commit demonstration
28+
* Commit and rollback demonstration
2929

30-
mvn clean spring-boot:run -Drun.arguments="commit,Test Value"
30+
mvn clean spring-boot:run -Dspring-boot.run.arguments="commit Test-Value"
3131

32-
* Rollback demonstration
33-
34-
mvn clean spring-boot:run -Drun.arguments="rollback,Test Value"
32+
mvn clean spring-boot:run -Dspring-boot.run.arguments="rollback Test-Value"
3533

3634
* System crash and recovery demonstration
3735

38-
mvn clean spring-boot:run -Drun.arguments="crash,Test Value"
36+
mvn clean spring-boot:run -Dspring-boot.run.arguments="crash Test-Value"
3937

40-
mvn spring-boot:run -Drun.arguments="recovery"
38+
mvn spring-boot:run -Dspring-boot.run.arguments="recovery"
4139

spring/narayana-spring-boot/pom.xml

Lines changed: 8 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -53,24 +53,23 @@
5353
</dependencyManagement>
5454

5555
<dependencies>
56-
<!-- spring-boot-starter-data-jpa includes hibernate-core -->
5756
<dependency>
5857
<groupId>org.springframework.boot</groupId>
5958
<artifactId>spring-boot-starter-data-jpa</artifactId>
6059
</dependency>
61-
<!-- javassist dependency updates javassist version 3.22.0-GA included in hibernate-core -->
62-
<dependency>
63-
<groupId>org.javassist</groupId>
64-
<artifactId>javassist</artifactId>
65-
<version>3.25.0-GA</version>
66-
</dependency>
6760
<dependency>
6861
<groupId>org.springframework.boot</groupId>
6962
<artifactId>spring-boot-starter-artemis</artifactId>
7063
</dependency>
7164
<dependency>
72-
<groupId>org.springframework.boot</groupId>
73-
<artifactId>spring-boot-starter-jta-narayana</artifactId>
65+
<groupId>dev.snowdrop</groupId>
66+
<artifactId>narayana-spring-boot-core</artifactId>
67+
<version>3.5.0</version>
68+
</dependency>
69+
<dependency>
70+
<groupId>dev.snowdrop</groupId>
71+
<artifactId>narayana-spring-boot-starter</artifactId>
72+
<version>3.5.0</version>
7473
</dependency>
7574
<dependency>
7675
<groupId>com.h2database</groupId>
@@ -80,20 +79,6 @@
8079
<groupId>org.apache.activemq</groupId>
8180
<artifactId>artemis-jms-server</artifactId>
8281
</dependency>
83-
<dependency>
84-
<groupId>org.apache.commons</groupId>
85-
<artifactId>commons-dbcp2</artifactId>
86-
<exclusions>
87-
<exclusion>
88-
<groupId>commons-logging</groupId>
89-
<artifactId>commons-logging</artifactId>
90-
</exclusion>
91-
</exclusions>
92-
</dependency>
93-
<dependency>
94-
<groupId>org.apache.commons</groupId>
95-
<artifactId>commons-pool2</artifactId>
96-
</dependency>
9782
<dependency>
9883
<groupId>org.springframework.boot</groupId>
9984
<artifactId>spring-boot-starter-test</artifactId>
@@ -110,7 +95,6 @@
11095
<plugin>
11196
<groupId>org.apache.maven.plugins</groupId>
11297
<artifactId>maven-surefire-plugin</artifactId>
113-
<version>${version.surefire.plugin}</version>
11498
<configuration>
11599
<skip>false</skip>
116100
<argLine>${modular.jdk.args}</argLine>

spring/narayana-spring-boot/src/main/java/org/jboss/narayana/quickstart/spring/EntriesRepository.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,5 @@
55
/**
66
* @author <a href="mailto:[email protected]">Gytis Trikleris</a>
77
*/
8-
public interface EntriesRepository extends JpaRepository<Entry, Long> {
9-
8+
interface EntriesRepository extends JpaRepository<Entry, Integer> {
109
}
Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,42 @@
11
package org.jboss.narayana.quickstart.spring;
22

3-
import org.springframework.beans.factory.annotation.Autowired;
3+
import java.util.List;
4+
5+
import org.slf4j.Logger;
6+
import org.slf4j.LoggerFactory;
47
import org.springframework.stereotype.Service;
58

69
import jakarta.transaction.Transactional;
7-
import java.util.List;
810

911
/**
1012
* Service to store entries in the database.
1113
*
1214
* @author <a href="mailto:[email protected]">Gytis Trikleris</a>
1315
*/
1416
@Service
15-
@Transactional
1617
public class EntriesService {
1718

19+
private final Logger logger = LoggerFactory.getLogger(EntriesService.class);
1820
private final EntriesRepository entriesRepository;
1921

20-
@Autowired
2122
public EntriesService(EntriesRepository entriesRepository) {
2223
this.entriesRepository = entriesRepository;
2324
}
2425

25-
public Entry create(String value) {
26-
System.out.println("Creating entry '" + value + "'");
26+
@Transactional
27+
public Entry createEntry(String value) {
28+
this.logger.info("Creating entry '{}'", value);
29+
return this.entriesRepository.save(new Entry(value));
30+
}
2731

28-
return entriesRepository.save(new Entry(value));
32+
public List<Entry> getEntries() {
33+
List<Entry> entries = this.entriesRepository.findAll();
34+
this.logger.info("Returning entries '{}'", entries);
35+
return entries;
2936
}
3037

31-
public List<Entry> getAll() {
32-
return entriesRepository.findAll();
38+
public void clearEntries() {
39+
this.entriesRepository.deleteAll();
3340
}
3441

3542
}
Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package org.jboss.narayana.quickstart.spring;
22

3+
import java.util.Objects;
4+
35
import jakarta.persistence.Entity;
46
import jakarta.persistence.GeneratedValue;
57
import jakarta.persistence.Id;
@@ -14,22 +16,38 @@ public class Entry {
1416
@GeneratedValue
1517
private Long id;
1618

17-
private String value;
19+
private String val;
1820

1921
Entry() {
20-
2122
}
2223

23-
public Entry(String value) {
24-
this.value = value;
24+
public Entry(String val) {
25+
this.val = val;
2526
}
2627

27-
public String getValue() {
28-
return value;
28+
public String getVal() {
29+
return this.val;
2930
}
3031

3132
@Override
3233
public String toString() {
33-
return "Entry{id=" + id + ", value='" + value + "'}";
34+
return "Entry{id=" + this.id + ", value='" + this.val + "'}";
35+
}
36+
37+
@Override
38+
public boolean equals(Object o) {
39+
if (this == o) {
40+
return true;
41+
}
42+
if (o == null || getClass() != o.getClass()) {
43+
return false;
44+
}
45+
Entry entry = (Entry) o;
46+
return Objects.equals(this.id, entry.id) && Objects.equals(this.val, entry.val);
47+
}
48+
49+
@Override
50+
public int hashCode() {
51+
return Objects.hash(this.id, this.val);
3452
}
3553
}
Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
package org.jboss.narayana.quickstart.spring;
22

3-
import org.springframework.beans.factory.annotation.Autowired;
3+
import java.util.LinkedList;
4+
import java.util.List;
5+
6+
import org.slf4j.Logger;
7+
import org.slf4j.LoggerFactory;
8+
import org.springframework.jms.annotation.JmsListener;
49
import org.springframework.jms.core.JmsTemplate;
510
import org.springframework.stereotype.Service;
611

@@ -12,18 +17,35 @@
1217
* @author <a href="mailto:[email protected]">Gytis Trikleris</a>
1318
*/
1419
@Service
15-
@Transactional
1620
public class MessagesService {
1721

22+
public final static String QUEUE_NAME = "test-messages";
23+
private final Logger logger = LoggerFactory.getLogger(MessagesService.class);
24+
private final List<String> receivedMessages = new LinkedList<>();
1825
private final JmsTemplate jmsTemplate;
1926

20-
@Autowired
2127
public MessagesService(JmsTemplate jmsTemplate) {
2228
this.jmsTemplate = jmsTemplate;
2329
}
2430

25-
public void send(String message) {
26-
jmsTemplate.convertAndSend(MessagesListener.QUEUE_NAME, message);
31+
@Transactional
32+
public void sendMessage(String message) {
33+
this.logger.info("Sending message '{}' to '{}' queue", message, QUEUE_NAME);
34+
this.jmsTemplate.convertAndSend(QUEUE_NAME, message);
35+
}
36+
37+
public List<String> getReceivedMessages() {
38+
this.logger.info("Returning received messages '{}'", this.receivedMessages);
39+
return this.receivedMessages;
2740
}
2841

42+
public void clearReceivedMessages() {
43+
this.receivedMessages.clear();
44+
}
45+
46+
@JmsListener(destination = QUEUE_NAME)
47+
public void onMessage(String message) {
48+
this.logger.info("Received message '{}'", message);
49+
this.receivedMessages.add(message);
50+
}
2951
}

spring/narayana-spring-boot/src/main/java/org/jboss/narayana/quickstart/spring/QuickstartApplication.java

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,18 @@
22

33
import org.springframework.boot.SpringApplication;
44
import org.springframework.boot.autoconfigure.SpringBootApplication;
5-
import org.springframework.boot.jta.narayana.DbcpXADataSourceWrapper;
6-
import org.springframework.context.ApplicationContext;
5+
import org.springframework.context.ConfigurableApplicationContext;
76
import org.springframework.context.annotation.Import;
87

9-
import java.io.Closeable;
8+
import dev.snowdrop.boot.narayana.autoconfigure.NarayanaAutoConfiguration;
109

1110
/**
1211
* Main Spring Boot application class.
1312
*
1413
* @author <a href="mailto:[email protected]">Gytis Trikleris</a>
1514
*/
1615
@SpringBootApplication
17-
@Import(DbcpXADataSourceWrapper.class)
16+
@Import(NarayanaAutoConfiguration.class)
1817
public class QuickstartApplication {
1918

2019
public static final Object TO_WAIT = new Object();
@@ -26,7 +25,7 @@ public static void main(String[] args) throws Exception {
2625
throw new IllegalArgumentException("Invalid arguments provided. See README.md for usage examples");
2726
}
2827

29-
ApplicationContext context = SpringApplication.run(QuickstartApplication.class, args);
28+
ConfigurableApplicationContext context = SpringApplication.run(QuickstartApplication.class, args);
3029
QuickstartService quickstartService = context.getBean(QuickstartService.class);
3130

3231
switch (CompleteAction.valueOf(args[0].toUpperCase())) {
@@ -45,12 +44,18 @@ public static void main(String[] args) throws Exception {
4544
case RECOVERY:
4645
quickstartService.demonstrateRecovery();
4746
}
48-
49-
((Closeable) context).close();
47+
context.close();
5048
}
5149

5250
public enum CompleteAction {
53-
COMMIT, ROLLBACK, CRASH, RECOVERY
51+
52+
COMMIT("COMMIT"), ROLLBACK("ROLLBACK"), CRASH("CRASH"), RECOVERY("RECOVERY");
53+
54+
public final String label;
55+
56+
private CompleteAction(String label) {
57+
this.label = label;
58+
}
5459
}
5560

5661
}

spring/narayana-spring-boot/src/main/java/org/jboss/narayana/quickstart/spring/QuickstartService.java

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66

77
import jakarta.transaction.TransactionManager;
88
import javax.transaction.xa.XAResource;
9+
10+
import java.util.ArrayList;
11+
import java.util.Collection;
912
import java.util.List;
1013

1114
/**
@@ -64,11 +67,13 @@ public void demonstrateCrash(String entry) throws Exception {
6467
* @throws Exception
6568
*/
6669
public void demonstrateRecovery() throws Exception {
67-
List<Entry> entriesBefore = entriesService.getAll();
70+
Iterable<Entry> entriesBefore = entriesService.getEntries();
6871
System.out.println("Entries at the start: " + entriesBefore);
6972
recoveryManagerService.addXAResourceRecovery(new DummyXAResourceRecovery());
70-
waitForRecovery(entriesBefore);
71-
System.out.println("Entries at the end: " + entriesService.getAll());
73+
List<Entry> target = new ArrayList<>();
74+
entriesBefore.forEach(target::add);
75+
waitForRecovery(target);
76+
System.out.println("Entries at the end: " + entriesService.getEntries());
7277
}
7378

7479
/**
@@ -80,17 +85,17 @@ public void demonstrateRecovery() throws Exception {
8085
* @throws Exception
8186
*/
8287
private void executeDemonstration(String entry, TransactionTerminator terminator, XAResource xaResource) throws Exception {
83-
System.out.println("Entries at the start: " + entriesService.getAll());
88+
System.out.println("Entries at the start: " + entriesService.getEntries());
8489

8590
transactionManager.begin();
8691
if (xaResource != null) {
8792
transactionManager.getTransaction().enlistResource(xaResource);
8893
}
89-
entriesService.create(entry);
90-
messagesService.send("Created entry '" + entry + "'");
94+
entriesService.createEntry(entry);
95+
messagesService.sendMessage("Created entry '" + entry + "'");
9196
terminator.terminate();
9297

93-
System.out.println("Entries at the end: " + entriesService.getAll());
98+
System.out.println("Entries at the end: " + entriesService.getEntries());
9499
}
95100

96101
/**
@@ -104,7 +109,7 @@ private void waitForRecovery(List<Entry> entriesBefore) throws Exception {
104109

105110
for (int i = 0; i < 3 && !isComplete; i++) {
106111
sleep(5000);
107-
isComplete = entriesBefore.size() < entriesService.getAll().size();
112+
isComplete = entriesBefore.size() < ((Collection<?>) entriesService.getEntries()).size();
108113
}
109114

110115
if (isComplete) {

0 commit comments

Comments
 (0)