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
|
public class Test implements javax.servlet.ServletContextListener{
ImageStorage storage;
private final ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);
ByteArrayOutputStream transfer(File file) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
try {
InputStream targetStream = new FileInputStream(file);
FileUtils.transfer(targetStream, baos);
} catch (IOException e) {
throw new RuntimeException(e);
}
return baos;
}
public void upload() throws NamingException {
Image image;
File path = new File("...");
for (File file : path.listFiles()) {
ByteArrayOutputStream baos = transfer(file);
byte[] content = baos.toByteArray();
DicomBean ejbDicom = (DicomBean) new InitialContext().lookup("java:comp/env/dicomBean");
ImageCache ejbCache = (ImageCache) new InitialContext().lookup("java:comp/env/imageCache");
Decoder ejbDecoder = (Decoder) new InitialContext().lookup("java:comp/env/decoder");
image = ejbDicom.writeImage(file.getName(), content);
storage.store(image.getSopInstanceUID(), content);
ejbCache.put(image.getSopInstanceUID(), content);
ejbDecoder.decode(image.getSopInstanceUID());
System.out.println("image uploaded");
}
}
public void beepForAnHour() {
final Runnable beeper = new Runnable() {
public void run() {
try {
upload();
} catch (NamingException ex) {
}
}
};
final ScheduledFuture<?> beeperHandle
= scheduler.scheduleAtFixedRate(beeper, 0, 5, MINUTES);
}
public void contextInitialized(ServletContextEvent sce) {
Test t = new Test();
t.beepForAnHour();
}
public void contextDestroyed(ServletContextEvent sce) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
} |
Partager