INT-4081: Custom PreparedStSetter for JdbcMChStore
JIRA: https://jira.spring.io/browse/INT-4081 * Add `MessageGroupPreparedStatementSetter` to let end-user to override the message insertion logic * Add tests and Docs
This commit is contained in:
committed by
Artem Bilan
parent
bf1fef39db
commit
34d0859515
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2016 the original author or authors.
|
||||
* Copyright 2002-2017 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -20,6 +20,9 @@ import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertNull;
|
||||
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.SQLException;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
|
||||
import org.junit.Before;
|
||||
@@ -27,8 +30,11 @@ import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.core.serializer.support.SerializingConverter;
|
||||
import org.springframework.integration.jdbc.store.JdbcChannelMessageStore;
|
||||
import org.springframework.integration.support.MessageBuilder;
|
||||
import org.springframework.jdbc.support.lob.DefaultLobHandler;
|
||||
import org.springframework.jdbc.support.lob.LobHandler;
|
||||
import org.springframework.messaging.Message;
|
||||
import org.springframework.test.annotation.DirtiesContext;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
@@ -42,6 +48,7 @@ import org.springframework.transaction.support.TransactionTemplate;
|
||||
/**
|
||||
* @author Gunnar Hillert
|
||||
* @author Gary Russell
|
||||
* @author Meherzad Lahewala
|
||||
*/
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@@ -50,6 +57,8 @@ public abstract class AbstractJdbcChannelMessageStoreTests {
|
||||
|
||||
protected static final String TEST_MESSAGE_GROUP = "AbstractJdbcChannelMessageStoreTests";
|
||||
|
||||
private static final String REGION = "AbstractJdbcChannelMessageStoreTests";
|
||||
|
||||
@Autowired
|
||||
protected DataSource dataSource;
|
||||
|
||||
@@ -64,7 +73,7 @@ public abstract class AbstractJdbcChannelMessageStoreTests {
|
||||
@Before
|
||||
public void init() throws Exception {
|
||||
messageStore = new JdbcChannelMessageStore(dataSource);
|
||||
messageStore.setRegion("AbstractJdbcChannelMessageStoreTests");
|
||||
messageStore.setRegion(REGION);
|
||||
messageStore.setChannelMessageStoreQueryProvider(queryProvider);
|
||||
messageStore.afterPropertiesSet();
|
||||
messageStore.removeMessageGroup("AbstractJdbcChannelMessageStoreTests");
|
||||
@@ -100,4 +109,40 @@ public abstract class AbstractJdbcChannelMessageStoreTests {
|
||||
assertEquals(message.getHeaders().getId(), messageFromDb.getHeaders().getId());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAddAndGetCustomStatementSetter() {
|
||||
messageStore.setMessageGroupPreparedStatementSetter(getMessageGroupPreparedStatementSetter());
|
||||
final Message<String> message = MessageBuilder.withPayload("Cartman and Kenny").build();
|
||||
|
||||
final TransactionTemplate transactionTemplate = new TransactionTemplate(transactionManager);
|
||||
|
||||
transactionTemplate.setIsolationLevel(Isolation.READ_COMMITTED.value());
|
||||
transactionTemplate.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED);
|
||||
|
||||
transactionTemplate.execute(new TransactionCallbackWithoutResult() {
|
||||
@Override
|
||||
protected void doInTransactionWithoutResult(TransactionStatus status) {
|
||||
messageStore.addMessageToGroup(TEST_MESSAGE_GROUP, message);
|
||||
}
|
||||
});
|
||||
Message<?> messageFromDb = messageStore.pollMessageFromGroup(TEST_MESSAGE_GROUP);
|
||||
assertNotNull(messageFromDb);
|
||||
assertEquals(message.getHeaders().getId(), messageFromDb.getHeaders().getId());
|
||||
}
|
||||
|
||||
private MessageGroupPreparedStatementSetter getMessageGroupPreparedStatementSetter() {
|
||||
return new MessageGroupPreparedStatementSetter() {
|
||||
|
||||
private SerializingConverter serializer = new SerializingConverter();
|
||||
private LobHandler lobHandler = new DefaultLobHandler();
|
||||
|
||||
@Override
|
||||
public void setValues(PreparedStatement preparedStatement, Message<?> requestMessage, Object groupId,
|
||||
String region, boolean priorityEnabled) throws SQLException {
|
||||
super.setValues(preparedStatement, requestMessage, groupId, region, priorityEnabled);
|
||||
byte[] messageBytes = serializer.convert(requestMessage);
|
||||
lobHandler.getLobCreator().setBlobAsBytes(preparedStatement, 6, messageBytes);
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user