1 package org.apache.commons.jcs.auxiliary.disk.jdbc.mysql;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.sql.SQLException;
23 import java.text.ParseException;
24 import java.util.Date;
25 import java.util.concurrent.TimeUnit;
26
27 import javax.sql.DataSource;
28
29 import org.apache.commons.jcs.auxiliary.AuxiliaryCacheAttributes;
30 import org.apache.commons.jcs.auxiliary.disk.jdbc.JDBCDiskCacheFactory;
31 import org.apache.commons.jcs.auxiliary.disk.jdbc.TableState;
32 import org.apache.commons.jcs.auxiliary.disk.jdbc.dsfactory.DataSourceFactory;
33 import org.apache.commons.jcs.auxiliary.disk.jdbc.mysql.util.ScheduleParser;
34 import org.apache.commons.jcs.engine.behavior.ICompositeCacheManager;
35 import org.apache.commons.jcs.engine.behavior.IElementSerializer;
36 import org.apache.commons.jcs.engine.logging.behavior.ICacheEventLogger;
37 import org.apache.commons.logging.Log;
38 import org.apache.commons.logging.LogFactory;
39
40
41
42
43
44
45 public class MySQLDiskCacheFactory
46 extends JDBCDiskCacheFactory
47 {
48
49 private static final Log log = LogFactory.getLog( MySQLDiskCacheFactory.class );
50
51
52
53
54
55
56
57
58
59
60
61 @Override
62 public <K, V> MySQLDiskCache<K, V> createCache( AuxiliaryCacheAttributes rawAttr,
63 ICompositeCacheManager compositeCacheManager,
64 ICacheEventLogger cacheEventLogger, IElementSerializer elementSerializer )
65 throws SQLException
66 {
67 MySQLDiskCacheAttributes cattr = (MySQLDiskCacheAttributes) rawAttr;
68 TableState tableState = getTableState( cattr.getTableName() );
69 DataSourceFactory dsFactory = getDataSourceFactory(cattr, compositeCacheManager.getConfigurationProperties());
70
71 MySQLDiskCache<K, V> cache = new MySQLDiskCache<K, V>( cattr, dsFactory, tableState, compositeCacheManager );
72 cache.setCacheEventLogger( cacheEventLogger );
73 cache.setElementSerializer( elementSerializer );
74
75
76 createShrinkerWhenNeeded( cattr, cache );
77 scheduleOptimizations( cattr, tableState, cache.getDataSource() );
78
79 return cache;
80
81 }
82
83
84
85
86
87
88
89
90 protected void scheduleOptimizations( MySQLDiskCacheAttributes attributes, TableState tableState, DataSource ds )
91 {
92 if ( attributes != null )
93 {
94 if ( attributes.getOptimizationSchedule() != null )
95 {
96 if ( log.isInfoEnabled() )
97 {
98 log.info( "Will try to configure optimization for table [" + attributes.getTableName()
99 + "] on schedule [" + attributes.getOptimizationSchedule() + "]" );
100 }
101
102 MySQLTableOptimizer optimizer = new MySQLTableOptimizer( attributes, tableState, ds );
103
104
105 try
106 {
107 Date[] dates = ScheduleParser.createDatesForSchedule( attributes.getOptimizationSchedule() );
108 if ( dates != null )
109 {
110 for ( int i = 0; i < dates.length; i++ )
111 {
112 this.scheduleOptimization( dates[i], optimizer );
113 }
114 }
115 }
116 catch ( ParseException e )
117 {
118 log.warn( "Problem creating optimization schedule for table [" + attributes.getTableName() + "]", e );
119 }
120 }
121 else
122 {
123 if ( log.isInfoEnabled() )
124 {
125 log.info( "Optimization is not configured for table [" + attributes.getTableName() + "]" );
126 }
127 }
128 }
129 }
130
131
132
133
134
135
136
137 protected void scheduleOptimization( Date startTime, MySQLTableOptimizer optimizer )
138 {
139 if ( log.isInfoEnabled() )
140 {
141 log.info( "startTime [" + startTime + "] for optimizer " + optimizer );
142 }
143
144
145 OptimizerTask runnable = new OptimizerTask( optimizer );
146 Date now = new Date();
147 long initialDelay = startTime.getTime() - now.getTime();
148
149
150 getScheduledExecutorService().scheduleAtFixedRate(runnable, initialDelay, 86400000L, TimeUnit.MILLISECONDS );
151 }
152
153
154
155
156
157
158 private static class OptimizerTask
159 implements Runnable
160 {
161
162 private MySQLTableOptimizer optimizer = null;
163
164
165
166
167
168
169 public OptimizerTask( MySQLTableOptimizer optimizer )
170 {
171 this.optimizer = optimizer;
172 }
173
174
175
176
177
178
179 @Override
180 public void run()
181 {
182 if ( optimizer != null )
183 {
184 boolean success = optimizer.optimizeTable();
185 if ( log.isInfoEnabled() )
186 {
187 log.info( "Optimization success status [" + success + "]" );
188 }
189 }
190 else
191 {
192 log.warn( "OptimizerRunner: The optimizer is null. Could not optimize table." );
193 }
194 }
195 }
196 }