Skip to content

Commit fa1e67f

Browse files
authored
Merge pull request #257 from graben/firstResource
Add NamedFirstXAResource for completeness of configuration. Remove obsolete unnamed implementations as resources are named by default.
2 parents 7296cb6 + 4013b0f commit fa1e67f

9 files changed

Lines changed: 101 additions & 101 deletions

File tree

narayana-spring-boot-core/src/main/java/dev/snowdrop/boot/narayana/core/jms/PooledXAConnectionFactoryWrapper.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,12 @@ public PooledXAConnectionFactoryWrapper(TransactionManager transactionManager, X
6161

6262
@Override
6363
protected ConnectionFactory wrapConnectionFactoryInternal(XAConnectionFactory xaConnectionFactory) {
64+
if (this.properties.isFirstResource() && this.properties.isLastResource()) {
65+
throw new IllegalArgumentException("Setting both firstResource and lastResource is not allowed");
66+
}
6467
JmsPoolNarayanaConnectionFactory pooledConnectionFactory = new JmsPoolNarayanaConnectionFactory();
6568
pooledConnectionFactory.setName(this.properties.getName());
69+
pooledConnectionFactory.setFirstResource(this.properties.isFirstResource());
6670
pooledConnectionFactory.setLastResource(this.properties.isLastResource());
6771
pooledConnectionFactory.setTransactionManager(this.transactionManager);
6872
pooledConnectionFactory.setConnectionFactory(xaConnectionFactory);

narayana-spring-boot-core/src/main/java/dev/snowdrop/boot/narayana/core/jms/pool/JmsPoolNarayanaConnectionFactory.java

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ public class JmsPoolNarayanaConnectionFactory extends JmsPoolXAConnectionFactory
3030
private static final long serialVersionUID = 1709204966732828338L;
3131

3232
private String name;
33+
private boolean firstResource;
3334
private boolean lastResource;
3435

3536
public String getName() {
@@ -40,6 +41,14 @@ public void setName(String name) {
4041
this.name = name;
4142
}
4243

44+
public boolean isFirstResource() {
45+
return this.firstResource;
46+
}
47+
48+
public void setFirstResource(boolean firstResource) {
49+
this.firstResource = firstResource;
50+
}
51+
4352
public boolean isLastResource() {
4453
return this.lastResource;
4554
}
@@ -50,11 +59,11 @@ public void setLastResource(boolean lastResource) {
5059

5160
@Override
5261
protected PooledNarayanaConnection createPooledConnection(Connection connection) {
53-
return new PooledNarayanaConnection(connection, getTransactionManager(), getName(), isLastResource());
62+
return new PooledNarayanaConnection(connection, getTransactionManager(), getName(), isFirstResource(), isLastResource());
5463
}
5564

5665
@Override
5766
protected JmsPoolXAJMSContext newPooledConnectionContext(JmsPoolConnection connection, int sessionMode) {
58-
return new JmsPoolNarayanaJmsContext(connection, sessionMode, getName(), isLastResource());
67+
return new JmsPoolNarayanaJmsContext(connection, sessionMode, getName(), isFirstResource(), isLastResource());
5968
}
6069
}

narayana-spring-boot-core/src/main/java/dev/snowdrop/boot/narayana/core/jms/pool/JmsPoolNarayanaJmsContext.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,25 +24,25 @@
2424
public class JmsPoolNarayanaJmsContext extends JmsPoolXAJMSContext {
2525

2626
private final String name;
27+
private final boolean firstResource;
2728
private final boolean lastResource;
2829

29-
public JmsPoolNarayanaJmsContext(JmsPoolConnection connection, int sessionMode, String name, boolean lastResource) {
30+
public JmsPoolNarayanaJmsContext(JmsPoolConnection connection, int sessionMode, String name, boolean firstResource, boolean lastResource) {
3031
super(connection, sessionMode);
3132
this.name = name;
33+
this.firstResource = firstResource;
3234
this.lastResource = lastResource;
3335
}
3436

3537
@Override
3638
public XAResource getXAResource() {
3739
XAResource xares = super.getXAResource();
38-
if (this.name != null) {
39-
if (this.lastResource) {
40-
xares = new NamedLastXAResource(xares, this.name);
41-
} else {
42-
xares = new NamedXAResource(xares, this.name);
43-
}
40+
if (this.firstResource) {
41+
xares = new NamedFirstXAResource(xares, this.name);
4442
} else if (this.lastResource) {
45-
xares = new LastXAResource(xares);
43+
xares = new NamedLastXAResource(xares, this.name);
44+
} else {
45+
xares = new NamedXAResource(xares, this.name);
4646
}
4747
return xares;
4848
}

narayana-spring-boot-core/src/main/java/dev/snowdrop/boot/narayana/core/jms/pool/LastXAResource.java

Lines changed: 0 additions & 83 deletions
This file was deleted.
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*
2+
* Copyright 2020 Red Hat, Inc, and individual contributors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package dev.snowdrop.boot.narayana.core.jms.pool;
18+
19+
import javax.transaction.xa.XAResource;
20+
21+
import org.jboss.tm.FirstResource;
22+
23+
public class NamedFirstXAResource extends NamedXAResource implements FirstResource {
24+
25+
public NamedFirstXAResource(XAResource xaResource, String name) {
26+
super(xaResource, name);
27+
}
28+
}

narayana-spring-boot-core/src/main/java/dev/snowdrop/boot/narayana/core/jms/pool/NamedLastXAResource.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import org.jboss.tm.LastResource;
2222

2323
public class NamedLastXAResource extends NamedXAResource implements LastResource {
24+
2425
public NamedLastXAResource(XAResource xaResource, String name) {
2526
super(xaResource, name);
2627
}

narayana-spring-boot-core/src/main/java/dev/snowdrop/boot/narayana/core/jms/pool/PooledNarayanaConnection.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,25 +28,25 @@
2828
public class PooledNarayanaConnection extends PooledXAConnection {
2929

3030
private final String name;
31+
private final boolean firstResource;
3132
private final boolean lastResource;
3233

33-
public PooledNarayanaConnection(Connection connection, TransactionManager transactionManager, String name, boolean lastResource) {
34+
public PooledNarayanaConnection(Connection connection, TransactionManager transactionManager, String name, boolean firstResource, boolean lastResource) {
3435
super(connection, transactionManager);
3536
this.name = name;
37+
this.firstResource = firstResource;
3638
this.lastResource = lastResource;
3739
}
3840

3941
@Override
4042
protected XAResource createXaResource(JmsPoolSession session) throws JMSException {
4143
XAResource xares = super.createXaResource(session);
42-
if (this.name != null) {
43-
if (this.lastResource) {
44-
xares = new NamedLastXAResource(xares, this.name);
45-
} else {
46-
xares = new NamedXAResource(xares, this.name);
47-
}
44+
if (this.firstResource) {
45+
xares = new NamedFirstXAResource(xares, this.name);
4846
} else if (this.lastResource) {
49-
xares = new LastXAResource(xares);
47+
xares = new NamedLastXAResource(xares, this.name);
48+
} else {
49+
xares = new NamedXAResource(xares, this.name);
5050
}
5151
return xares;
5252
}

narayana-spring-boot-core/src/main/java/dev/snowdrop/boot/narayana/core/properties/MessagingHubConnectionFactoryProperties.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ public class MessagingHubConnectionFactoryProperties {
2222

2323
private boolean enabled = false;
2424
private String name = "jms";
25+
private boolean firstResource = false;
2526
private boolean lastResource = false;
2627
private int maxConnections = 1;
2728
private Duration connectionIdleTimeout = Duration.ofSeconds(30);
@@ -48,6 +49,14 @@ public void setName(String name) {
4849
this.name = name;
4950
}
5051

52+
public boolean isFirstResource() {
53+
return this.firstResource;
54+
}
55+
56+
public void setFirstResource(boolean firstResource) {
57+
this.firstResource = firstResource;
58+
}
59+
5160
public boolean isLastResource() {
5261
return this.lastResource;
5362
}

narayana-spring-boot-core/src/test/java/dev/snowdrop/boot/narayana/core/jms/PooledXAConnectionFactoryWrapperTest.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import dev.snowdrop.boot.narayana.core.properties.MessagingHubConnectionFactoryProperties;
3232
import dev.snowdrop.boot.narayana.core.properties.RecoveryCredentialsProperties;
3333
import org.jboss.narayana.jta.jms.JmsXAResourceRecoveryHelper;
34+
import org.jboss.tm.FirstResource;
3435
import org.jboss.tm.LastResource;
3536
import org.junit.jupiter.api.BeforeEach;
3637
import org.junit.jupiter.api.Test;
@@ -42,6 +43,7 @@
4243
import org.mockito.junit.jupiter.MockitoExtension;
4344

4445
import static org.assertj.core.api.Assertions.assertThat;
46+
import static org.assertj.core.api.Assertions.assertThatThrownBy;
4547
import static org.mockito.ArgumentMatchers.any;
4648
import static org.mockito.BDDMockito.given;
4749
import static org.mockito.Mockito.mock;
@@ -95,6 +97,26 @@ void wrapWithCredentials() throws Exception {
9597
verify(this.mockRecoveryCredentialsProperties).getPassword();
9698
}
9799

100+
@Test
101+
void wrapWithFirstResource() throws Exception {
102+
given(this.spyMessagingHubConnectionFactoryProperties.isFirstResource()).willReturn(true);
103+
given(this.mockRecoveryCredentialsProperties.isValid()).willReturn(false);
104+
ConnectionFactory connectionFactory = this.wrapper.wrapConnectionFactory(this.mockXaConnectionFactory);
105+
106+
XAConnection mockXaConnection = mock(XAConnection.class);
107+
given(this.mockXaConnectionFactory.createXAConnection()).willReturn(mockXaConnection);
108+
given(mockXaConnection.getMetaData()).willReturn(mock(ConnectionMetaData.class));
109+
try (Connection connection = connectionFactory.createConnection()) {
110+
Transaction mockTransaction = mock(Transaction.class);
111+
given(this.mockTransactionManager.getTransaction()).willReturn(mockTransaction);
112+
ArgumentCaptor<XAResource> captorXaResource = ArgumentCaptor.captor();
113+
given(mockTransaction.enlistResource(captorXaResource.capture())).willReturn(true);
114+
try (Session ignored = connection.createSession()) {
115+
assertThat(captorXaResource.getValue()).isInstanceOf(FirstResource.class);
116+
}
117+
}
118+
}
119+
98120
@Test
99121
void wrapWithLastResource() throws Exception {
100122
given(this.spyMessagingHubConnectionFactoryProperties.isLastResource()).willReturn(true);
@@ -114,4 +136,14 @@ void wrapWithLastResource() throws Exception {
114136
}
115137
}
116138
}
139+
140+
@Test
141+
void invalidFirstLastResourceConfiguration() throws Exception {
142+
given(this.spyMessagingHubConnectionFactoryProperties.isFirstResource()).willReturn(true);
143+
given(this.spyMessagingHubConnectionFactoryProperties.isLastResource()).willReturn(true);
144+
given(this.mockRecoveryCredentialsProperties.isValid()).willReturn(false);
145+
assertThatThrownBy(() -> this.wrapper.wrapConnectionFactory(this.mockXaConnectionFactory))
146+
.isInstanceOf(IllegalArgumentException.class)
147+
.hasMessage("Setting both firstResource and lastResource is not allowed");
148+
}
117149
}

0 commit comments

Comments
 (0)