How to declare and use global variables? - spring-integration

i have a console application.i want to read some configuration settings from DB. And these settings should be stored in a global or session variable so
i can use the variable through out the application ,
how can i implement it ? what are the ways ? which channels do i need to use ? and how payload should be stored?

It's not at all clear what you mean, but you can declare a Map object as a #Bean (or <bean/>) and load it up during initialization.
You can then reference it directly in your components by #Autowired or via SpEL in expressions in integration components: #mapBean['foo'].

Related

Inject RxHttpClient in Micronaut with url coming from an environment variable

When I am injecting a RxHttpClient in micronaut, I have an url with a token that I want to get from an environment variable to avoid hardcoding a secret.
In my service, I have injected the client like this:
#Client('${System.getenv(\'BOT_URL\')}')
#Inject
RxHttpClient httpClient
Being BOT_URL my url that's stored in an environment variable.
The project build but it fails while trying to use the client with this error:
2021-03-20 20:05:14.37 Could not resolve placeholder ${System.getenv('BOT_KEY')}
My variable is correctly defined in the server, how can I access to it when injecting the client?
Micronaut ships with pre-made PropertySourceLoaders that will resolve PropertySources from different sources. You can read more on externalized configuration through the docs.
Besides these PropertySourceLoaders, PropertySources are resolved from environment variables and are automatically injected and available for use within the ApplicationContext following the property source key syntax:
i.e. SOME_ENVIRONMENT_VARIABLE translates to some.environment.variable
Hence you can simply inject your HttpClient declaratively with the environment variable key translated to dot-separated-property-key syntax:
#Client('${bot.url}')
#Inject
RxHttpClient httpClient
You should be able to access environment variables just using ${BOT_URL}. I know for a fact that this works in the application.yml file. If it doesn't work in the annotation you can always create a property in application.yml with the value of the environment variable and use the property in your annotation.
For the docs on this, try the "Property value binding" section of the micronaut docs.

Apache Camel paho socketFactory setting

I am have created the camel route which use camel-paho component to consume MQTT stream. It is enabled with SSL and i need to pass the socket-factory. I went through the documentation od camel-paho and below parameter is available
socketFactory (security)
Sets the SocketFactory to use. This allows an application to apply its own policies around the creation of network sockets. If using an SSL connection, an SSLSocketFactory can be used to supply application-specific security settings.
I have passed the custom socket-factory in the URL by setting the above parameter to the class name as below
from("paho:"test?brokerUrl="+MQTT_BROKER_URL+"&clientId=subX4&cleanSession=false&socketFactory=com.sample.mqttCustomSocketFactory.java")
Above setting is not working. Is that the correct way of passing the parameter ?
Whenever you set a complex object to the Camel endpoint parameter, it needs to be a bean reference. So in your case, the endpoint should look something like this:
from("paho:test?...&socketFactory=#mySocketFactory")
where the #mySocketFactory is defined in the Camel bean registry like this:
SocketFactory mySocketFactory = ...
context.getRegistry().bind("mySocketFactory", mySocketFactory);
or in Spring context XML:
<bean id="mySocketFactory" ...>

Environment specific configuration of datasources in loopback4 application

I have just started my first loopback project and chosen loopback4 version for the application. Its purely a server application which will interact with databases (Redis and mongodb) and will call external API services due to micro-service architecture.
Now, I have 3 datasources in my application i.e. mongodb, Redis, and REST based datasource to call external services. I am facing 2 problems in going forward.
1. Environment specific configurations of Datasources: I need to maintain configuration for all three datasources according to the NODE_ENV environment variable. For lb3 i found this solution,
https://loopback.io/doc/en/lb3/Environment-specific-configuration.html#data-source-configuration
which does not work in lb4. One solution is to add configuration files having names mongodb.staging.json and mongodb.production.json and same for redis and rest datasources in directory src/datasources, and load this config according to NODE_ENV variable using if condition and pass it to the constructor of datasource. It works but it does not seem nice, as it should be application's responsibility to do this.
Can somebody suggest me lb3 equivalent solution for the above?
2. Calling External APIs via datasource: in lb4, To call external services its recommended to have a separate REST based datasource and its service to call it via controller. Now, In REST datasource config, one has to define a template of all the API calls which will happen to the external service https://loopback.io/doc/en/lb4/REST-connector.html#defining-a-custom-method-using-a-template.
As my application calls external service heavily with relatively large number of request parameters. It becomes really messy to declare each API call with its request params and to maintain this in the datasource config which will be environment specific.
Can somebody tell me a more robust and cleaner alternative of the above problem?
Thanks in advance!!
Using environment variables in datasource configs
The datasource config is simply a JSON file that's imported in into *.datasource.ts. Hence, you can replace that JSON file with a Typescript file and import it accordingly. LoopBack 4 does not provide any custom variable substitution mechanism. Instead, it is recommended to use process.env.
Recent CLI versions replace the JSON config in favour of using a single Typescript file:
import {inject} from '#loopback/core';
import {juggler} from '#loopback/repository';
const config = {
name: 'db',
connector: 'memory',
};
export class DbDataSource extends juggler.DataSource {
static dataSourceName = 'db';
static readonly defaultConfig = config;
constructor(
#inject('datasources.config.db', {optional: true})
dsConfig: object = config,
) {
super(dsConfig);
}
}
The dependency injection in the constructor allows you to override the config programmatically via the IoC container of the application.
Further reading
https://loopback.io/doc/en/lb4/DataSources.html
Calling external APIs without REST connector
The REST connector enforces a well-defined interface for querying external APIs so as to be able to do validation before sending out the request.
If this is not favourable, it is possible to create a new Service as a wrapper to the HTTP queries. From there, you can expose your own functions to handle requests to an external API. As Services do not need to follow a rigid structure, it is possible to customize it to your use-case.
It is also possible to create a new request directly inside the controller using either built-in or external libraries.
Overall, there isn't a 100% right or wrong way of doing certain things in LoopBack 4. Hence why the framework provides numerous ways to tackle the same issue.

Openwhisk - passing environment variables to action

Im using NodeJS action in openwhisk.
Is there any way to pass environment variables into whisk so I can read them in my NodeJS action using process.env ?
This is possible but you need to use a custom Docker runtime. The default built-in Node.js runtime does not support this. Apache OpenWhisk uses default action parameters, rather than environment parameters, to pass things like credentials and other application configuration to action code.
If you extend the existing Node.js Docker runtime for Apache OpenWhisk, you can set environment parameters in the build file for the image. This can then be used as the --docker parameter value when creating the action.

How to access application properties from controller/service with jhipster?

Into my application.yml there is a property : eureka.instance.appname
How can i access it from my Rest Controller?
In the parameters of you controller method you can add
#Value("${eureka.instance.appname}") String eurekaName
which you can use further.

Resources