Hi Dharam
Yes, of course. With Apache Geode, transaction management is applied at
the cache-level, as "local", cache-based transactions. Therefore, it is
only logical for the configuration of Geode's cache-based transaction
management to be (indirectly) set on the cache itself, which is then
registered with the (underlying) CacheTransactionManager instance [0] that
is only obtainable [1] once an instance of the cache exists.
Therefore, when using SDG, the configuration of Geode TransactionListeners
and the single TransactionWriter are set on the o.s.d.g.CacheFactoryBean,
using setTransactionListeners(:List<TransactionListener>) [2] and
setTransactionWriter(:TransactionWriter) [3].
There really is never a good reason to acquire a reference to Geode's
CacheTransactionManager [0] (i.e. using Geode's API) in the context of
local, cache transactions, since SDG appropriately integrates Geode's
cache-based local transactions into *Spring's* Transaction Abstraction and
Management infrastructure [4] (rooted in Spring's PlatformTransactionManager),
as described here [5] and as is apparent here [6].
Anyway...
If you are, say, combining SDG's Annotation based config with registering
your Geode TransactionListeners and TransactionWriter as Spring managed
beans in the Spring context, then it would look a little something like
this...
@SpringBootApplication
@PeerCacheApplication(name = "TransactionExample")
@EnableGemfireCacheTransactions
public class BootGeodeServerApplication {
public static void main(String[] args) {
new SpringApplicationBuilder(BootGeodeServerApplication.class)
.web(WebApplicationType.NONE)
.build()
.run(args);
}
@Bean
ApplicationRunner runner(GemFireCache gemfireCache) {
return args -> {
assertThat(gemfireCache).isNotNull();
CacheTransactionManager cacheTransactionManager =
gemfireCache.getCacheTransactionManager();
assertThat(cacheTransactionManager).isNotNull();
assertThat(cacheTransactionManager.getListeners())
.containsExactly(transactionListenerOne(), transactionListenerTwo());
assertThat(cacheTransactionManager.getWriter()).isSameAs(transactionWriter());
System.err.println("SUCCESS!");
};
}
@Bean
PeerCacheConfigurer
transactionListenersWriterConfigurer(List<TransactionListener>
transactionListenerBeans,
TransactionWriter transactionWriterBean) {
return (beanName, peerCacheFactoryBean) -> {
peerCacheFactoryBean.setTransactionListeners(transactionListenerBeans);
peerCacheFactoryBean.setTransactionWriter(transactionWriterBean);
};
}
@Bean
TransactionListener transactionListenerOne() {
return new TransactionListenerAdapter() { };
}
@Bean
TransactionListener transactionListenerTwo() {
return new TransactionListenerAdapter() { };
}
@Bean
TransactionWriter transactionWriter() {
return event -> { };
}
}
Of course, you can define as many TransactionListeners as is required by
your application and also configure them however necessary, such as
injecting them with other Spring managed beans.
Hope this helps!
-John
[0]
http://gemfire-95-javadocs.docs.pivotal.io/org/apache/geode/cache/CacheTransactionManager.html
[1]
http://gemfire-95-javadocs.docs.pivotal.io/org/apache/geode/cache/GemFireCache.html#getCacheTransactionManager--
[2]
https://docs.spring.io/spring-data/geode/docs/current/api/org/springframework/data/gemfire/CacheFactoryBean.html#setTransactionListeners-java.util.List-
[3]
https://docs.spring.io/spring-data/geode/docs/current/api/org/springframework/data/gemfire/CacheFactoryBean.html#setTransactionWriter-org.apache.geode.cache.TransactionWriter-
[4]
https://docs.spring.io/spring/docs/current/spring-framework-reference/data-access.html#transaction
[5]
https://docs.spring.io/spring-data/geode/docs/current/reference/html/#apis:transaction-management
[6]
https://docs.spring.io/spring-data/geode/docs/current/api/org/springframework/data/gemfire/transaction/GemfireTransactionManager.html
On Sun, Oct 7, 2018 at 9:32 PM, Dharam Thacker <[email protected]>
wrote:
> Hi John & Team,
>
> Could you suggest me a way to wire in TransactionListener/Writer via
> spring-data-geode annotation model/java config model?
>
> Transaction works if I do *@EnableTransactionManagement* but I could not
> find a way using java config to wire in TransactionListener/writer.
>
> Thanks & Regrads,
> - Dharam Thacker
>
--
-John
john.blum10101 (skype)