Java process hang in a short time - java

I run my application on a VMWare Linux guest, and it doesn't work well.
I write another simple counter to run on the same guest for testing.
(add counter every 0.5s)
Counter works fine while application stop,
but shortly hanged while appliction is running (marked time and numbers).
10:21:24.386 : 166
10:21:25.154 : 167
10:21:25.971 : 168
10:21:25.971 : 169 <--
10:21:26.957 : 170
10:21:26.957 : 171 <--
10:21:27.848 : 172
10:21:27.885 : 173
10:21:28.776 : 174
10:21:28.884 : 175
10:21:30.374 : 176
10:21:30.374 : 177 <--
10:21:30.379 : 178
10:21:31.264 : 179
10:21:31.380 : 180
10:21:32.155 : 181
10:21:32.933 : 182
10:21:32.933 : 183 <--
counter code shows below, but I don't think theres problem
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss.SSS");
ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);
scheduler.scheduleAtFixedRate(new Runnable(){
int i=0;
#Override
public void run() {
Calendar cal = Calendar.getInstance();
System.out.println(sdf.format(cal.getTime()) + " : " + i);
i++;
}
}, 500, 500, TimeUnit.MILLISECONDS);
Here's the top result while application is running
top - 11:02:49 up 20:49, 5 users, load average: 12.87, 14.66, 13.22
Tasks: 196 total, 5 running, 190 sleeping, 0 stopped, 1 zombie
%Cpu0 : 77.3 us, 20.5 sy, 0.0 ni, 0.0 id, 0.0 wa, 0.0 hi, 2.3 si, 0.0 st
%Cpu1 : 82.4 us, 16.5 sy, 0.0 ni, 0.0 id, 0.0 wa, 0.0 hi, 1.1 si, 0.0 st
KiB Mem : 16267652 total, 7639820 free, 7309136 used, 1318696 buff/cache
KiB Swap: 8257532 total, 8257532 free, 0 used. 8588648 avail Mem
It seems my application running out of resource and making JVM working improperly ?
Except for adding more resource to guest, is there any ways to reduce resource consuming?
Using jmap -histo to find mostly used object and check it ?
Hers's enviroment info
java version "1.8.0_181"
CentOS Linux release 7.5.1804 (Core)
VMware 6.0

Related

Jedis performance degrades with large connection pool size (under high concurrency)

I'am using JMH to test the performance of Jedis under high concurrency (200 concurrent threads). The test is performed on a localhost Redis with a 8-processor CPU. The version of Jedis is 4.1.1 and The Version of Redis is 6.2.6. The test code is shown below:
#State(Scope.Benchmark)
public class CommonClientBenchmark {
private JedisPool jedisPool;
private final String host = "127.0.0.1";
private final int port = 6379;
private final int poolSize = 50
#Setup
public void setup() {
JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
jedisPoolConfig.setMaxTotal(poolSize);
jedisPoolConfig.setMaxIdle(poolSize);
jedisPool = new JedisPool(jedisPoolConfig, host, port, 30000);
}
#TearDown
public void tearDown() {
jedisPool.close();
}
#Threads(200)
#Fork(1)
#Benchmark
#BenchmarkMode(Mode.Throughput)
#Warmup(iterations = 1, time = 30, timeUnit = TimeUnit.SECONDS)
#Measurement(iterations = 2, time = 30, timeUnit = TimeUnit.SECONDS)
public void jedisSet() {
try (Jedis jedis = jedisPool.getResource()) {
jedis.set("jedis", "jedis");
}
}
public static void main(String[] args) throws IOException, RunnerException {
CommonClientBenchmark commonClientBenchmark = new CommonClientBenchmark();
commonClientBenchmark.setup();
org.openjdk.jmh.Main.main(args);
}
}
When I set poolSize to 50, I got a QPS of about 70k. However, When I increase the poolSize to 200 (which matches the concurrent thread number), the QPS dropped heavily to about only 30k. I've also run a redis-benchmark with 200 connections, and I got a QPS of about 80k. To find out the reason of performance loss of Jedis with large pool size under high concurrency, I've also tried using top -o cpu to observer the overall CPU usage and the CPU usage of each process. And I got the stats below:
=> 200 thread, jedis 50 conn, qps about 70000
Load Avg: 6.41, 5.72, 5.21 CPU usage: 18.89% user, 51.0% sys, 30.10% idle
ID COMMAND %CPU TIME #TH #WQ #PORT MEM PURG CMPRS
8082 java 258.1 00:55.99 236/4 1 511 211M- 0B 0B
0 kernel_task 100.9 33:45.69 212/9 0 0 264M 0B 0B
1514 redis-server 86.8 16:31.14 4/1 0 17 20M 0B 4268K
=> 200 thread, jedis pool 200 conn, qps about 30000
Load Avg: 8.94, 12.30, 8.44 CPU usage: 5.75% user, 85.42% sys, 8.81% idle
PID COMMAND %CPU TIME #TH #WQ #PORT MEM PURG CMPRS PGRP
4556 java 599.7 07:06.31 236/15 1 511 202M 0B 0B 4255
0 kernel_task 56.8 12:42.50 212/9 0 0 323M 0B 0B 0
1514 redis-server 29.6 06:10.48 4/1 0 17 20M 0B 2488K 1514
I found that Jedis consumes much more CPU time when the connection pool size augments to 200, and, according to the overall CPU usage data, a big portion (about 85%) of CPU time was spent in kernel mode. I wonder if this means that with large connection pool size and under high concurrency, Jedis will make much more socket syscall (write/read) in parallel, and the resulting frequent context switch finally impact the performance heavily? Many thanks!

Heap Corruption In C When Using DLL With JNA

I am using C language Native API callbacks with DLL files. When we are calling callback first time everything is working fine but on second call I am getting heap corruption error and JVM is getting crashed.
In the native code the memory allocated in first call is being released and then is being used in second call again and during memory allocation in second call JVM is being crashed. But on the same place when in second call new memory pointer is used rather than the one which was used in previous call I am not getting this heap corruption error.
As this callback is called many times I can not keep on allocating new space every time. In below logs I am getting error as INVALID_POINTER_READ.
I am not able to understand what is the reason behind it and how this can be fixed. When same DLL is used with JNA it's working fine.
Java/JNA Code:
Setting Hook:
final PropertyCallBack callback = new PropertyCallBack();
final int setHookStatus = callback.setHook();
private static CALLBACK callback;
public int setHook() {
if (callback != null) {
return 0;
}
synchronized (this) {
if (callback == null) {
callback = new CALLBACK();
return callback.setHook();
}
}
return 0;
}
Callback Method Called From Native:
#Override
public int PropertyHook(final DESTINATION dest, final BACSTAC_READ_INFO.ByReference info) {
final PROPERTY_CONTENTS.ByReference content = new PROPERTY_CONTENTS.ByReference();
final BUFFER.ByReference buffer = new BUFFER.ByReference();
// Memory assign
final int bufferSize = 1048;
buffer.pBuffer = new Memory(bufferSize);
buffer.nBufferSize = bufferSize;
content.tag = "INVALID";
content.buffer = buffer;
content.nElements = 0;
Pointer dev = NativeLibrary.INSTANCE.Call_1();
Pointer obj = null;
if (dev != null) {
obj = NativeLibrary.INSTANCE.call_2(dev, info.objectID);
}
final int readDbStatus = NativeLibrary.INSTANCE.call_3(obj, info.prop, info.index, content, null);
final int responseStatus = NativeLibrary.INSTANCE.call_4(dest, info, content);
return 0;
}
When I analyzed heap dump with windbg I am getting below details:
This dump file has an exception of interest stored in it.
The stored exception information can be accessed via .ecxr.
(6201c.5ef10): Access violation - code c0000005 (first/second chance not available)
For analysis of this file, run !analyze -v
ntdll!NtWaitForMultipleObjects+0x14:
00007ffa`46deb4f4 c3 ret
0:026> !analyze -v
*******************************************************************************
* *
* Exception Analysis *
* *
*******************************************************************************
*** WARNING: Unable to verify checksum for srv.dll
DEBUG_FLR_EXCEPTION_CODE(c0000374) and the ".exr -1" ExceptionCode(c0000005) don't match
KEY_VALUES_STRING: 1
Key : AV.Fault
Value: Read
Key : Timeline.Process.Start.DeltaSec
Value: 46
PROCESSES_ANALYSIS: 1
SERVICE_ANALYSIS: 1
STACKHASH_ANALYSIS: 1
TIMELINE_ANALYSIS: 1
Timeline: !analyze.Start
Name: <blank>
Time: 2019-12-02T11:13:41.439Z
Diff: 3429439 mSec
Timeline: Dump.Current
Name: <blank>
Time: 2019-12-02T10:16:32.0Z
Diff: 0 mSec
Timeline: Process.Start
Name: <blank>
Time: 2019-12-02T10:15:46.0Z
Diff: 46000 mSec
DUMP_CLASS: 2
DUMP_QUALIFIER: 400
CONTEXT: (.ecxr)
rax=0000000000030000 rbx=000000002b200000 rcx=0000000000000303
rdx=0000000000000003 rsi=01fda8c00000ed00 rdi=000000002b223ef0
rip=00007ffa46d6cb7a rsp=000000002b8ff500 rbp=0000000000000008
r8=0000000000000028 r9=0000000000000030 r10=00000000014da2d0
r11=00000000014e2ef0 r12=0000000000000001 r13=0000000000000003
r14=000000002b223ee0 r15=000000000600c1ba
iopl=0 nv up ei pl zr na po nc
cs=0033 ss=002b ds=002b es=002b fs=0053 gs=002b efl=00010246
ntdll!RtlpAllocateHeap+0xdaa:
00007ffa`46d6cb7a 498b07 mov rax,qword ptr [r15] ds:00000000`0600c1ba=????????????????
Resetting default scope
FAULTING_IP:
ntdll!RtlpAllocateHeap+daa
00007ffa`46d6cb7a 498b07 mov rax,qword ptr [r15]
EXCEPTION_RECORD: (.exr -1)
ExceptionAddress: 00007ffa46d6cb7a (ntdll!RtlpAllocateHeap+0x0000000000000daa)
ExceptionCode: c0000005 (Access violation)
ExceptionFlags: 00000000
NumberParameters: 2
Parameter[0]: 0000000000000000
Parameter[1]: 000000000600c1ba
Attempt to read from address 000000000600c1ba
DEFAULT_BUCKET_ID: HEAP_CORRUPTION
PROCESS_NAME: javaw.exe
FOLLOWUP_IP:
ntdll!RtlpAllocateHeap+daa
00007ffa`46d6cb7a 498b07 mov rax,qword ptr [r15]
READ_ADDRESS: 000000000600c1ba
ERROR_CODE: (NTSTATUS) 0xc0000005 - The instruction at 0x%p referenced memory at 0x%p. The memory could not be %s.
EXCEPTION_CODE: (NTSTATUS) 0xc0000374 - A heap has been corrupted.
EXCEPTION_CODE_STR: c0000005
EXCEPTION_PARAMETER1: 0000000000000000
EXCEPTION_PARAMETER2: 000000000600c1ba
WATSON_BKT_PROCSTAMP: 5d1dea24
WATSON_BKT_PROCVER: 8.0.2210.11
PROCESS_VER_PRODUCT: Java(TM) Platform SE 8
WATSON_BKT_MODULE: ntdll.dll
WATSON_BKT_MODSTAMP: 7f828745
WATSON_BKT_MODOFFSET: 1cb7a
WATSON_BKT_MODVER: 10.0.17134.799
MODULE_VER_PRODUCT: Microsoft® Windows® Operating System
BUILD_VERSION_STRING: 17134.1.amd64fre.rs4_release.180410-1804
MODLIST_WITH_TSCHKSUM_HASH: f06ad8a6a7f7267c783c08e3a62df4696020d52f
MODLIST_SHA1_HASH: cdafa8057ac19b1a3608c439ebbfa992407212d6
NTGLOBALFLAG: 0
PROCESS_BAM_CURRENT_THROTTLED: 0
PROCESS_BAM_PREVIOUS_THROTTLED: 0
APPLICATION_VERIFIER_FLAGS: 0
DUMP_FLAGS: 94
DUMP_TYPE: 1
ANALYSIS_SESSION_HOST: MD2E86EC
ANALYSIS_SESSION_TIME: 12-02-2019 16:43:41.0439
ANALYSIS_VERSION: 10.0.18362.1 x86fre
THREAD_ATTRIBUTES:
ADDITIONAL_DEBUG_TEXT: Enable Pageheap/AutoVerifer ; Followup set based on attribute [Is_ChosenCrashFollowupThread] from Frame:[0] on thread:[PSEUDO_THREAD]
FAULTING_THREAD: 0005ef10
THREAD_SHA1_HASH_MOD_FUNC: 5d531e271dfb1ef7af4984c7ee0dd671c07337f5
THREAD_SHA1_HASH_MOD_FUNC_OFFSET: d858fa5fb04738fbbbbb9e4df89e26d53dc74794
OS_LOCALE: ENU
BUGCHECK_STR: APPLICATION_FAULT_INVALID_POINTER_READ_HEAP_CORRUPTION
PRIMARY_PROBLEM_CLASS: APPLICATION_FAULT
PROBLEM_CLASSES:
ID: [0n262]
Type: [HEAP_CORRUPTION]
Class: Primary
Scope: DEFAULT_BUCKET_ID (Failure Bucket ID prefix)
BUCKET_ID
Name: Add
Data: Omit
PID: [0x6201c]
TID: [0x5ef10]
Frame: [0] : ntdll!RtlpAllocateHeap
ID: [0n262]
Type: [HEAP_CORRUPTION]
Class: Primary
Scope: BUCKET_ID
Name: Add
Data: Omit
PID: [0x6201c]
TID: [0x5ef10]
Frame: [0] : ntdll!RtlpAllocateHeap
ID: [0n313]
Type: [#ACCESS_VIOLATION]
Class: Addendum
Scope: BUCKET_ID
Name: Omit
Data: Omit
PID: [Unspecified]
TID: [0x5ef10]
Frame: [0] : ntdll!RtlpAllocateHeap
ID: [0n285]
Type: [INVALID_POINTER_READ]
Class: Primary
Scope: BUCKET_ID
Name: Add
Data: Omit
PID: [Unspecified]
TID: [0x5ef10]
Frame: [0] : ntdll!RtlpAllocateHeap
LAST_CONTROL_TRANSFER: from 00007ffa46d69725 to 00007ffa46d6cb7a
STACK_TEXT:
00000000`00000000 00000000`00000000 heap_corruption!javaw.exe+0x0
THREAD_SHA1_HASH_MOD: ca4e26064d24ef7512d2e94de5a93c38dbe82fe9
SYMBOL_STACK_INDEX: 0
SYMBOL_NAME: heap_corruption!javaw.exe
FOLLOWUP_NAME: MachineOwner
MODULE_NAME: heap_corruption
IMAGE_NAME: heap_corruption
DEBUG_FLR_IMAGE_TIMESTAMP: 0
STACK_COMMAND: ** Pseudo Context ** ManagedPseudo ** Value: a3807e8 ** ; kb
FAILURE_BUCKET_ID: HEAP_CORRUPTION_c0000005_heap_corruption!javaw.exe
BUCKET_ID: APPLICATION_FAULT_INVALID_POINTER_READ_HEAP_CORRUPTION_heap_corruption!javaw.exe
FAILURE_EXCEPTION_CODE: c0000005
FAILURE_IMAGE_NAME: heap_corruption
BUCKET_ID_IMAGE_STR: heap_corruption
FAILURE_MODULE_NAME: heap_corruption
BUCKET_ID_MODULE_STR: heap_corruption
FAILURE_FUNCTION_NAME: javaw.exe
BUCKET_ID_FUNCTION_STR: javaw.exe
BUCKET_ID_OFFSET: 0
BUCKET_ID_MODTIMEDATESTAMP: 0
BUCKET_ID_MODCHECKSUM: 0
BUCKET_ID_MODVER_STR: 0.0.0.0
BUCKET_ID_PREFIX_STR: APPLICATION_FAULT_INVALID_POINTER_READ_
FAILURE_PROBLEM_CLASS: APPLICATION_FAULT
FAILURE_SYMBOL_NAME: heap_corruption!javaw.exe
WATSON_STAGEONE_URL: http://watson.microsoft.com/StageOne/javaw.exe/8.0.2210.11/5d1dea24/ntdll.dll/10.0.17134.799/7f828745/c0000005/0001cb7a.htm?Retriage=1
TARGET_TIME: 2019-12-02T10:16:32.000Z
OSBUILD: 17134
OSSERVICEPACK: 753
SERVICEPACK_NUMBER: 0
OS_REVISION: 0
SUITE_MASK: 256
PRODUCT_TYPE: 1
OSPLATFORM_TYPE: x64
OSNAME: Windows 10
OSEDITION: Windows 10 WinNt SingleUserTS
USER_LCID: 0
OSBUILD_TIMESTAMP: unknown_date
BUILDDATESTAMP_STR: 180410-1804
BUILDLAB_STR: rs4_release
BUILDOSVER_STR: 10.0.17134.1.amd64fre.rs4_release.180410-1804
ANALYSIS_SESSION_ELAPSED_TIME: 307a
ANALYSIS_SOURCE: UM
FAILURE_ID_HASH_STRING: um:heap_corruption_c0000005_heap_corruption!javaw.exe
FAILURE_ID_HASH: {ddc2b378-b1e1-2aec-adc8-f11b7a5773a9}
Any help in fix/debug will be highly appreciated.
I got the solution of to prevent above heap corruption by calling NativeLibrary methods of PropertyHook in another thread. Somehow by calling NativeLibrary methods in different thread heap is not getting corrupted and sub-sequently JVM is not being crashed.

Why does the slow task block the other smaller tasks in this program that uses parallel streams?

Following a question form a colleague about parallel streams I wrote the following code to test something out.
public class Test {
public static void main(String args[]) {
List<Runnable> list = new LinkedList<>();
list.add(() -> {
try {
Thread.sleep(10000);
System.out.println("Time : " + System.nanoTime() + " " + "Slow task");
} catch (InterruptedException e) {
e.printStackTrace();
}
});
for (int i = 0; i < 1000; i++) {
int j = i;
list.add(() -> System.out.println("Time : " + System.nanoTime() + " " + j));
}
list.parallelStream().forEach(r -> r.run());
}
}
Strangely the output is always something like the following.
Time : 4096118049370412 61
Time : 4096118049567530 311
Time : 4096118049480238 217
Time : 4096118049652415 405
Time : 4096118049370678 436
Time : 4096118049370575 155
Time : 4096118049720639 437
Time : 4096118049719368 280
Time : 4096118049804630 281
Time : 4096118049684148 406
Time : 4096118049660398 218
TRUNCATED
Time : 4096118070511768 669
Time : 4096118070675678 670
Time : 4096118070584951 426
Time : 4096118070704143 427
Time : 4096118070714441 428
Time : 4096118070722080 429
Time : 4096118070729569 430
Time : 4096118070736782 431
Time : 4096118070744069 432
Time : 4096118070751286 433
Time : 4096118070758554 434
Time : 4096118070765913 435
Time : 4096118070550370 930
Time : 4096118070800538 931
Time : 4096118070687425 671
Time : 4096118070813669 932
Time : 4096118070827794 672
Time : 4096118070866089 933
Time : 4096118070881358 673
Time : 4096118070895344 934
Time : 4096118070907608 674
Time : 4096118070920712 935
Time : 4096118070932934 675
Time : 4096118070945131 936
Time : 4096118070957850 676
Time : 4096118070982326 677
Time : 4096118070991158 678
Time : 4096118070999002 679
Time : 4096118071006501 680
Time : 4096118071017766 681
Time : 4096118071025766 682
Time : 4096118071033318 683
Time : 4096118071070603 684
Time : 4096118071080240 685
Time : 4096128063025914 Slow task
Time : 4096128063123940 0
Time : 4096128063148135 1
Time : 4096128063173285 2
Time : 4096128063176723 3
Time : 4096128063179939 4
Time : 4096128063183077 5
Time : 4096128063191001 6
Time : 4096128063194156 7
Time : 4096128063197273 8
Time : 4096128063200395 9
Time : 4096128063203581 10
Time : 4096128063206988 11
Time : 4096128063210155 12
Time : 4096128063213285 13
Time : 4096128063216411 14
Time : 4096128063219542 15
Time : 4096128063222733 16
Time : 4096128063232190 17
Time : 4096128063235653 18
Time : 4096128063238827 19
Time : 4096128063241962 20
Time : 4096128063245176 21
Time : 4096128063248296 22
Time : 4096128063251444 23
Time : 4096128063254557 24
Time : 4096128063257705 25
Time : 4096128063261566 26
Time : 4096128063264733 27
Time : 4096128063268115 28
Time : 4096128063272851 29
Process finished with exit code 0
That is, there is always some tasks waiting for the slow task to finish processing, even though all the other tasks have finished. I would assume that the slow task should take only one thread and all the other tasks should finish without any problem and only the slow task should take the full 10 seconds. I have 8 CPUs so the parallelism level is 7.
What could the reason be for this?
To add more information, the code is only for understanding purposes. I am not putting it anywhere in production.
There are some limited capabilities when it comes to work-stealing with streams, so if a single thread has pegged itself for some work in other runners, that work will be blocked until it's finished processing other tasks.
You can visualize this by adding a few more debugging notes around your code...
class Test {
public static void main(String[] args) {
List<Runnable> list = new LinkedList<>();
list.add(() -> {
try {
System.out.println("Long sleep - " + Thread.currentThread().getName());
Thread.sleep(10000);
System.out.println("Time : " + System.nanoTime() + " " + "Slow task");
} catch (InterruptedException e) {
e.printStackTrace();
}
});
for (int i = 0; i < 1000; i++) {
int j = i;
list.add(() -> System.out.println("Time : " + System.nanoTime() + " " + j));
}
list.parallelStream().forEach(r -> {
System.out.println(Thread.currentThread().getName());
r.run();
System.out.println();
});
}
}
Upon running this, I observe the following message come up:
Long sleep - ForkJoinPool.commonPool-worker-4
...and about ten seconds later...
Time : 11525122027429 Slow task
ForkJoinPool.commonPool-worker-4
Time : 11525122204035 0
ForkJoinPool.commonPool-worker-4
Time : 11525122245739 1
ForkJoinPool.commonPool-worker-4
Time : 11525122267015 2
ForkJoinPool.commonPool-worker-4
Time : 11525122286921 3
ForkJoinPool.commonPool-worker-4
Time : 11525122306266 4
ForkJoinPool.commonPool-worker-4
Time : 11525122338787 5
ForkJoinPool.commonPool-worker-4
Time : 11525122357288 6
ForkJoinPool.commonPool-worker-4
Time : 11525122376716 7
ForkJoinPool.commonPool-worker-4
Time : 11525122395218 8
ForkJoinPool.commonPool-worker-4
Time : 11525122414165 9
ForkJoinPool.commonPool-worker-4
Time : 11525122432755 10
ForkJoinPool.commonPool-worker-4
Time : 11525122452805 11
ForkJoinPool.commonPool-worker-4
Time : 11525122472624 12
ForkJoinPool.commonPool-worker-4
Time : 11525122491380 13
ForkJoinPool.commonPool-worker-4
Time : 11525122514417 14
ForkJoinPool.commonPool-worker-4
Time : 11525122534550 15
ForkJoinPool.commonPool-worker-4
Time : 11525122553751 16
So this implies that on my box, worker-4 had some work slated for it which couldn't be stolen based on the fact that there were some uneven chunks. Note: if a thread is processing a task in a chunk, that work isn't going to be broken up any further.
[31, 31, 31, 32, 31, 31, 31, 32, 31, 31, 31, 32, 31, 31, 31, 32, 31, 31, 31, 32, 31, 31, 31, 32, 31, 31, 31, 32, 31, 32, 31, 32, 0]
If you were looking for a threading implementation which could steal work from threads which ran longer, it'd be best to use the work-stealing pool directly.
class Test {
public static void main(String[] args) throws InterruptedException {
List<Runnable> list = new LinkedList<>();
list.add(() -> {
try {
System.out.println("Long sleep - " + Thread.currentThread().getName());
Thread.sleep(10000);
System.out.println("Time : " + System.nanoTime() + " " + "Slow task");
} catch (InterruptedException e) {
e.printStackTrace();
}
});
for (int i = 0; i < 1000; i++) {
int j = i;
list.add(() -> {
System.out.println(Thread.currentThread().getName());
System.out.println("Time : " + System.nanoTime() + " " + j);
System.out.println();
});
}
final ExecutorService stealingPool = Executors.newWorkStealingPool();
list.forEach(stealingPool::execute);
stealingPool.shutdown();
stealingPool.awaitTermination(15, TimeUnit.SECONDS);
}
}
The above prints out a more tenable and more reasonable result at the end of the list:
Time : 12210445469314 Slow task
...which implies that all available work has been processed in the time allotted (15 seconds).

PDFBox on Mac critical error when silent printing

I have been experimenting with bumping my applications dependency on PDFBox to the 2.0.0 snapshot.
I'm having some major issues with it though...
So my code recieves a PDF as a BASE64 String, i decode it, and load the resulting bytearray into a PDDocument. Before I bumped the version number, calling .silentPrint();on the PDDocument worked like a charm.
The implementation of silent printing changed in 2.0.0, and I now do it this way:
private Status doPdfPrint(Document document, PrintService printService) {
ByteArrayInputStream bais = null;
PDDocument doc = null;
PrinterJob printerJob = PrinterJob.getPrinterJob();
try {
printerJob.setPrintService(printService);
bais = new ByteArrayInputStream(document.getDecodedData());
doc = PDDocument.load(bais, true); //Force load
PDFPrinter pdfPrinter = new PDFPrinter(doc);
pdfPrinter.silentPrint(printerJob);
doc.close();
bais.close();
} catch (IOException | PrinterException e) {
log.warn("Failed to print! Exception occurred: {}", e.getMessage());
log.debug("EXCEPTION", e);
return Status.PRINTING_FAILED;
} finally {
IOUtils.closeQuietly(bais);
IOUtils.closeQuietly(doc);
}
return Status.PRINTING_OK;
}
However, when running this on OSX Yosemite, this results in:
2015-02-12 08:10:44.475 java[20264:1353636] Cocoa AWT: Not running on AppKit thread 0 when expected. (
0 libosxapp.dylib 0x0000000125997782 +[ThreadUtilities getJNIEnv] + 38
1 libawt_lwawt.dylib 0x000000012bf3004d syncFromJavaPixels + 1842
2 libawt_lwawt.dylib 0x000000012bf304e3 LockImage + 75
3 libawt_lwawt.dylib 0x000000012bf43040 Java_sun_java2d_CRenderer_doImage + 170
4 ??? 0x0000000108c15694 0x0 + 4441855636
5 ??? 0x0000000108c0798d 0x0 + 4441799053
)
2015-02-12 08:10:44.475 java[20264:1353636] Please file a bug report at http://java.net/jira/browse/MACOSX_PORT with this message and a reproducible test case.
2015-02-12 08:10:44.478 java[20264:1353636] java.lang.StackOverflowError
at sun.java2d.CRenderer.doImage(Native Method)
at sun.java2d.OSXSurfaceData.blitImage(OSXSurfaceData.java:1027)
at sun.java2d.CRenderer.blitImage(CRenderer.java:461)
at sun.java2d.CRenderer.scaleImage(CRenderer.java:455)
at sun.java2d.CRenderer.transformImage(CRenderer.java:508)
at sun.java2d.CRenderer.transformImage(CRenderer.java:582)
at sun.java2d.SunGraphics2D.drawImage(SunGraphics2D.java:3461)
at sun.print.ProxyGraphics2D.drawImage(ProxyGraphics2D.java:1315)
at org.apache.pdfbox.rendering.PageDrawer.drawBufferedImage(PageDrawer.java:755)
at org.apache.pdfbox.rendering.PageDrawer.drawImage(PageDrawer.java:719)
at org.apache.pdfbox.contentstream.operator.graphics.DrawObject.process(DrawObject.java:51)
at org.apache.pdfbox.contentstream.PDFStreamEngine.processOperator(PDFStreamEngine.java:802)
at org.apache.pdfbox.contentstream.PDFStreamEngine.processStreamOperators(PDFStreamEngine.java:464)
at org.apache.pdfbox.contentstream.PDFStreamEngine.processStream(PDFStreamEngine.java:438)
at org.apache.pdfbox.contentstream.PDFStreamEngine.processPage(PDFStreamEngine.java:149)
at org.apache.pdfbox.rendering.PageDrawer.drawPage(PageDrawer.java:160)
at org.apache.pdfbox.rendering.PDFRenderer.renderPage(PDFRenderer.java:203)
at org.apache.pdfbox.rendering.PDFRenderer.renderPageToGraphics(PDFRenderer.java:166)
at org.apache.pdfbox.printing.PDFPrinter$PDFPrintable.print(PDFPrinter.java:430)
at sun.lwawt.macosx.CPrinterJob$4.run(CPrinterJob.java:653)
at sun.lwawt.macosx.CPrinterJob.printToPathGraphics(CPrinterJob.java:667)
at sun.lwawt.macosx.CPrinterJob.printLoop(Native Method)
at sun.lwawt.macosx.CPrinterJob.print(CPrinterJob.java:303)
at sun.print.RasterPrinterJob.print(RasterPrinterJob.java:1323)
at org.apache.pdfbox.printing.PDFPrinter.print(PDFPrinter.java:250)
at org.apache.pdfbox.printing.PDFPrinter.silentPrint(PDFPrinter.java:182)
at com.memnon.printr.PrintHandler.doPdfPrint(PrintHandler.java:123)
at com.memnon.printr.PrintHandler.print(PrintHandler.java:87)
at com.memnon.printr.PrintHandler.print(PrintHandler.java:77)
at com.memnon.printr.PrintHandler.print(PrintHandler.java:48)
at com.memnon.printr.communication.DocumentResponseHandler.handleSuccessful(DocumentResponseHandler.java:78)
at com.memnon.printr.communication.DocumentResponseHandler.handle(DocumentResponseHandler.java:53)
at com.memnon.printr.messaging.PrintConsumer.executePrinterJob(PrintConsumer.java:62)
at com.memnon.printr.messaging.PrintConsumer.run(PrintConsumer.java:44)
at java.lang.Thread.run(Thread.java:745)
2015-02-12 08:10:44.493 java[20264:1353636] NSAlert is being used from a background thread, which is not safe. This is probably going to crash sometimes. Break on void _NSAlertWarnUnsafeBackgroundThreadUsage() to debug. This will be logged only once. This may break in the future.
2015-02-12 08:10:46.639 java[20264:1353636] Apple AWT Internal Exception: Printing failed because PMSessionEndDocumentNoDialog() returned -30871.
2015-02-12 08:10:46.639 java[20264:1353636] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Printing failed because PMSessionEndDocumentNoDialog() returned -30871.'
*** First throw call stack:
(
0 CoreFoundation 0x00007fff8895c66c __exceptionPreprocess + 172
1 libobjc.A.dylib 0x00007fff890e876e objc_exception_throw + 43
2 CoreFoundation 0x00007fff8895c51d +[NSException raise:format:] + 205
3 AppKit 0x00007fff8d117e80 -[NSPrintSpoolingGraphicsContext dealloc] + 43
4 libobjc.A.dylib 0x00007fff890fb89c _ZN11objc_object17sidetable_releaseEb + 236
5 libobjc.A.dylib 0x00007fff890e1e8f _ZN12_GLOBAL__N_119AutoreleasePoolPage3popEPv + 575
6 CoreFoundation 0x00007fff88834302 _CFAutoreleasePoolPop + 50
7 libawt_lwawt.dylib 0x000000012bf25fa4 Java_sun_lwawt_macosx_CPrinterJob_printLoop + 165
8 ??? 0x0000000108c15694 0x0 + 4441855636
9 ??? 0x0000000108c07160 0x0 + 4441796960
10 ??? 0x0000000108c0798d 0x0 + 4441799053
)
libc++abi.dylib: terminating with uncaught exception of type NSException
Any idea what's going on?
Regards
So I'm posting here 2 years after this occured for the first time..
It's still occuring. However, it's a Java bug, rather then a PDFBox bug. So after filing my bugreport on PDFBox issue-tracker, I got the advice to set a DPI for the document when doing printing. And voila it works. However, it's nearly impossible to figure out the DPI of a pdf document.
However, if you know what the DPI is, you can simply do this:
final int rasterizedDpi = 203;
final PDDocument pdfDocument = PDDocument.load(data);
final PrinterJob job = PrinterJob.getPrinterJob();
final PDFPageable pageable = new PDFPageable(pdfDocument, Orientation.AUTO, false, rasterizedDpi);
job.setPageable(pageable);
job.print();
The hint here is the 4th argument to PDFPageable, the dpi...
Until Oracle fixes this JDK bug, i guess we're going to have to call this method :)

Read Excel file into R with XLConnect package from URL

There are lots of good examples out there on how to read Microsoft Excel files into R with the XLConnect package, but I can't find any examples of how to read in an Excel file directly from a URL. The reproducible example below returns a "FileNotFoundException (Java)". But, I know the file exists because I can pull it up directly by pasting the URL into a browser.
fname <- "https://www.misoenergy.org/Library/Repository/Market%20Reports/20140610_sr_nd_is.xls"
sheet <- c("Sheet1")
data <- readWorksheetFromFile(fname, sheet, header=TRUE, startRow=11, startCol=2, endCol=13)
Although, the URL is prefixed with "https:" it is a public file that does not require a username or password.
I have tried to download the file first using download.file(fname, destfile="test.xls") and got a message that says it was downloaded but when I try to open it in Excel to check to see if it was successful i get a Excel popup box that says "..found unreadable content in 'test.xls'.
Below are the specifics of my system:
Computer: 64-bit Dell running
Operating System: Windows 7 Professional
R version: R-3.1.0
Any assistance would be greatly appreciated.
You can use RCurl to download the file:
library(RCurl)
library(XLConnect)
appURL <- "https://www.misoenergy.org/Library/Repository/Market%20Reports/20140610_sr_nd_is.xls"
f = CFILE("exfile.xls", mode="wb")
curlPerform(url = appURL, writedata = f#ref, ssl.verifypeer = FALSE)
close(f)
out <- readWorksheetFromFile(file = "exfile.xls", sheet = "Sheet1", header = TRUE
, startRow = 11, startCol = 2, endCol = 15, endRow = 35)
> head(out)
Col1 EEI Col3 IESO MHEB Col6 PJM SOCO SWPP TVA WAUE Col12 Other Total
1 Hour 1 272 NA 768 1671 NA 148 200 -52 198 280 NA 700 4185
2 Hour 2 272 NA 769 1743 NA 598 200 -29 190 267 NA 706 4716
3 Hour 3 272 NA 769 1752 NA 598 200 -28 194 267 NA 710 4734
4 Hour 4 272 NA 769 1740 NA 598 200 -26 189 266 NA 714 4722
5 Hour 5 272 NA 769 1753 NA 554 200 -27 189 270 NA 713 4693
6 Hour 6 602 NA 769 1682 NA 218 200 -32 223 286 NA 714 4662
Two things:
Try using a different package--I know the gdata package's read.xls function has support for URLs
Try loading in a publicly-available xls file to make sure it's not an issue with the particular website.
For instance, you can try:
library("gdata")
site <- "http://www.econ.yale.edu/~shiller/data/chapt26.xls"
data <- read.xls(site, header=FALSE, skip=8)
head(data)
XLConnect does not support importing directly from URLs. You have to use e.g. download.file first to download the file to your local machine:
require(XLConnect)
tmp = tempfile(fileext = ".xls")
download.file(url = "http://www.econ.yale.edu/~shiller/data/chapt26.xls", destfile = tmp)
readWorksheetFromFile(file = tmp, sheet = "Data", header = FALSE, startRow = 9, endRow = 151)
or with your originally proposed URL:
require(XLConnect)
tmp = tempfile(fileext = ".xls")
download.file(url = "https://www.misoenergy.org/Library/Repository/Market%20Reports/20140610_sr_nd_is.xls", destfile = tmp, method = "curl")
readWorksheetFromFile(file = tmp, sheet = "Sheet1", header = TRUE, startRow = 11, startCol = 2, endCol = 13)
library(relenium)
library(XML)
library(RCurl)
firefox=firefoxClass$new()
url="https://www.misoenergy.org/Library/Repository/Market%20Reports/20140610_sr_nd_is.xls"
url=sprintf(url)
firefox$get(url)
This will open a Firefox instance within R and ask you to download the file, which you could then open in the next line of code. I don't know of any R utilities that will open an excel spreadsheet from HTTPS.
You could then set a delay while you're saving the file and then read the sheet from your downloads folder:
Sys.sleep(10)
sheet <- c("Sheet1")
data <- readWorksheetFromFile(path, sheet, header=TRUE, startRow=11, startCol=2, endCol=13)

Categories

Resources