bus properties settings ignored - java

I had cxf.xml file:
<beans ....
<cxf:bus>
<cxf:features>
<cxf:logging/>
</cxf:features>
</cxf:bus>
</beans>
but during some operations, i got error:
org.apache.cxf.interceptor.Fault: Unmarshalling Error: Maximum Number of Child Elements limit (50000) Exceeded
and I found out that it can be overridden with org.apache.cxf.stax.maxChildElements value. So i first tried putting org.apache.cxf.stax.maxChildElements=120000 in gradle, in IDEA arguments, which didnt work, so i modified my .xml file like this:
<cxf:bus>
<cxf:features>
<cxf:logging/>
</cxf:features>
<cxf:properties>
<entry key="org.apache.cxf.stax.maxChildElements" value="120000"/>
</cxf:properties>
</cxf:bus>
but none of this worked, and I am currently without ideas, as of why this setting is not getting registered.
Im working with libraries cxf-rt-frontend-jaxws and cxf-rt-transports-http both on version 3.2.7.

You can try doing configuration via java code as well.
You can get Bus instance which is used for creating your org.apache.cxf.endpoint.Client:
YourActual service = //return instance of your #WebService class by defining address & interpreter;
Client client=ClientProxy.getClient(service);
client.getBus().setProperty(StaxUtils.MAX_CHILD_ELEMENTS,120000)
Or if you have your endpoint implementor then you can do :
Bus bus=BusFactory.getDefaultBus();
bus.setProperty(StaxUtils.MAX_CHILD_ELEMENTS,120000);
Object implementor = <Your service implementation class>;
EndpointImpl ep = (EndpointImpl) Endpoint.publish(yourendpoint, implementor);
ep.setBus(bus);
Above Bus configuration psudo-code taken from CXF doc.

If when you say "IDEA Arguments" you mean this box (labeled: Program Arguments)...
Try instead enabling Modify Options -> Add VM Options
Then write your VM Option string in the box labeled VM options.
try
-Dorg.apache.cxf.stax.maxChildElements=1000000
This may or may not work depending on what you meant by IDEA Arguments. I have had IDEA ignore many options that I have tried to pass to the JVM via the "Program Arguments" box at work. Hopefully this resolves your issue. If this fails, I will try to monitor this thread.

Related

How do I "inject" structured configuration information with Spring?

I have the following scenario:
There will be a Java language testbed system consisting of a number of "services" that collaborate by passing messages to one another. The service implementation is intended to be generic, i.e. there is no specific "business logic" contained within. For the purposes of the testbed it is important to be able to create various collections of services, configured externally (if possible).
The services themselves are unaware of the existence of any other service. Each service simply subscribes to the topics where it expects to receive information and publishes on topics where it sends information out to any waiting subscribers. With careful configuration it then would be possible to simulate a data flow graph.
The plan is to configure a given service instance by providing configuration information that describes the information needed to set up subscribers (readers) and publishers (writers). The configuration information may include other properties not related to publish/subscribe.
Below is a possible example:
Note: XML was chosen for the example simply because it's easy enough to read and allows for structured data.
<service>
<name>Service A</name>
<service-id>service ID</service-id>
<publish>
<per-second>5</per-second>
<topic>
<name>Topic 1</name>
<class>org.xyz.Topic1</class>
<!-- override 5/sec rate -->
<per-second>10</per-second>
</topic>
<topic>
<name>Topic 2</name>
<class>org.xyz.Topic2</class>
</topic>
</publish>
<subscribe>
<topic>
<name>Topic 3</name>
<class>org.xyz.Topic3</class>
</topic>
</subscribe>
</service>
<service>
<name>Service B</name>
<service-id>service ID</service-id>
<publish>
<per-second>30</per-second>
<topic>
<name>Topic 3</name>
<class>org.xyz.Topic3</class>
</topic>
</publish>
<subscribe>
<topic>
<name>Topic 2</name>
<class>org.xyz.Topic2</class>
</topic>
</subscribe>
</service>
...
I would like to use the Spring framework to help with the configuration of these services. Note: I am very new to Spring and am currently reading Spring in Action (and other sources) to educate myself.
What I would like to know is: How could I "inject" the configuration information to some sort of controller or factory that would then use it to create the collection of services and provide them with the necessary information to create the readers and writers they will use to receive and send messages?
From what I've read so far, Spring appears to be pretty powerful WRT to dependency injection and "bean wiring", but I don't know enough about what can (and cannot) be done, nor how to do it.
I'm not partial to whether Spring is configured by Java or XML. I just used XML because it easy to put together, allows for data structuring and seems to be used everywhere. If it makes more sense to specify the configuration a different way, just let me know.
How a given service would handle an event (i.e. receive a specific message) to possibly send out a message "response", or take some other action, is a topic outside the scope of this question. I am researching how that could be done - mainly at rules based processing. If anyone has suggestions, I will gladly take a look at them.
Make a config file like this:
some.paremeter=cool
some.other.parameter=awesome
named myconfig.properties. Make sure the file is in your classpath, then include -Dspring.config.name=myconfig in your vm args; then in the xml you can use ${some.parameter} etc. In particular, putting the config file in <project-root>/config/ will work.
For example, here is a simple MongoClient from one of my projects that uses a spring config:
<!--Mongo-->
<bean id="mongoClient" class="com.mongodb.MongoClient">
<constructor-arg>
<bean class="com.mongodb.MongoClientURI">
<constructor-arg type="java.lang.String"
value="mongodb://${mongo.db.user}:${mongo.db.password}#${mongo.db.host}:${mongo.db.port}/${mongo.db.database}"/>
</bean>
</constructor-arg>
</bean>

Changing JAVA DNS Cache settings

I am using java8 for my web application. I would like to change the settings of JAVA DNS Cache.
This is the code:
java.security.Security.setProperty("networkaddress.cache.ttl", "60");
java.security.Security.setProperty("sun.net.inetaddr.negative.ttl", "10");
I would like to know where exactly I should write the code (in which class file) to reflect the changes in JVM before DNS chance gets initialised.
I would like to make the change using a java utility class file.Please suggest for the same.
And also suggest how to configure the same change in build.xml(ant)?
I agree with answers updated where they have suggested to change security file.But i would like to know the configuration in other ways too due to limitations that i have in my project.
You can also set it globally by add this line
networkaddress.cache.ttl=60
to $JAVA_HOME/jre/lib/security/java.security file.
Your best bet is to set it directly in the java.security file located in /lib/security
Note: if you are using a shared JVM, you'll need to set this in your startup command -Djava.security.properties=/DirectoryPath/filename and set the value of security.overridePropertiesFile to true.
If you want to set this in code then you have a number of options - you just need to put the code in a place that you know will be executed before the first message is processed after application startup.
My preference would be to register a ContextListener and put the code in there.
I encountered a similar issue where my java application wouldn't resolve the URL.
In addition to what is suggested ( i.e.,networkaddress.cache.ttl and networkaddress.cache.negative.ttl) I had to reset the caches in the URL Object.
URL url = new URL(urlSrt);
URLConnection con = url.openConnection();
con.setUseCaches(false);
I am not aware of (build.xml)ant. I will still give it a try just in case you are using spring framework. Following is what I did to set the networkaddress.cache.ttl at the time of application initialisation.
Define a new Java class as follows.
package com.example.util;
public class SecurityManager {
public SecurityManager() {
java.security.Security.setProperty("networkaddress.cache.ttl", "60");
}
}
Ask spring framework to instantiate the object of above class as a singleton at the time on spring container creation.
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean id="securityManager"
class="com.example.util.SecurityManager" scope="singleton" />
</beans>

How to manipulate a list bean defined in XML in Java

I have an XML Spring configuration containing a list
<util:list id="deviceList" value-type="package.path.to.Device">
<ref bean="device1"/>
<ref bean="device2"/>
</util:list>
that is being autowired into my Java code:
#Autowired
private List<Device> devices;
As I am working on a dynamic web project, it is possible to add and delete devices to/from the list at runtime.
I have a thread that triggers sensors on the devices periodically and that gets the initial device list by constructor injection:
<bean id="sensorTriggerThread" scope="singleton" class="package.path.to.SensorTriggerThread">
<constructor-arg>
<ref bean="deviceList"/>
</constructor-arg>
</bean>
I can add and delete new devices normally with the devices.add / devices.remove method, but that does not influence the list in the XML file, which results in the newly created devices not getting triggered. I am fairly new to Spring and it seems I found that autowiring does not work in both directions.
How can I manipulate the device list from the XML file in a Java class?
I know that it is possible to somehow edit this list as a bean so that all parts of the application that relate to it also get these changes.
EDIT: I should mention I have no write access to the SensorTriggerThread file as it is part of a jar library.
I have a thread that triggers the devices periodically and that gets
the initial device list by constructor injection.
You can always set the new device list through a setter method in sensorTriggerThread.
I solved the problem by changing the #Autowired annotation to an #Resource(name="deviceList") annotation. Not sure why this was necessary as I only have 1 list of devices configured in the XML file but now it works.

can't set a different filter in file:inbound-channel-adapter and scanner?

Below is part of my Spring Integration config :
<bean id="recursiveScanner" class="org.springframework.integration.file.RecursiveLeafOnlyDirectoryScanner" >
<property name="filter" ref="skipTmpFileFilter" />
</bean>
<bean id="skipTmpFileFilter" class="org.springframework.integration.file.filters.RegexPatternFileListFilter">
<constructor-arg value="^[^~].*"/>
</bean>
<file:inbound-channel-adapter directory="${inbound.folder}"
scanner="recursiveScanner"
id="fileChannel"
filter="fileNameFilter">
<integration:poller id="poller" fixed-delay="10000" />
</file:inbound-channel-adapter>
As you can see, I'd like to define 2 different filters :
one to skip temp files, in the recursiveScanner
one more advanced in which I have defined some other patterns, fileNameFilter (details of which are not relevant, so I'm not providing it)
What I see when I launch this in debug mode is that first, skipTmpFileFilter is set in recursiveScanner, but it is overwritten by fileNameFilter a bit after, making skipTmpFileFilter ineffective.
Is it the intended behavior or a bug ? I think it would make sense to be able to configure 2 different filters, one generic (in scanner) and one more specific (in the inbound adapter). Here, I'm kind of forced to use a composite filter.
Thanks
Vincent
If we take a look to the source code of FileReadingMessageSource, we'll see:
public void setFilter(FileListFilter<File> filter) {
Assert.notNull(filter, "'filter' must not be null");
this.scanner.setFilter(filter);
}
And there is no more stuff around the filter for the FileReadingMessageSource. Everything is delegated to the DirectoryScanner.
So, there is no any choice, unless provide only one filter option: or for the DirectoryScanner bean, or <file:inbound-channel-adapter>.
And yes: to have several filters in place you should use CompositeFileListFilter.
However I think we can protect that point of the override case.
Feel free to raise JIRA issue on the matter.

Spring putting dynamically generated values into placeholders

I am new to Spring. I now understand how to use placeholders to read values from a properties file:
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="classpath:properties.txt"/>
</bean>
<int-mqtt:outbound-channel-adapter id="mqtt-publish"
client-id="${clientID}"
client-factory="clientFactory"
auto-startup="true"
url="${url}"
default-qos="${qos}"
default-retained="${retain}"
default-topic="${topic}" />
Everything works fine with the code above... But... Is it possible for instance to replace the clientID by something generated at runtime (or from user input) instead of statically reading it from a properties file?
By runtime, do you mean dynamically for each message?
In that case, no, because the clientId is used while establishing the connection, which is done once (or when the connection to the server is lost).
If you mean to provide a dynamic value programmatically when the application context initializes, then, yes, the Spring Expression Language is the solution.
For example, #{myBean.myProperty} will call the getMyProperty() method on a bean myBean and #{myBean.someMethod()} will invoke someMethod().
Also see the dynamic-ftp sample, which uses placeholders at runtime by creating a new outbound adapter on demand using property placeholders, in a child application context.

Categories