It is possible to consume the values of the properties file being served using Spring Config server.
Here's my current implementation to consume the values inside a file
Parameters params = new Parameters();
FileBasedConfigurationBuilder<PropertiesConfiguration> builder = new FileBasedConfigurationBuilder<PropertiesConfiguration>(
PropertiesConfiguration.class).configure(params.fileBased().setFile(new File("client-config.properties")));
PropertiesConfiguration config = builder.getConfiguration();
// Perform interpolation on all variables
PropertiesConfiguration extConfig =
(PropertiesConfiguration) config.interpolatedConfiguration();
So I was wondering if can use this kind of implementation but instead of getting a file, it retrieves the values from the Spring Config Server
Related
I am looking for a place to store some request scoped attributes such as user id using a Quarkus request filter. I later want to retrieve these attributes in a Log handler and put them in the MDC logging context.
Is Vertx.currentContext() the right place to put such request attributes? Or can the properties I set on this context be read by other requests?
If this is not the right place to store such data, where would be the right place?
Yes ... and no :-D
Vertx.currentContext() can provide two type of objects:
root context shared between all the concurrent processing executed on this event loop (so do NOT share data)
duplicated contexts, which are local to the processing and its continuation (you can share in these)
In Quarkus 2.7.2, we have done a lot of work to improve our support of duplicated context. While before, they were only used for HTTP, they are now used for gRPC and #ConsumeEvent. Support for Kafka and AMQP is coming in Quarkus 2.8.
Also, in Quarkus 2.7.2, we introduced two new features that could be useful:
you cannot store data in a root context. We detect that for you and throw an UnsupportedOperationException. The reason is safety.
we introduced a new utility class ( io.smallrye.common.vertx.ContextLocals to access the context locals.
Here is a simple example:
AtomicInteger counter = new AtomicInteger();
public Uni<String> invoke() {
Context context = Vertx.currentContext();
ContextLocals.put("message", "hello");
ContextLocals.put("id", counter.incrementAndGet());
return invokeRemoteService()
// Switch back to our duplicated context:
.emitOn(runnable -> context.runOnContext(runnable))
.map(res -> {
// Can still access the context local data
String msg = ContextLocals.<String>get("message").orElseThrow();
Integer id = ContextLocals.<Integer>get("id").orElseThrow();
return "%s - %s - %d".formatted(res, msg, id);
});
}
In ehcache2, I stored my properties externally and read them using a input stream which was an available option for configuring this, but the same option isn't present in ehcache3.
ehcache2 code which works: https://www.ehcache.org/documentation/2.8/code-samples.html
Create a CacheManager from a configuration in an InputStream.
try {
CacheManager manager = CacheManager.newInstance(fis);
} finally {
fis.close();```
Is there any work around for the same in ehcache3?
Ehcache3 doesn't have as straightforward a way of doing this as was present in ehcache2, but it definitely is possible.
You need to use the "import org.w3c.dom.Document" class and pass a Document to the XMLConfiguration constructor which you can use to build your cache manager:
cacheManager = CacheManagerBuilder.newCacheManager(xmlConfiguration);
The document itself is build like this:
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setNamespaceAware(true);
Document document = factory.newDocumentBuilder().parse(inputStream);
XMLConfiguration xmlConfiguration = new XMLConfiguration(document);
And now it should work. You might have xml related errors, but otherwise, this should work perfectly normal if your xml is of the correct format.
I have a property file as below
Key1~~value1
Key2~~value2
How to read this custom separator property file using Spring.
Spring #PropertySource can read the property files with keys and values separated by = or :.
On the presumption that you are using a created properties file. You can use the setGlobalSeparator(String globalSeparator) method of PropertiesConfigurationLayout and change the separator to use ~~. Per the documentation, it will overwrite the existing configuration layout to set the new global separator.
Something like this:
PropertiesConfiguration propertiesConfig = new PropertiesConfiguration("test.properties");
PropertiesConfigurationLayout layout = new PropertiesConfigurationLayout(propertiesConfig);
layout.setGlobalSeparator("~~");
propertiesConfig.setLayout(layout);
We're using a VelocityLayoutServlet as the view resolver in Spring Boot.
#Bean(name = "velocityViewResolver")
public VelocityLayoutViewResolver velocityViewResolver() {
VelocityLayoutViewResolver resolver = new VelocityLayoutViewResolver();
this.properties.applyToViewResolver(resolver);
resolver.setLayoutUrl("layout/default.vm");
return resolver;}
We want to load global macros from a VM_global_library.vm file, as described in the Velocity User Guide. Expected Velocity to load that default file from /templates directory, but not happening.
Adding theexplicit setting mentioned in the Velocity User Guide did not work either:
spring.velocity.velocimacro.library=VM_global_library.vm
velocimacro.library - A comma-separated list of all Velocimacro template libraries. By default, Velocity looks for a single library: VM_global_library.vm. The configured template path is used to find the Velocimacro libraries.
Are we missing some magic, or is this missing from the integration?
Velocity properties can be set with "spring.velocity.properties.*" attribute.
This configuration works for me:
spring.velocity.properties.velocimacro.library=templates/VM_global_library.vm
I have a Storm topology containing spouts/bolts.
There is some configuration data that is specific to a particular spout and
also a particular bolt that I would like to use (i.e. read from a config file)
so that it is not hard coded. Examples of config data is a filename that the
spout is to read from and a filename that a bolt is to write to.
I think config data is passed into the open and prepare methods.
How can I incorporate the component-specific data from a configuration file?
There are at least two ways to do this:
1) Include application-specific configuration in Storm config, which will be available during IBolt.prepare() ISpout.open() method calls. One strategy you could use is to have application prefix for the configuration keys, avoiding potential conflicts.
Config conf = new backtype.storm.Config();
// Storm-specific configuration
// ...
// ..
// .
conf.put("my.application.configuration.foo", "foo");
conf.put("my.application.configuration.bar", "foo");
StormSubmitter.submitTopology(topologyName, conf, topology);
2) Include component configuration during Spout/Bolt constructor.
Properties properties = new java.util.Properties();
properties.load(new FileReader("config-file"));
BaseComponent bolt = new MyBoltImpl(properties);