summaryrefslogtreecommitdiffstats
path: root/Master/Reference Architectures and Patterns/EJB 3.0 Code/Rima Examples/src/examples/timer/CleanDayLimitOrdersBean.java
blob: 1f90550ae945f3b958d4a4bdff158228abbb77e4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package examples.timer;

import javax.ejb.*;
import javax.annotation.Resource;

import java.util.Calendar;
import java.util.TimeZone;
import java.util.SimpleTimeZone;
import java.util.GregorianCalendar;
import java.util.Date;

@Stateless
public class CleanDayLimitOrdersBean implements CleanDayLimitOrders {
    
    @Resource private SessionContext ctx;
            
    public void cleanPeriodicallyDayLimitOrders()
    {
        // Get hold of the eastern time zone assuming that the securities are being 
        // traded on NYSE and NASDAQ exchanges.
        String[] timezoneIDs = TimeZone.getAvailableIDs (-5 * 60 * 60 * 1000);

	SimpleTimeZone est = new SimpleTimeZone (-5 * 60 * 60 * 1000, timezoneIDs[0]);

        // Provide the rules for start and end days of daylight savings time.
	est.setStartRule (Calendar.APRIL, 1, Calendar.SUNDAY, 2 * 60 * 60 * 1000);
	est.setEndRule (Calendar.OCTOBER, -1, Calendar.SUNDAY, 2 * 60 * 60 * 1000);

        // Get hold of a calendar instance for the eastern time zone.
	Calendar cal = new GregorianCalendar(est);

        // Set the calendar to the current time.
	cal.setTime (new Date ());
	
        // Calculate the difference between now and market close i.e. 4 PM Eastern.
	int hourofday = cal.get (cal.HOUR_OF_DAY);
	int minuteofhour = cal.get (cal.MINUTE);

        // If this method is invoked after the market close, then set the timer expiration
        // immediately i.e. start=0. Otherwise, calculate the milliseconds that needs
        // to elapse until first timer expiration.
	long start = 0;
	if (hourofday < 16)
	{
            int hourdiff = 16 - hourofday - 1;
            int mindiff = 60 - minuteofhour;
			
            start = (hourdiff * 60 * 60 * 1000) + (mindiff * 60 * 1000);
        }

        // Finally, get hold of the timer service instance from EJBContext object and create the 
        // recurrent expiration timer.
        TimerService timerService = ctx.getTimerService();
        Timer timer = timerService.createTimer(start, 86400000, null);
	
        System.out.println("CleanDayLimitOrdersBean: Timer created to first expire after " + start + " milliseconds.");
    }
    
    @Timeout
    public void handleTimeout(Timer timer)
    {
        System.out.println("CleanDayLimitOrdersBean: handleTimeout called.");

        // Put here the code for cleaning the database of day limit orders that have
        // not been executed.
     }
}