JSF 2 + Quartz scheduling library - jsf

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.

Related

Spring Session with Hazelcast 4

I'm trying to upgrade to Hazelcast 4.0 in our Spring Boot 2.2.1 application.
We use the #EnableHazelcastHttpSession annotation, which pulls in HazelcastHttpSessionConfiguration, which pulls in HazelcastIndexedSessionRepository from the spring-session-hazelcast jar.
However, this class won't compile because it imports Hazelcast's IMap which has moved to a different package in Hz 4.0.
Is there any way to fix this so that Spring Session works with Hazelcast 4?
I just copied the HazelcastIndexedSessionRepository into my own source code, changed the import from com.hazelcast.core.IMap to com.hazelcast.map.IMap, and swapped the sessionListenerId from String to UUID. If I keep it in the same package, then it loads my class instead of the one in the jar, and everything compiles and works fine.
Edit: We no longer get the SessionExpiredEvent, so something's not quite right, but manual testing shows us that our sessions do time out and force the user to log in again, even across multiple servers.
I found the cause of the error, you must look that the session repository is created by HazelcastHttpSessionConfiguration, in the class it checks wich version of hazelcast is in the classpath, when hazelcast4 is true then it instantiates Hazelcast4IndexedSessionRepository that doesn't use 'com.hazelcast.core.IMap' and you don't get the class not found exception.
Code of class HazelcastHttpSessionConfiguration
#Bean
public FindByIndexNameSessionRepository<?> sessionRepository() {
return (FindByIndexNameSessionRepository)(hazelcast4 ? this.createHazelcast4IndexedSessionRepository() : this.createHazelcastIndexedSessionRepository());
}
Remove the usage of HazelcastIndexedSessionRepository replace it with Hazelcast4IndexedSessionRepository or remove the code and let spring autoconfiguration do the job by HazelcastHttpSessionConfiguration

How to override/turnoff scheduled tasks in test of Spring context [duplicate]

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).

Mule Java Component and Thread Safe

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.

How do I know at runtime which implementing classes are set for my Spring beans?

In hybris, is there an easy way to know which implementing class is being used for a certain Spring bean?
I mean, I can override a Bean by doing something like this:
<alias name="myCheckoutFacade" alias="checkoutFacade"/>
<bean id="myCheckoutFacade" class="com.pedra.facades.checkout.impl.MyCheckoutFacadeImpl" scope="tenant" parent="defaultCheckoutFacade">
<property name="commerceCheckoutService" ref="myCommerceCheckoutService"/>
</bean>
... so now when Spring needs to create a bean with the alias checkoutFacade the implementing class will be MyCheckoutFacadeImpl as opposed to the overridden defaultCheckoutFacade which was defined in some other xml configuration file.
So is there a way to know at runtime which implementing class is being used for a certain Spring bean definition? Without having to debug the code, I mean.
Beanshell or Groovy :-)
Checking the implementing class of a bean is just one of the many cool things you can do at runtime with Beanshell or Groovy.
Disclaimer: Be careful running Beanshell or Groovy code on a production machine!
Log in to the HAC and go to Console > Beanshell or Groovy
Execute the following code in either Beanshell or Groovy to get your implementing class:
de.hybris.platform.core.Registry.getApplicationContext().getBean("checkoutFacade");
Both consoles will show the result of the last expression in the Result tab.
In the Groovy console for Hybris 5.x, simple execute the following:
checkoutFacade
As you can see, each bean is automatically def-ed into each Groovy script.
As for Beanshell, you could create a bean function in Beanshell:
import de.hybris.platform.core.Registry;
import de.hybris.platform.commercefacades.order.CheckoutFacade;
Object bean(String beanName)
{
return Registry.getApplicationContext().getBean(beanName);
}
CheckoutFacade checkoutFacade = (CheckoutFacade) bean("checkoutFacade");
print(checkoutFacade);
I ended up using Beanshell so much that I created my own wrapper application that allows me to develop Beanshell in Eclipse, and use Eclipse as the Beanshell console. But that's a whole other post!
Resources:
Beanshell User Manual
Beanshell Commands Documentation (Built-in functions like print())

Grails Spring Security Configuration thru xml

i am new at grails spring security, and i wanted to know if it is possible to enable/disable security of in my app thru an xml entry.
rather than having
grails.plugins.springsecurity.active = true
I d like to read the value from a bean that i declared in my resources.xml file.
grails.plugins.springsecurity.active = com.myorg.util.BeanUtil.getBean("repositorySettings").getIsSecured()
Using this approach throws errors. is there a way that i can accomplish this, and read the true/false value from a bean in the resources.xml
It's somewhat possible. resources.xml and resources.groovy are loaded after the plugin is, but parsed before. So you can put code in resources.groovy that will run while it's being parsed, before the plugin loads the config, and determines whether it's enabled. But you can't use a bean for that since it wouldn't be ready until it's too late. This wouldn't be possible in resources.xml since that's just XML but resources.groovy allows Groovy code and bean definitions:
import com.pbbi.mimgr.util.BeanUtil
beans = {
def grailsApplication = springConfig.unrefreshedApplicationContext.grailsApplication
def securityConfig = grailsApplication.config.grails.plugins.springsecurity
securityConfig.active = BeanUtil.getBean('repositorySettings').isSecured
}
If BeanUtil accesses the ApplicationContext then this won't work. But if it's accessing singletons that aren't Spring beans it should be fine.

Resources