Multithreaded JPEG to PDF conversion

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,


Thanks for your inquiry. I am looking into the issue and will get back to you soon.

Best Regards,

Hi Denis,


Thanks for your patience. I have tested your sample code with new DOM approach for Image to PDF conversion and Aspose.Pdf for Java 11.1.0 and unable to noticed the processing lag with 10 threads. Please download latest version of Aspose.Pdf for Java and use following code, it will resolve the issue.

…<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,


Thanks for your feedback. I have tested the scenario using new DOM approch(com.aspose.pdf) with the shared JPG image and unable to notice the performance issue. However I have checked your shared solution and noticed it is still using old generator(aspose.pdf). As suggested above, please use new generator(com.aspose.pdf). It is more efficient and improved, it will help you to improve the situation. Please try above shared code and share the results.

We are sorry for the inconvenience caused.

Best Regards,

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,


Thanks for your feedback. I have again tested the scenario both with my sample test project and your share Jar with 3 threads. My project is taking approx. 14 seconds but your shared jar is taking 82 seconds for conversion. So I have logged a ticket PDFNEWJAVA-35525 in our issue tracking system for further investigation. We will keep you updated about the issue resolution progress.

We are sorry for the inconveince caused.

Best Regards,

Hi, I would like to ask if there is any update for the issue?

Denis

Hi Denis,


Thanks for your patience.

We have further investigated the issue reported earlier and as per our observations, the problem was found only in i586 version of Java, and the version for x64 works fine. Even when using 10 threads, it is faster than single thread. I have tested several versions of JDK 1.8.0 from earlier versions to the most recent - the situation is same. It appears to be a problem of 32 bit architecture version of Java Virtual Machine. We advise to use x64 bit version of JDK.