Hi,
I have a problem with code performing a conversion from JPEG to PDF, when executing from multiple threads. The execution below takes ~ 6 sec to complete with 1 thread, but if the number of threads are increased to 10 it tasks ~ 122 sec. It seems that some internal resource prevents executing Pdf.save from multiple independent threads. Could we find out the reason of this behavior and if there are some solutions to it.
I would like to avoid synchronizing the calls
Using aspose.pdf-10.8.0.jar.
package test;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import org.apache.log4j.Logger;
import aspose.pdf.ImageFileType;
import aspose.pdf.MarginInfo;
import aspose.pdf.Pdf;
import aspose.pdf.Section;
public class JPEGConversionIssue {
static private Logger logger = Logger.getLogger(JPEGConversionIssue.class);
public static void main(String[] args) throws Exception {
long time = System.currentTimeMillis();
ExecutorService executor = Executors.newFixedThreadPool(10); //try running in 1 thread
for (int i = 0; i < 15; ++i) {
executor.execute(new ThreadRunner());
}
executor.shutdown();
executor.awaitTermination(Integer.MAX_VALUE, TimeUnit.SECONDS);
System.out.println(“transformation took: " + (System.currentTimeMillis() - time) + " ms”);
}
public static class ThreadRunner implements Runnable {
@Override
public void run() {
try {
logger.info(“convertion.start”);
FileInputStream fileInputStream = new FileInputStream(“C:/Users/Denis/Desktop/image.jpeg”);
byte data[] = new byte[fileInputStream.available()];
fileInputStream.read(data);
fileInputStream.close();
ByteArrayInputStream inputStream = new ByteArrayInputStream(data);
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
Pdf pdf = new Pdf();
Section section = pdf.getSections().add();
MarginInfo marginInfo = new MarginInfo();
marginInfo.setTop(5);
marginInfo.setRight(5);
marginInfo.setBottom(5);
marginInfo.setLeft(5);
section.getPageInfo().setMargin(marginInfo);
aspose.pdf.Image image = new aspose.pdf.Image(section);
section.getParagraphs().add(image);
int imageFileType = -1;
imageFileType = ImageFileType.Jpeg;
image.getImageInfo().setImageFileType(imageFileType);
image.getImageInfo().setImageStream(inputStream);
pdf.save(outputStream);
FileOutputStream out = new FileOutputStream(“out/” + Thread.currentThread().getName() + “.” + System.currentTimeMillis() + “.pdf”);
out.write(outputStream.toByteArray());
out.close();
logger.info(“convertion.done”);
} catch (Exception e) {
e.printStackTrace();
logger.error(e);
}
}
}
}
Hi Denis,
Hi Denis,
…<o:p></o:p>
Document pdf = new Document();
com.aspose.pdf.Page section = pdf.getPages().add();
com.aspose.pdf.MarginInfo marginInfo = new com.aspose.pdf.MarginInfo();
marginInfo.setTop(5);
marginInfo.setRight(5);
marginInfo.setBottom(5);
marginInfo.setLeft(5);
section.getPageInfo().setMargin(marginInfo);
com.aspose.pdf.Image image = new com.aspose.pdf.Image();
section.getParagraphs().add(image);
//int imageFileType = -1;
//imageFileType = ImageFileType.Jpeg;
//image.setFileType(imageFileType);
image.setImageStream(inputStream);
pdf.save(outputStream);
....
Please feel free to contact us for any further assistance.
Best Regards,
Hi,
Thank you for quick reply. I’ve tried the change suggested,
for small JPEG the problem is not noticeable, however the problem still
persists for the image being attached (scan, 200 dpi).
In case the issues cannot be reproduces please give a try for sample code attached: test-nolib.zip, the file ‘convert-test_lib/aspose.pdf-11.1.0.jar.add’ should be replaced with jar file (to save space).
run from cmd: java -jar convert-test.jar 1
where: 1 - is the thread number, try also for 10 threads
Hi Denis,
Hi,
sorry for my mistake, please find attached the new binary files with updated source (aspose-nolib.zip). It looks that we still have the same problems. I have attached also the console output for running the sample with 3 threads (attachment: output.txt)
Please let me know if I’m missing something
Thank you,
Denis
Don’t know if that is important, using java:
C:\Users\Denis\Desktop\aspose>java -version
java version "1.8.0_71"
Java™ SE Runtime Environment (build 1.8.0_71-b15)
Java HotSpot™ Client VM (build 25.71-b15, mixed mode, sharing)
Hi Denis,
Hi, I would like to ask if there is any update for the issue?
Denis
Hi Denis,