Java

Quick log4j set up

<!– pom.xml –> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>1.7.7</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> <version>1.7.7</version> </dependency> <!– src/main/resources/log4j.xml –> <?xml version=”1.0″ encoding=”UTF-8″?> <!DOCTYPE log4j:configuration SYSTEM “log4j.dtd”> <log4j:configuration xmlns:log4j=”http://jakarta.apache.org/log4j/”> <appender name=”my-file-appender” class=”org.apache.log4j.FileAppender”> <param name=”file” value=”some.file” /> <param name=”append” value=”true” /> <param name=”encoding” value=”UTF-8″ /> <layout class=”org.apache.log4j.PatternLayout”> <param name=”ConversionPattern” value=”%d %t %-5p %c – %m%n” /> </layout> </appender> <appender name=”console” …

Quick log4j set up Read More »

Dynamic file name in log4j’s file appender

In your log4j.xml, set the file name as a variable <appender name=”my-file-appender” class=”org.apache.log4j.FileAppender”> <param name=”file” value=”${myFilePath}” /> <!– … –> </appender> Then in your java code, make sure the following is called before the first getLogger() call System.setProperty(“myFilePath”, someFilePath);

How to prevent Quartz from re-firing a job while it is still running

Answer: use @DisallowConcurrentExecution Example Without this annotation import java.time.LocalTime; import org.quartz.DisallowConcurrentExecution; import org.quartz.Job; import org.quartz.JobExecutionContext; import org.quartz.JobExecutionException; public class HelloJob implements Job { @Override public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException { System.out.println(Thread.currentThread().getName() + “:Job started at ” + LocalTime.now()); try { Thread.sleep(10 * 1000); } catch (InterruptedException e) { throw new RuntimeException(e); } System.out.println(Thread.currentThread().getName() + …

How to prevent Quartz from re-firing a job while it is still running Read More »

Quartz concepts

Basic concepts Job: What to do Trigger: When to do it Scheduler: The director to associate triggers to jobs and to invoke the trigger A job can be associated with several triggers, but a trigger should not be used to trigger more than one job Go into details for some depth JobKey = Job Name …

Quartz concepts Read More »

Forwarded servlet request won’t be intercepted by filters

The new request created by RequestDispatcher.forward(request, response) will not be intercepted by its matching filters defined in web.xml To make a filter work for requests created by dispatcher, you have to add 2 dispatcher configurations to your filter mapping someFilter /* REQUEST FORWARD

There are two Application Contexts in Spring MVC apps

The root context, normally seen as biz-layer context, is normally defined as "applicationContext.xml" and loaded by ContextLoaderListener.  The other context, the web-layer one is normally defined as "mvc-servlet.xml" and loaded by DispatcherServlet. The web-layer context  will inherit beans from the root one, Except  some processors such as PropertyConfigurator. As a result, you should define two …

There are two Application Contexts in Spring MVC apps Read More »

Wildfly wierd feature in 8.2.0 — automatic redirect if your path matches a directory under your webapp

1. If there is a directory "abc" under your webapp root directory and there is no index.jsp under this "abc" directory 2. and you request http://xxx/abc regardless of GET or POST method 3. then wildfly will redirect you to: 302, GET http://xxx/abc/ The code is on github String remaining = match.getRemaining() == null ? match.getMatched() …

Wildfly wierd feature in 8.2.0 — automatic redirect if your path matches a directory under your webapp Read More »

Spring + TestNG + @Spy Example

@ContextConfiguration({“/spring/applicationContext.xml”}) public class SprintTestngWithSpyAnnotationITCase extends AbstractTestNGSpringContextTests { //this bean will take mocked objects injected into it, but it itself is not going to be mocked @InjectMocks @Resource SomeService someService; //it equals to one that is created via spy(applicationContext.getBean(SomeDAO.class)) and it will be // injected to “someService” above @Spy @Resource SomeDAO someDAO; @BeforeMethod public void beforeMethod() …

Spring + TestNG + @Spy Example Read More »