I would like to ask about camunda process with fluent BPMN model API in spring boot.
What is best practice for setting auto deployment and redeploy (versioning)?
#Configuration
public class BpmProcess {
private final Log logger = LogFactory.getLog(getClass());
#Autowired
private ProcessEngine processEngine;
#Bean
public BpmnModelInstance bpmnModelInstance() {
BpmnModelInstance modelInstance = Bpmn.createExecutableProcess("esign-store")
.name("esign store document")
.startEvent("esign-startEvent")
.serviceTask("esign-uploadStorage")
.name("upload to storage")
.camundaAsyncBefore()
.camundaDelegateExpression("${uploadStorageDelegate}")
.serviceTask("esign-uploadCezar")
.name("upload to cezar")
.camundaAsyncBefore()
.camundaDelegateExpression("${uploadCezarDelegate}")
.endEvent("esign-endEvent")
.camundaAsyncBefore()
.messageEventDefinition("esign-endEventDefinition")
.done();
MessageEventDefinition event = modelInstance.getModelElementById("esign-endEventDefinition");
event.setCamundaDelegateExpression("${endReplyDelegate}");
Bpmn.writeModelToStream(System.out, modelInstance);
return modelInstance;
}
#PostConstruct
public void deploy(){
processEngine.getRepositoryService()
.createDeployment()
.addModelInstance("esign-store.bpmn", bpmnModelInstance())
.name("esign-store-service")
.deploy();
}
}
I don't know if it is correctly.
#PostConstruct is too early, you have to wait until the engine is set up and running. With camunda spring boot, you can listen to the PostDeployEvent and use this a s a hook:
#SpringBootApplication
#EnableProcessApplication
public class MyApp {
...
#EventListener
public void onStart(PostDeployEvent event) {
// deploy here
}
}
Related
I try to get spring boot working with cdi events.
I have the following class which fires the event.
#Component
#UIScope
public class Login extends LoginOverlay
{
#Autowired
private UserInfo userInfo;
#Inject
private Event<UpdateCWViewEvent> cwevent;
#PostConstruct
public void init()
{
addLoginListener(new ComponentEventListener<LoginEvent>()
{
#Override
public void onComponentEvent(LoginEvent event)
{
userInfo.login(event.getUsername(), event.getPassword());
if (userInfo.isLoggedIn())
{
setButtonLabel();
close();
cwevent.fire(new UpdateCWViewEvent());
}
}
});
}
}
And in another class the following method
public void update(#Observes(notifyObserver=Reception.IF_EXISTS) UpdateCWViewEvent event)
{
//do something
}
Now I have the following problem. I need an Implementation of javax.enterprise.event.Event. I tried to take weld and use the standard Eventimpl. Now, I tried to configure a Spring Configuration class to tell my application, there is an implementation of my event.
#Configuration
public class Config
{
#Bean
public Event<UpdateCWViewEvent> cwEvent()
{
//return EventImpl.of(injectionPoint, beanManagerImpl);
}
}
I dont know what to do with the injectionPoint and beanManagerImpl. Does anybody of you had the same problem and solved it? Or does anybody have an alternative to fire easy cdi events in a spring boot application?
Thank you very much and stay healthy!
We are still using Spring Boot 1.5.x and we want to start using TestContainers. However, all examples are with Spring boot 2.x which is using TestPropertyValues class only available in 2.x. Is it even possible to apply new property values to the configurable context in 1.5.x?
This is the code working in 2.x:
#RunWith(SpringRunner.class)
#SpringBootTest
#ContextConfiguration(initializers = {UserRepositoryTCIntegrationTest.Initializer.class})
public class UserRepositoryTCIntegrationTest extends UserRepositoryCommonIntegrationTests {
#ClassRule
public static PostgreSQLContainer postgreSQLContainer = new PostgreSQLContainer("postgres:11.1")
.withDatabaseName("integration-tests-db")
.withUsername("sa")
.withPassword("sa");
static class Initializer
implements ApplicationContextInitializer<ConfigurableApplicationContext> {
public void initialize(ConfigurableApplicationContext configurableApplicationContext) {
TestPropertyValues.of(
"spring.datasource.url=" + postgreSQLContainer.getJdbcUrl(),
"spring.datasource.username=" + postgreSQLContainer.getUsername(),
"spring.datasource.password=" + postgreSQLContainer.getPassword()
).applyTo(configurableApplicationContext.getEnvironment());
}
}
}
good question :). You have different options to setup your testcontext with Spring Boot 1.5 + TestContainers. Instead of using an indirect way by setting the datasource-properties with dynamic values (like in your example code), you can use the following option:
Provide DataSource Bean via #TestConfiguration
#RunWith(SpringRunner.class)
#DataJpaTest
#AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE)
public class YourRepositoryIntTest {
#Autowired
private YourRepository sut;
#Test
public void testMethod() {
// Given
String expectedId = "SOMEID";
// When
Entity entity = sut.testMethod();
// Then
Assertions.assertThat(entity.getId()).isEqualTo(expectedId);
}
#TestConfiguration
public static class Config {
#Bean
public MySQLContainer testContainer() {
MySQLContainer container = new MySQLContainer();
container.start();
return container;
}
#Bean
#Primary
public DataSource dataSource(MySQLContainer container) {
return DataSourceBuilder.create()
.url(container.getJdbcUrl())
.username(container.getUsername())
.password(container.getPassword())
.driverClassName(container.getDriverClassName())
.build();
}
}
}
Database containers can be launched by simply using a JDBC URL scheme:
application.properties
spring.datasource.driver-class-name=org.testcontainers.jdbc.ContainerDatabaseDriver
spring.datasource.url=jdbc:tc:postgresql:11://localhost/test
Note:
Testcontainers needs to be on your application's classpath at runtime for this to work
I have created a spring cloud task that will perform some specific task based on the requirement. I wanted to call this task from another spring boot application. Please let me know is there any way of calling the below task from an external application.
#SpringBootApplication
#EnableTask
public class FileGenerationTaskApplication {
#Autowired
private DataSource dataSource;
public class FileGeneratorTaskConfigurer extends DefaultTaskConfigurer {
public FileGeneratorTaskConfigurer(DataSource dataSource){
super(dataSource);
}
}
#Bean()
public FileGeneratorTaskConfigurer getTaskConfigurer() {
return new FileGeneratorTaskConfigurer(dataSource);
}
public static void main(String[] args) {
SpringApplication.run(FileGenerationTaskApplication.class, args);
}
#Component
public static class FileGeneratorTaskRunner implements ApplicationRunner {
#Autowired
private FulfillmentFileGenerationService service;
public void run(ApplicationArguments args) throws Exception {
System.out.println("FileGeneratorTaskRunner from Spring Cloud Task!");
service.fulFillmentFileGenerationTask();
}
}
}
Can we create a REST api to call the spring cloud task?
It would be nice to have the Task registered on Spring Cloud Dataflow.
After you have your Task registered, you can make REST calls to trigger the Task. Check this example out.
You can also use Spring Cloud Dataflow Rest Client
DataFlowOperations dataFlowOperations = new DataFlowTemplate(URI.create(springDataFlowUri));
TaskOperations operations = dataFlowOperations.taskOperations();
Then you can start launching the Tasks previously got using the API Rest.
In case you do not want to use Spring Cloud DataFlow, remember when you create a Task, this is a Spring Boot Application by itself, so you can expose end points to trigger the Task.
Spring boot integration test looks like this
#RunWith(SpringRunner.class)
#SpringBootTest(classes = Application)
class IntegrationTest {
static QpidRunner qpidRunner
#BeforeClass
static void init() {
qpidRunner = new QpidRunner()
qpidRunner.start()
}
#AfterClass
static void tearDown() {
qpidRunner.stop()
}
}
So, Qpid instance is run before and teared down after all tests. I want to know is there a way to check whether spring boot application is still running before calling qpidRunner.stop(). I want to stop Qpid only when I'm sure that spring app has finished its stopping.
The Spring Boot integration test can configure an ApplicationListener which listens for ContextClosedEvent. Define a nested #TestConfiguration class inside the test class to add beans to the application's primary configuration.
#TestConfiguration
static class MyConfiguration {
#Bean
public ApplicationListener<ContextClosedEvent> contextClosedEventListener() {
return event -> qpidRunner.stop();
}
}
Taking into account that ConfigurableWebApplicationContext can be injected in a SpringBootTest, adding this lines to the code solved the problem
static ConfigurableWebApplicationContext context
#Autowired
void setContext(ConfigurableWebApplicationContext context) {
AbstractDocsIntegrationTest.context = context
}
#AfterClass
static void tearDown() {
context.stop()
qpidRunner.stop()
}
Spring docs about stop method
Stop this component, typically in a synchronous fashion, such that the
component is fully stopped upon return of this method.
JUnit AfterClass annotated method must be static, therefore #Autowired workaround with setContext method.
I have an infrastructure based on EclipseLink + JPA Guice Persist
When I redeploy the application always I have caching problems with caching Entitys and I have to reboot the server (Oracle Weblogic 11g) .This problem is treated in a this post: https://bugs.eclipse.org/bugs/show_bug.cgi?id=326552 But, maybe is not a bug ¿?¿? ...
I managed to solve the problem as follows :
Originally I have centralized everything in a GuiceModule:
1.Create the module JPAPersist
2.Binding of a Initializer class thas invokes the persistenceService.start()
public class MyGuiceModule implements Module {
#Override
public void configure(final Binder binder) {
Properties props = _dbConnectionPropertiesForPool();
JpaPersistModule jpaModule = new JpaPersistModule(persistenceUnit);
jpaModule.properties(props);
binder.install(jpaModule);
binder.bind(JPAInitializer.class).asEagerSingleton();
}
public class JPAInitializer {
#Inject
public JPAInitializer(final PersistService service) {
service.start();
}
}
Everything works fine .... but as I said when redeploy remain cached instances
HOW DO I HAVE SOLVED?
I changed the method JPAInitializer
public static class JPAInitializer {
private static PersistService _persistenceService = null;
#Inject
public JPAInitializer(final PersistService service) {
_persistenceService = service;
_persistenceService.start();
}
public static void stop() {
_persistenceService.stop();
}
}
I created a method stop () that stops the service ..but WTF! I have been forced to save the the injected Persistence service in a static variable :((
From the guice / listener filter that is the entrypoint invoke the stop when the application is undeployed (onContextDestroyed)
public void contextDestroyed(ServletContextEvent servletContextEvent) {
JPAInitializer.stop();
}
Now, when i redeploy there is no cache issue or problem, and there is no need to restart the server
It works this way, but I do not know if it's all right to create a static instance PesistenceService., so i'm trying to find another way to invoke the stop.....
Any suggestion?
Found solution .
Create a inteface to handle Guice Persistence Service :
interface MyPersistenceServiceHandler {
public void start();
public void stop();
}
This will be used into the main DB Guice Module :
binder.bind(MyPersistenceServiceHandler .class)
.to(JPAPersistenceServiceControl.class)
.in(Singleton.class);
static class JPAPersistenceServiceControl
implements MyPersistenceServiceHandler {
private final PersistService _service;
#Inject
public JPAPersistenceServiceControl(final PersistService service) {
_service = service;
}
#Override
public void start() {
if (_service == null) throw new IllegalStateException("NO persistence service available!");
_service.start();
}
#Override
public void stop() {
if (_service == null) throw new IllegalStateException("NO persistence service available!");
_service.stop();
}
}
Get the instance in the RESTEndoint/Guice filter through Guice Injector.
jpaServiceHandler = _myGuiceInjector.getInstance(MyPersistenceServiceHandler .class);
Start the service on contextInitialized : jpaServiceHandler.start();
Stop the service on contextDeproyed : jpaServiceHandler.stop();