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 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194
| import java.util.Timer;
import java.util.TimerTask;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
public class SampleService
{
public static int x = 1;
public static int y = 2000;
public static String e = "nom";
public static int z = 500;
public static String t = "01/05/2013";
public static String o = "Null";
private Timer timer;
public static void main(String[] args)
{
System.out.println("SampleService application main entry point invoked...");
if (args.length == 0)
{
System.out.println("Doing nothing, no command-line parameter(s) specified");
}
else if ("start".equalsIgnoreCase(args[0]))
{
System.out.println("Start parameter specified");
serviceStart(args);
}
else if ("stop".equalsIgnoreCase(args[0]))
{
System.out.println("Stop parameter specified");
serviceStop(args);
}
else
{
System.out.println("Command-line parameter '" + args[0] + "' not recognised, doing nothing");
}
}
public static void outputJvmDetails()
{
System.out.println("JVM " +
System.getProperty("java.vm.vendor", "{vm.vendor}")
+ " " +
System.getProperty("java.vm.name", "{vm.name}")
+ " " +
System.getProperty("java.vm.version", "{vm version}"));
}
public static void outputHeapDetails()
{
Runtime rt = Runtime.getRuntime();
final long totalBytes = rt.totalMemory();
final long freeBytes = rt.freeMemory();
rt = null;
System.out.println("Heap Size " +
kBytes(totalBytes)
+ " total, free "
+ kBytes(freeBytes)
+ " (used "
+ kBytes(totalBytes - freeBytes)
+ ")");
}
private static String kBytes(long bytes)
{
if (bytes > 1024)
{
bytes -= (bytes % 1024); // rounding to get whole KB figures
}
final long kb = (long) bytes / 1024;
return Long.toString(kb) + "KB";
}
public static void serviceStart(String[] args)
{
System.out.println("Service start function invoked...");
outputJvmDetails();
outputHeapDetails();
getServiceInstance().execute(args);
}
public static void serviceStop(String[] args)
{
System.out.println("Service stop function invoked...");
getServiceInstance().abort();
outputHeapDetails();
}
private static SampleService serviceInstance = null;
private static synchronized SampleService getServiceInstance()
{
if (serviceInstance == null)
{
serviceInstance = new SampleService();
}
return serviceInstance;
}
private boolean serviceExecuting = false;
/**
* Default constructor, for the single instance that ever exists.
*/
private SampleService()
{
setServiceExecuting(false);
}
/**
* Thread-safe update to the service control flag, sets or clears it.
*
* @param executingNow value to be stored in the flag
*/
private synchronized void setServiceExecuting(boolean executingNow)
{
serviceExecuting = executingNow;
}
private synchronized boolean isServiceExecuting()
{
return serviceExecuting;
}
private void execute(String[] args)
{
final boolean useMemory = (args.length > 1);
System.out.println("Starting service execution");
if (useMemory)
{
System.out.println("(Execution loop will exercise memory heap)");
}
setServiceExecuting(true);
// while (isServiceExecuting())
// {
timer = new Timer();
timer.schedule(new SampleService.Tache(),0,60*60*5);
// }
System.out.println("Ended service execution");
}
class Tache extends TimerTask {
public void run()
{
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection connection = DriverManager.getConnection("jdbc:odbc:database");
try (Statement st = connection.createStatement()) {
// for(int i =0; i < 1; i++) {
st.executeUpdate("INSERT INTO tablegmao(ID,codepiece,quantite,demandeur,datecommande,datecheance) values (" +x+ "," +y+ "," +z+ ",'" +e+ "','" +t+ "','"+o+"')");
x++;
y+=10;
// }
}
connection.close();
} catch(ClassNotFoundException exp) {
System.err.println(exp);
}
catch(SQLException exp) {
System.out.println(exp);
}
}
}
private void abort()
{
System.out.println("Aborting service execution");
setServiceExecuting(false);
}
} |
Partager