Spring Batch - Use the application's Transaction Manager - spring-batch

I have a spring batch job as part of an application. The application already has a TransactionManager and when I use the #EnableBatchProcessing Spring Batch also tries to create a TransactionManager in the SimpleBatchConfiguration. I can't use the override bean property here as I need to use the application's TransactionManager. I was thinking creating a Custom annotation like the #EnableBatchProcessing and use a custom configuration which does not create a TransactionManager. Is there a better way to set or configure SpringBatch to use the existing application's TransactionManager and not create it's own?

This is a known issue: https://github.com/spring-projects/spring-batch/issues/816, which was fixed in v5.0.0-M1.
Is there a better way to set or configure SpringBatch to use the existing application's TransactionManager and not create it's own?
Unless you remove the usage of #EnableBatchProcessing, you won't be able to prevent the exposure of the transaction manager bean because of the #Bean annotation on org.springframework.batch.core.configuration.annotation.SimpleBatchConfiguration#transactionManager.
That said, you can provide a custom BatchConfigurer and override getTransactionManager to instruct Spring Batch to use a custom transaction manager. This is mentioned in documentation here.

Related

Apache Shiro: How do I add a new annotation method interceptor into the mix?

I am looking at this class: https://shiro.apache.org/static/1.3.1/xref/org/apache/shiro/spring/security/interceptor/AopAllianceAnnotationsAuthorizingMethodInterceptor.html
It is registering all method interceptors that work with Shiro. Among them is this interceptor:
PermissionAnnotationMethodInterceptor .
I want to create my own custom interceptor and integrate it into the Shiro model.
My own interceptor would be replacing the existing PermissionAnnotationMethodInterceptor.
How do I do it in a clean programmatic Spring Boot way?
You should be able to replace the AuthorizationAttributeSourceAdvisor bean with your own implementation
Or implement your own annotations anyway you want and disable Shiro's annotation processing: shiro.annotations.enabled=false

#SpringBootTest how to pre-populate embedded MongoDB?

Doing implementation of a microservice with a few endpoints based on Spring Boot and MongoDB and trying to write integration tests using #SpringBootTest annotation capabilities.
At the moment, I am facing an issue that I need to pre-populate an embedded MongoDB instance that instantiated only during 'test' phrase with some test data.
And I did not find any out-of-the-box option available in Spring Boot for this purpose.
Some people advice to use for test data pre-populating tools like mongobee or mongoprefill or nosql-unit but for me, it seems like overhead or workaround, do not want to introduce any new dependencies even in test scope.
So could you please advice: In the current Spring Boot ecosystem, what is the right way to pre-populate MongoDB for testing purpose, when we are talking about integration (end-to-end) testing with #SpringBootTest?
There are multiple ways to pre-populate data:
Use the JUnit lifecycle methods like #BeforeEach, #BeforeAll to fill in data
You could disable the Spring Boot autoconfiguration for the embedded MongoDB and do it on your own and insert data after creating the connection
You could somehow mirror the #Sql feature we have for testing relational databases and write something similar using the AsbtractTestExectuionListener. For this have a look at the Spring class SqlScriptsTestExecutionListener
Provide a class that implements the CommandLineRunner interface and only activate this bean for your integration test profile with #Profile("integration-test")

EclipseLink (JPA) table-based multitenancy with JTA, how?

Our application project is an OSGI bundle using JPA with EclipseLink and JTA, and needs single-table multi-tenancy, where tenant ID comes from a REST request. From what I've read and tried, it almost seems that is impossible:
Since tenant ID changes depending on the request, each request with a new tenant ID needs to manually create a new PersistenceContext (EntityManager) with the appropriate property.
But persistence contexts can't be manually created when using JTA (#PersistenceUnit does not get injected and Persistence.createEntityManagerFactory does not work), according to http://tomee.apache.org/jpa-concepts.html.
Am I missing something? Or is this literally impossible to do?
You can set multitenant/discriminator properties in the entity manager for a request. But it is not safe for multi-threading and lazy initialization.
I tried our CMobileCom JPA that supports single-table multitenancy. For each tenant, a new EntityManager should be used. That is, an EntityManager should not be shared to access data for multiple tenants. This is also true for EclipseLink.
Disclaimer: I am a developer of CMobileCom JPA, a light weight JPA implementation for Java and Android.

Spring-Boot #RepositoryRestResource How to override a save method with custom behaviour

In my app, I need to customized the call of the save method over a POST Restful service to post an event to a RabbitMQ queue.
Each time a consumer of my API is firing a POST on my resource, I want to publish an event on my RabbitMQ queue to make some asynchronous processing.
Right now, I use #RepositoryRestResource and Spring-Data-Jpa to expose a CRUD API over my Spring-Data JPA Repository. It does the job, very straightforward and simple. I'd like to stick with that so in the case of a POST (save method) I'd like to compose or change the behaviour. I need to store the data in my database but also to publish an event in the RabbitMQ queue.
I tried several solution but I failed.
May be you have the solution .
How to I extend a particular method in my Rest CRUD repository ?
One way that I have solved this kind of problem in the past is to use Aspect Oriented Programming, and luckily since you are using the Spring Framework it is quite easy and well documented. You could put "Around" advice around the constructor for the domain objects (just a suggestion) and have it send a message to the RabbitMQ Exchange.
Another way of doing this is to use the Log4j AMQP appender and log the object prior to saving it.
You can use a custom org.springframework.context.ApplicationListener. Spring Data REST offers the convenient base class AbstractRepositoryEventListener.
#Component
public class PublishToRabbitMQAfterSavingYourEntity extends AbstractRepositoryEventListener<YourEntity> {
#Override
public void onAfterSave(YourEntity entity) {
// publish to RabbitMQ
}
}

Using Hystrix with Spring Data Repositories

Given that one of the main benefits of Spring Data and the related REST repositories is that most of the time the developer doesn't have to worry about the underlying implementations, is there an out-of-the-box way to leverage the Spring Cloud Netflix libraries, specifically the Hystrix annotations in this case, without extending every call in the provided Repository interfaces or creating my own implementation?
Currently you need to wrap calls in another service whose methods are annotated with #HystrixCommand. Because of the way both Spring Data and the Hystrix Aspect work (they both create proxies), there would need to be specific integration in Spring Data for #HystrixCommand. #ccit-spence is right, you really want to put #HystrixCommand on the services calling into a Spring Data REST repository.