Java Servlets and ImageIO Error

I am trying to write a WebApplication that examines some DICOM files. My idea was to convert DICOM files to JPEG on the fly, invoking my servlet. I am using dcm4che 2.0.27 to convert files.

Servlet is called as

<img src="pathToServlet/PathToDICOMFile">. 

Problem: when I have a lot of tags (of course, accessing various DICOM files), sometimes I get an exception like java.util.ConcurrentModificationException

Here is the method my servlet calls:

 void convertFile(String path, OutputStream to) throws IOException { File myDicomFile = new File(path); BufferedImage myJpegImage = null; ImageIO.scanForPlugins(); Iterator<ImageReader> iter = ImageIO.getImageReadersByFormatName("DICOM"); ImageReader reader = (ImageReader) iter.next(); DicomImageReadParam param = (DicomImageReadParam) reader.getDefaultReadParam(); ImageInputStream iis = ImageIO.createImageInputStream(myDicomFile); reader.setInput(iis, false); myJpegImage = reader.read(0, param); iis.close(); ImageIO.write(myJpegImage, "JPEG", to); to.close(); } 

"path" is the absolute path to the dicom file, and "out" is only response.getOutputStream ().

An exception can be selected in 3 places:

  • ImageIO.getImageReadersByFormatName ("DICOM");
  • myJpegImage = reader.read (0, param);
  • ImageIO.write (myJpegImage, "JPEG", to);

Here is the stack trace where it was selected when calling ImageIO.getImageReadersByFormatName ("DICOM"):

 java.util.ConcurrentModificationException at java.util.HashMap$HashIterator.nextEntry(HashMap.java:806) at java.util.HashMap$ValueIterator.next(HashMap.java:835) at javax.imageio.spi.PartialOrderIterator.<init>(PartiallyOrderedSet.java:177) at javax.imageio.spi.PartiallyOrderedSet.iterator(PartiallyOrderedSet.java:85) at javax.imageio.spi.SubRegistry.getServiceProviders(ServiceRegistry.java:759) at javax.imageio.spi.ServiceRegistry.getServiceProviders(ServiceRegistry.java:451) at javax.imageio.spi.ServiceRegistry.getServiceProviders(ServiceRegistry.java:507) at javax.imageio.ImageIO.getImageReadersByFormatName(ImageIO.java:708) at example.project.dicomtest.myDicomConverter.ConvertHelper.convertFile(ConvertHelper.java:32) at example.project.dicomtest.myDicomConverter.GetImage.doGet(GetImage.java:40) at javax.servlet.http.HttpServlet.service(HttpServlet.java:621) at javax.servlet.http.HttpServlet.service(HttpServlet.java:722) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:304) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210) at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:224) at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:185) at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472) at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:151) at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100) at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:929) at org.apache.catalina.core.StandardEnginateValve.invoke(StandardEngineValve.java:118) at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:405) at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:269) at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:515) at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:302) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603) at java.lang.Thread.run(Thread.java:722) 

Is this because ImageIO is not thread safe? What can I do to solve this problem? Thank you so much for any help and best wishes!

+4
source share
2 answers

These two lines:

 ImageIO.scanForPlugins(); Iterator<ImageReader> iter = ImageIO.getImageReadersByFormatName("DICOM"); 

You only need to do it once when your application loads or your servlet is initialized. In particular, the first line of ImageIO.scanForPlugins() mutates the common data used inside the ImageIO class. This is probably the reason for your simultaneous changes. Try moving these two lines to either the servlet filter or the init(ServletConfig) method of your servlet.

+6
source

using synchronous methods to prevent simultaneous modifications can help. read this question

stackoverflow.com/questions/9884148/use-of-synchronized-method-in-affable- bean -shopping cart

+1
source

Source: https://habr.com/ru/post/1469501/


All Articles