An excerpt from this http://www.mulesoft.org/documentation/display/current/Configuring+Java+Components is:
When you specify the class directly on the component or pooled-component element, the PrototypeObjectFactory is used by default, and a new instance is created for each invocation, or a new pooled component is created in the case of the PooledJavaComponent
And, I have configured a Java class as Mule Java component like below:
<component class="com.mycompany.SalesOrderProductsHandler" doc:name="Java" />. The class SalesOrderProductsHandler has implemented org.mule.api.lifecycle.Callable and has one state variable named targetProductsIndex.
My question follows:
Will a new instance of com.mycompany.SalesOrderProductsHandler get created every time a new request comes?
The documentation is absolutely correct. With:
<component class="com.mycompany.SalesOrderProductsHandler" />
you will get a new instance of com.mycompany.SalesOrderProductsHandler for each invocation.
Related
This question already has an answer here:
Disable a Spring Scheduler Task via Property
(1 answer)
Closed 5 years ago.
I have a small Spring webapp. Besides my plain unit tests, I'm writing a unit test that just verifies required bean wiring. I'm using the default applicationContext.xml file, not a "test" version. I do have some fake test resources that normally defined in my Tomcat JNDI context.
The test basically works, but one annoyance is that some scheduled tasks that are defined in the default context start up and emit some error messages that don't effect the test result.
The scheduled tasks are defined in the context like this:
<task:scheduled-tasks>
<task:scheduled ref="ratesQueryProcessor" method="run" fixed-rate="30000"/> <!-- Every 120 seconds -->
</task:scheduled-tasks>
Is there something that I can do in the spring context resulting from the default applicationContext.xml and my "test resources" XML file, and perhaps a JavaConfig class, which would "override" these scheduled tasks to turn them off?
If it matters, here's a small excerpt from my unit test class:
#RunWith(SpringRunner.class)
#ContextConfiguration(value = {"file:src/main/webapp/WEB-INF/applicationContext.xml", "/testResources.xml"})
//#ContextHierarchy({
// #ContextConfiguration("file:src/main/webapp/WEB-INF/applicationContext.xml"),
// #ContextConfiguration(classes = SpringWiringTest.Config.class)
//})
#TestPropertySource(properties = { "env = tomcat", "doNotifications = false" })
public class SpringWiringTest {
The commented out section is because I'm attempting to define my test resources in a JavaConfig class, but at this point I'm unable to use BOTH an XML file and a JavaConfig class (I have another SO posting asking about this).
This is a duplicate of Disable a Spring Scheduler Task via Property.
The solution is to use bean definition profiles (a.k.a., environment profiles).
I've started with SI and kind of stuck right now as we want to use SI in one of our existing project avoiding changes where we can.
A bean which we would be using as a service activator accepts an constructor argument of a java object.
that object is in the payload but then I'm unable to set it using inner bean usage of service-activator
<service-activator input-channel="ADMIN_TEST_CONNECTION" method="testConnection">
<beans:bean class="mypackage.request.AdminRequestProcessor">
<beans:constructor-arg value="payload"/>
</beans:bean>
</service-activator>
it's complaining about Could not convert argument value of type [java.lang.String] to required type.
Please help in how to access payload and set it as an constructor argument.
If I go via non- constructor arg route and change existing java object then it works with this call in the service activator
expression="#bean.testConnection(payload)"/>
but I don't wish you to change the existing java code until there is no other way.
I think you don't have choice unless change something or add code around existing.
Service-Activator performs its functionality against each incoming message in the input-channel. And that functionality is exactly method invocation where Message is used as a context for method arguments.
Not sure what you are going to do with that payload, but that doesn't look correct to use statefull object like your AdminRequestProcessor. Just don't forget that you may have many incoming message, but service-activator should be consistent.
Plus don't forget that <beans:bean> is singleton, so your AdminRequestProcessor is instantiated only once.
Looking to your sample I'd suggest something like this:
expression="new AdminRequestProcessor(payload).testConnection()"/>
If you really don't want to change anything in your code.
Everything rest you can find in the Reference Manual.
I Have a JSF-2.2 web app on a WildFly 8.1 app server shiping Hibernate-validator 5.1
I want to set some constrainst programmaticaly using the fluent API, because they depends on the case for example a min and max of a #Size constraint could vary or a field could be #NotNull or not...
so I try to programmaticaly configure constraints such as describe here : http://docs.jboss.org/hibernate/validator/5.0/reference/en-US/html_single/#section-programmatic-api
I do somthing like that to try (in a EJB #Singleton #Startup):
HibernateValidatorConfiguration configuration = Validation
.byProvider( HibernateValidator.class )
.configure();
ConstraintMapping constraintMapping = configuration.createConstraintMapping();
constraintMapping
.type( Car.class )
.property( "manufacturer", FIELD )
.constraint( new NotNullDef() )
.property( "licensePlate", FIELD )
.ignoreAnnotations()
.constraint( new NotNullDef() )
.constraint( new SizeDef().min( 2 ).max( 14 ) );
Validator validator = configuration.addMapping( constraintMapping )
.buildValidatorFactory()
.getValidator();
But then JSF don't use this new constraints mapping.
I can submit forms without problem even if I break the constraints programmaticaly set
I don't know how to configure the Validator or ValidatorFactory JSF is using or how to provide to JSF an other Validator or ValidatorFactory...
Or may be It's more about configuring WildFly server, something to do in a config file or JNDI, I don't have a clue...
EDIT
I try to bind new Validator and validator factory in JNDI
But I can't because "Naming context is read-only"
Hashtable jndiProperties = new Hashtable();
jndiProperties.put(Context.URL_PKG_PREFIXES, "org.jboss.ejb.client.naming");
Context context = new InitialContext(jndiProperties);
context.bind("java:comp/Validator", factory.getValidator());
context.bind("java:comp/ValidatorFactory", factory);
Thank you Hardy
As you proposed I post Hibenate Validator improvement
https://hibernate.atlassian.net/browse/HV-955
There is no way atm to do what you are after. Hibernate Validator has indeed the programmatic mapping, but it is a Hibernate Validator specific feature. There is no way to bootstrap this functionality in a Bean Validation way. I am saying this, since the only way to customize your ValidatorFactory and hence Validator instance within the container is via validation.xml. And there is no mechanism for the fluent API in this configuration file.
Your JNDI idea is in principal good, but as you say, it is only read only.
validation.xml allows for vendor specific properties though. One could imagine a property like org.hibernate.validator.config_factory=acme.MyConfig. The value of the property would point to a fully specified class which would contain some sort of factory method which returns the programmatic mapping to be added to the configuration. Unfortunately, such a property does not yet exist. You could open an issue here though ;-)
I have a web application thats using JSF 2. In this application I am using a charting library which is getting data from an xml file, the application updates the xml file, when someone accesses the site, because of jsf 2 Action. Now I want to implement the Quartz library the open source scheduling library, to update the xml file and not rely on the users action, but I have no idea how to call an Action from Quartz using JSF 2.
Thanks in advance guys.
Generally speaking , you should implement your scheduled logic , define when it will run , and initialize your scheduled jobs when the application server starts.
Implement scheduled logic
Your scheduled class should implement org.quartz.Job interface and override its execute() which contains the logic of your scheduled job. In your case , it is the method to update the XML file. You should make this method does not have any dependencies on JSF such that it can be called outside the JSF .
public class MyScheduledJob implements Job {
public void execute(JobExecutionContext context) throws JobExecutionException {
updateXML();
}
}
Initialize and start Quartz
Quartz provides a ServletContextListener called QuartzInitializerListener that allows you to initialize and start Quartz when the application server starts .
Add this listener to your web.xml
<listener>
<listener-class> org.quartz.ee.servlet.QuartzInitializerListener</listener-class>
</listener>
By default , it will look for a file called quartz.properties in the classpath to initialize Quartz . You can refer this for the more info about configurable options available in quartz.properties
Define which Job will run at which time
You can define it in a XML file (Its schema definition can be found here ) and configure XMLSchedulingDataProcessorPlugin in quartz.properties to load this XML when Quartz is initialized.
For example , in the quartz.properties
org.quartz.plugin.jobInitializer.class
=org.quartz.plugins.xml.XMLSchedulingDataProcessorPlugin org.quartz.plugin.jobInitializer.fileNames = quartz-config.xml
org.quartz.plugin.jobInitializer.failOnFileNotFound = true
Then in the quartz-config.xml
<?xml version="1.0" encoding="UTF-8"?>
<job-scheduling-data
xmlns="http://www.quartz-scheduler.org/xml/JobSchedulingData"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.quartz-scheduler.org/xml/JobSchedulingData http://www.quartz-scheduler.org/xml/job_scheduling_data_1_8.xsd"
version="1.8">
<schedule>
<job>
<name>MyScheduledJob</name>
<group>MyScheduledGroup</group>
<description>Job to update XML </description>
<job-class>com.xxxx.xxxx.xxxx.MyScheduledJob </job-class>
</job>
<trigger>
<cron>
<name>midNightTrigger</name>
<job-name>MyScheduledJob</job-name>
<job-group>MyScheduledGroup</job-group>
<!-- It will run every night at 3:30 am -->
<cron-expression>0 30 3 * * ?</cron-expression>
</cron>
</trigger>
</schedule>
</job-scheduling-data>
All the above is for Quartz 'a latest version 2.1 . You can check out the sample codes and tutorials from Quartz for more info.
If you actually want to invoke a JSF action from a scheduled job, the job's execute() method will need to include code that makes an HTTP request to the JSF action. You will likely want to use a code library such as Apache HttpClient or HTTP Unit, if java's URLConnection class does not easily fit your needs.
I am starter in mutithreading. I am trying to index my data into solr.For that I was writing the following code
I am getting null pointer exception in the line highlighted
You need to add the following:
<context:annotation-config/>
You need to set the path for autowiring package scan and in your case it will be:
<context:component-scan base-package="a.b.c" />
After it you need to mark the class as candidate for autowiring:
#Component("indexTask")
#Scope("prototype")
IndexTask implements Callable<IndexObject>
{
//ommited
}
Next you can remove indexTask bean configuration from xml file. your package will be created automatically.
Hope it helps.
Autowiring doesn't happen automatically, you need to configure it. See the Spring docs for detail, but essentially you need to add
<context:annotation-config/>