profiles in spring batch - spring-batch

I am trying to understand how to use #profiles in spring-batch.
I created a java file with two classes in it:
#Configuration
#Profile("nonlocal")
class NonLocalConfiguration {
}
and
#Configuration
#Profile("local")
class LocalConfiguration {
}
in the main java class I am trying to set the profiles as follows:
AbstractApplicationContext context = new ClassPathXmlApplicationContext("applicationcontext.xml");
String run_env = System.getenv("profile");
System.out.println("run_env is: " + run_env);
context.getEnvironment().setActiveProfiles(run_env);
context.refresh();
I set the profile using an Environment variable as profile=local
When the program is executed, I get a null pointer exception which I think is not getting the profile correctly. How can I use profiles concept in Spring Batch? Will it be different in normal spring vs spring batch?

As far as I understood, you have to set the profile before you create the ApplicationContext. Once the application contenxt is created, changing the profile will not have any influence.
From the SpringBoot documentation, chapter 25.2 http://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-profiles.html
Programmatically setting profiles
You can programmatically set active
profiles by calling SpringApplication.setAdditionalProfiles(…​) before
your application runs. It is also possible to activate profiles using
Spring’s ConfigurableEnvironment interface.

Related

Map Rest Controller to Camunda Workflow

My work starts from Create Order.This task will be triggered from some GUI screen.
I have written a RestController which will be exposed to GUI.How can i map CreateOrderController to the Camunda CreateOrder task .And I need to pass OrderInfo object to the next task i.e."Place Order"
#RestController
public class CreateOrderController {
#PostMapping("/rest/create/order")
public String createOrder(#RequestBody OrderInfo orderInfo) {
System.out.println(" Order created with Order id " + orderInfo.getOrderId());
return "Order id created with " + orderInfo.getOrderId() ;
}
From within you controller, you can use the Java API
https://docs.camunda.org/javadoc/camunda-bpm-platform/7.18/org/camunda/bpm/engine/TaskService.html#complete(java.lang.String,java.util.Map)
if the controller is running in the same JVM. If the controller is running on a different environment you need to use the REST API:https://docs.camunda.org/manual/7.18/reference/rest/task/post-complete/
...but is the instance already running when you perform the user task?
It seems like you may want to remove the user task and instead start the process from your rest controller. For this you should look at:
https://docs.camunda.org/javadoc/camunda-bpm-platform/7.18/org/camunda/bpm/engine/RuntimeService.html#startProcessInstanceById(java.lang.String)
or
https://docs.camunda.org/manual/7.18/reference/rest/process-definition/post-start-process-instance/
You may also find the free training on Camunda Academy helpful:
https://academy.camunda.com/page/camunda-7

How to set the offset.commit.policy to AlwaysCommitOffsetPolicy in debezium?

I created a Debezium Embedded engine to capture MySQL change data. I want to commit the offsets as soon as I can. In the code, the config is created including follows.
.with("offset.commit.policy",OffsetCommitPolicy.AlwaysCommitOffsetPolicy.class.getName())
Running this returns, java.lang.NoSuchMethodException: io.debezium.embedded.spi.OffsetCommitPolicy$AlwaysCommitOffsetPolicy.<init>(io.debezium.config.Configuration)
However, When I start the embedded engine with,
.with("offset.commit.policy",OffsetCommitPolicy.PeriodicCommitOffsetPolicy.class.getName()), the embedded engine works fine.
Note that the class OffsetCommitPolicy.PeriodicCommitOffsetPolicy constructor includes the config parameter while OffsetCommitPolicy.AlwaysCommitOffsetPolicy doesn't.
public PeriodicCommitOffsetPolicy(Configuration config) {
...
}
How to get the debezium embedded engine to use its AlwaysCommitOffsetPolicy?
Thanks for the report. This is partly bug (which we would appreciate if you could log into our Jira). You can solve this issue by calling a dedicated method embedded engine builder like `io.debezium.embedded.EmbeddedEngine.create().with(OffsetCommitPolicy.always())'
Tested with version 1.4.0Final:
new EmbeddedEngine.BuilderImpl() // create builder
.using(config) // regular config
.using(OffsetCommitPolicy.always()) // explicit commit policy
.notifying(this::handleEvent) // even procesor
.build(); // and finally build!

How to pass Proxy settings to custom updater code with Install4j API?

We are implementing the updater service using the install4j APIs (without using the Updater.exe). We could successfully use the APIs as below to get the possible update version
UpdateCheckRequest updateCheckRequest = new UpdateCheckRequest(updatesUrl).applicationDisplayMode(ApplicationDisplayMode.UNATTENDED)
.askForProxy(false).connectTimeout(10000).readTimeout(20000);
UpdateDescriptor updateDescriptor = UpdateChecker.getUpdateDescriptor(updateCheckRequest);
return updateDescriptor.getPossibleUpdateEntry();
However, we are missing the below JVM arguments, (to set proxy settings and enable logging) which are available in the updater screens.
-Dinstall4j.noProxyAutoDetect=true
-DproxySet=true
-DproxyHost= “”,
-DproxyPort=””
-DproxyAuth="true"
-DproxyAuthUser=””
-DproxyAuthPassword=””
-Dinstall4j.keepLog=true -Dinstall4j.alternativeLogfile=${installer:sys.installationDir}/logs/patch-agent-updater.log
Please let us know how to pass them to the custom updater ?
In this case the update checker runs in the same process, so you can set these properties via System.setProperty.

SpringApplicationContextLoader ignores Application class

The SpringApplicationContextLoader assumes that the application is either using 100% XML or 100% Java config. This is because #ContextConfiguration allows either a list of classes or locations/value, not both. If any is specified, SpringApplicationContextLoader ignores the Application class that creates and starts the SpringApplication.
Trying to make Boot work with a 100% Groovy/no-XML pet project, I ran across the above issue. My Application class has #EnableAutoConfiguration and #ComponentScan annotations on it, the former required by Boot to set up a Web server. The later I had to keep because of SPR-11627. On the other hand, if I omitted the locations/value on #ContextConfiguration, dependencies weren't set up (duh!).
I give the code below along with a patch that I locally made to SpringApplicationContextLoader. If there's a better way, please let me know.
MovieDatabaseRESTClientIntegrationTest.groovy
RunWith(SpringJUnit4ClassRunner)
#ContextConfiguration(value = ['classpath:client-config.groovy', 'classpath:integ-test-config.groovy'],
loader = PatchedSpringApplicationContextLoader)
#SpringApplicationConfiguration(classes = MovieDatabaseApplication)
#WebAppConfiguration
#IntegrationTest
class MovieDatabaseRESTClientIntegrationTest {
MovieDatabaseApplication.groovy
#EnableAutoConfiguration
#ComponentScan
class MovieDatabaseApplication {
SpringApplicationContextLoader.java fix
private Set<Object> getSources(MergedContextConfiguration mergedConfig) {
Set<Object> sources = new LinkedHashSet<Object>();
sources.addAll(Arrays.asList(mergedConfig.getClasses()));
sources.addAll(Arrays.asList(mergedConfig.getLocations()));
/* The Spring application class may have annotations on it too. If such a class is declared on the test class,
* add it as a source too. */
SpringApplicationConfiguration springAppConfig = AnnotationUtils.findAnnotation(mergedConfig.getTestClass(),
SpringApplicationConfiguration.class);
if (springAppConfig != null) {
sources.addAll(Arrays.asList(springAppConfig.classes()));
}
if (sources.isEmpty()) {
throw new IllegalStateException(
"No configuration classes or locations found in #SpringApplicationConfiguration. "
+ "For default configuration detection to work you need Spring 4.0.3 or better (found "
+ SpringVersion.getVersion() + ").");
}
return sources;
}
Also posted on Spring forum.
I could be wrong but I don't think there is any support for beans{} configuration in #ContextConfiguration and #SpringContextConfiguration is just an extension of that. A feature request in JIRA would be appropriate. Also there has never been any support for mixed configuration format (as the entry point at least) - you always have to choose either XML or #Configuration, or else supply your own ContextLoader. You also shouldn't have both #ContextConfiguration and #SpringContextConfiguration on the same class (the behaviour is undefined).

EventLogInstaller Full Setup with Categories?

It appears the MSDN docs are broken concerning creating an Event Log completely along with a definitions file for messages. I am also lost on how to setup Categories (I have custom numbers in the 3000's for messages).
Can anyone point me to a link or show sample code on how to make this right?
You should start (if you haven't done so already) here:
EventLogInstaller Class (System.Diagnostics)
The sample provided there is the foundation for what you want to do. To sum it up, build a public class inheriting from System.Configuration.Install.Installer in an assembly (could be the same DLL where you have the rest of your application, a separate DLL, or an EXE file), decorate it with the RunInstaller attribute, and add your setup code in the constructor:
using System;
using System.Configuration.Install;
using System.Diagnostics;
using System.ComponentModel;
[RunInstaller(true)]
public class MyEventLogInstaller: Installer
{
private EventLogInstaller myEventLogInstaller;
public MyEventLogInstaller()
{
// Create an instance of an EventLogInstaller.
myEventLogInstaller = new EventLogInstaller();
// Set the source name of the event log.
myEventLogInstaller.Source = "NewLogSource";
// Set the event log that the source writes entries to.
myEventLogInstaller.Log = "MyNewLog";
// Add myEventLogInstaller to the Installer collection.
Installers.Add(myEventLogInstaller);
}
}
When you have your assembly compiled, you may use the InstallUtil tool available through the Visual Studio Command Prompt to run the installer code.
Regarding the message definition file (which includes category definitions), the MSDN documentation for EventLogInstaller.MessageResourceFile mentions that you should create an .mc file, compile it, and add it as a resource to your assembly. Digging around, I found an excellent post which should guide you to the end, here:
C# with .NET - Event Logging (Wayback Machine)