Log4J: AsyncAppender and NullPointerException during stacktrace print - log4j

Updated:
This is true not only for AsyncAppender. Happens for console one as well.
I faced with wierd behavior of AsyncAppender that occurs rarely but with sufficient harm.
Here is code snippet:
public void testNPE() {
try {
try {
throw new NullPointerException();
} catch (NullPointerException e) {
throw new RuntimeException(e);
}
} catch (RuntimeException e) {
logger.error("Catcha!" + e.getLocalizedMessage(), e);
}
}
Probable result should be:
07.12.12 10:21:34,904 ERROR [main] >> [com.ubs.eqdel.markitfeed.core.RetrieverTest:74] Catcha! java.lang.NullPointerException
java.lang.RuntimeException: java.lang.NullPointerException
at com.ubs.eqdel.markitfeed.core.RetrieverTest.testNPE(RetrieverTest.java:71)
...
Caused by: java.lang.NullPointerException
at com.ubs.eqdel.markitfeed.core.RetrieverTest.testNPE(RetrieverTest.java:69)
... 25 more
But for 5 of 20 attempts I see:
Exception in thread "Dispatcher-Thread-0" java.lang.NullPointerException
at java.io.Writer.write(Writer.java:140)
at org.apache.log4j.helpers.CountingQuietWriter.write(CountingQuietWriter.java:45)
at org.apache.log4j.WriterAppender.subAppend(WriterAppender.java:309)
at org.apache.log4j.RollingFileAppender.subAppend(RollingFileAppender.java:263)
at org.apache.log4j.WriterAppender.append(WriterAppender.java:160)
at org.apache.log4j.AppenderSkeleton.doAppend(AppenderSkeleton.java:251)
at org.apache.log4j.helpers.AppenderAttachableImpl.appendLoopOnAppenders(AppenderAttachableImpl.java:66)
at org.apache.log4j.AsyncAppender$Dispatcher.run(AsyncAppender.java:583)
at java.lang.T07.12.12 10:23:54,972 ERROR [main] >> [com.ubs.eqdel.markitfeed.core.RetrieverTest:74] Catcha! java.lang.NullPointerException
java.lang.RuntimeException: java.lang.NullPointerException
at com.ubs.eqdel.markitfeed.core.RetrieverTest.testNPE(RetrieverTest.java:71)
...
Caused by: java.lang.NullPointerException
at com.ubs.eqdel.markitfeed.core.RetrieverTest.testNPE(RetrieverTest.java:69)
... 25 more
What's wrong with AsyncAppender? Did anybody face the same? And what is the workaround on this?

Looks like this bug.
Edit: Based on the release notes, 1.2.16 (released on 2010-04-06) should contain the fix for this.

Related

ImapIdleChannelAdapter infinite loop on AUTHENTICATIONFAILED

I'm using ImapIdleChannelAdapter to listen to mailboxes, taking a list with credentials in a database.
If a password is wrong, I get an AUTHENTICATIONFAILED.
The problem is that it will never stop trying to reconnect.
I tried to set shouldReconnectAutomatically to false, but it will just stop the IdleTask from resubmitting, not the ReceivingTask.
Code from ImapIdleChannedAdapter:
private class ReceivingTask implements Runnable {
ReceivingTask() {
}
#Override
public void run() {
if (isRunning()) {
try {
ImapIdleChannelAdapter.this.idleTask.run();
logger.debug("Task completed successfully. Re-scheduling it again right away.");
}
catch (Exception ex) { //run again after a delay
logger.warn(ex, () -> "Failed to execute IDLE task. Will attempt to resubmit in "
+ ImapIdleChannelAdapter.this.reconnectDelay + " milliseconds.");
ImapIdleChannelAdapter.this.receivingTaskTrigger.delayNextExecution();
publishException(ex);
}
}
}
}
private class IdleTask implements Runnable {
IdleTask() {
}
#Override
public void run() {
if (isRunning()) {
try {
logger.debug("waiting for mail");
ImapIdleChannelAdapter.this.mailReceiver.waitForNewMessages();
Folder folder = ImapIdleChannelAdapter.this.mailReceiver.getFolder();
if (folder != null && folder.isOpen() && isRunning()) {
Object[] mailMessages = ImapIdleChannelAdapter.this.mailReceiver.receive();
logger.debug(() -> "received " + mailMessages.length + " mail messages");
for (Object mailMessage : mailMessages) {
Runnable messageSendingTask = createMessageSendingTask(mailMessage);
if (isRunning()) {
ImapIdleChannelAdapter.this.sendingTaskExecutor.execute(messageSendingTask);
}
}
}
}
catch (MessagingException ex) {
logger.warn(ex, "error occurred in idle task");
if (ImapIdleChannelAdapter.this.shouldReconnectAutomatically) {
throw new IllegalStateException("Failure in 'idle' task. Will resubmit.", ex);
}
else {
throw new org.springframework.messaging.MessagingException(
"Failure in 'idle' task. Will NOT resubmit.", ex);
}
}
}
}
}
So in my logs I get infinite:
WARN 2777 --- [ask-scheduler-3] o.s.i.mail.ImapIdleChannelAdapter : Failed to execute IDLE task. Will attempt to resubmit in 10000 milliseconds.
org.springframework.messaging.MessagingException: Failure in 'idle' task. Will NOT resubmit.; nested exception is javax.mail.AuthenticationFailedException: [AUTHENTICATIONFAILED] Invalid credentials (Failure)
at org.springframework.integration.mail.ImapIdleChannelAdapter$IdleTask.run(ImapIdleChannelAdapter.java:290)
at org.springframework.integration.mail.ImapIdleChannelAdapter$ReceivingTask.run(ImapIdleChannelAdapter.java:246)
at org.springframework.scheduling.support.DelegatingErrorHandlingRunnable.run(DelegatingErrorHandlingRunnable.java:54)
at org.springframework.scheduling.concurrent.ReschedulingRunnable.run(ReschedulingRunnable.java:95)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
at java.util.concurrent.FutureTask.run$$$capture(FutureTask.java:266)
at java.util.concurrent.FutureTask.run(FutureTask.java)
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$201(ScheduledThreadPoolExecutor.java:180)
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:293)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at java.lang.Thread.run(Thread.java:748)
Caused by: javax.mail.AuthenticationFailedException: [AUTHENTICATIONFAILED] Invalid credentials (Failure)
at com.sun.mail.imap.IMAPStore.protocolConnect(IMAPStore.java:708)
at javax.mail.Service.connect(Service.java:342)
at javax.mail.Service.connect(Service.java:222)
at javax.mail.Service.connect(Service.java:171)
at org.springframework.integration.mail.AbstractMailReceiver.connectStoreIfNecessary(AbstractMailReceiver.java:331)
at org.springframework.integration.mail.AbstractMailReceiver.openFolder(AbstractMailReceiver.java:338)
at org.springframework.integration.mail.ImapMailReceiver.waitForNewMessages(ImapMailReceiver.java:176)
at org.springframework.integration.mail.ImapIdleChannelAdapter$IdleTask.run(ImapIdleChannelAdapter.java:271)
... 11 common frames omitted
Those logs are odd:
Failed to execute IDLE task. Will attempt to resubmit in 10000 milliseconds
Failure in 'idle' task. Will NOT resubmit.
Is there a possibility to stop the reconnexion attempts?
I don't really want to set the reconnectDelay to absurd duration... It does not feel right.
Thank you!
Add an ApplicationListener<ImapIdleExceptionEvent> bean (or an #EventListener method) to receive ImapIdleExceptionEvents.
You can then stop the adapter in the event listener (the channel adapter is the source of the event).

Numerous Kafka Producer Network Thread generated during data publishing, Null Pointer Exception Spring Kafka

I am writing a Kafka Producer using Spring Kafka 2.3.9 that suppose to publish around 200000 messages to a topic. For example, I have a list of 200000 objects that I fetched from a database and I want to publish json messages of those objects to a topic.
The producer that I have written is working fine for publishing, let's say, 1000 messages. Then it is creating some null pointer error(I have included the screen shot below).
During debugging, I found that the number of Kafka Producer Network Thread is very high. I could not count them but they are definitely more than 500.
I have read the thread Kafka Producer Thread, huge amound of threads even when no message is send and did a similar configuration by making producerPerConsumerPartition property false on DefaultKafkaProducerFactory. But still it is not decreasing the Kafka Producer Network Thread count.
Below are my code snippets, error and picture of those threads. I can't post all of the code segments since it is from a real project.
Code segments
public DefaultKafkaProducerFactory<String, String> getProducerFactory() throws IOException, IllegalStateException {
Map<String, Object> configProps = getProducerConfigMap();
DefaultKafkaProducerFactory<String, String> defaultKafkaProducerFactory = new DefaultKafkaProducerFactory<>(configProps);
//defaultKafkaProducerFactory.transactionCapable();
defaultKafkaProducerFactory.setProducerPerConsumerPartition(false);
defaultKafkaProducerFactory.setProducerPerThread(false);
return defaultKafkaProducerFactory;
}
public Map<String, Object> getProducerConfigMap() throws IOException, IllegalStateException {
Map<String, Object> configProps = new HashMap<>();
configProps.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, kafkaProperties.getBootstrapAddress());
configProps.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class);
configProps.put(ProducerConfig.RETRIES_CONFIG, kafkaProperties.getKafkaRetryConfig());
configProps.put(ProducerConfig.ACKS_CONFIG, kafkaProperties.getKafkaAcknowledgementConfig());
configProps.put(ProducerConfig.CLIENT_ID_CONFIG, kafkaProperties.getKafkaClientId());
configProps.put(ProducerConfig.BUFFER_MEMORY_CONFIG, 512 * 1024 * 1024);
configProps.put(ProducerConfig.MAX_BLOCK_MS_CONFIG, 10 * 1000);
configProps.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, StringSerializer.class);
//updateSSLConfig(configProps);
return configProps;
}
#Bean
public KafkaTemplate<String, String> kafkaTemplate() {
ProducerFactory<String, String> producerFactory = getProducerFactory();
KafkaTemplate<String, String> kt = new KafkaTemplate<String, String>(stringProducerFactory, true);
kt.setCloseTimeout(java.time.Duration.ofSeconds(5));
return kt;
}
Error
2020-12-07 18:14:19.249 INFO 26651 --- [onPool-worker-1] o.a.k.clients.producer.KafkaProducer : [Producer clientId=kafka-client-09f48ec8-7a69-4b76-a4f4-a418e96ff68e-1] Closing the Kafka producer with timeoutMillis = 0 ms.
2020-12-07 18:14:19.254 ERROR 26651 --- [onPool-worker-1] c.w.p.r.g.xxxxxxxx.xxx.KafkaPublisher : Exception happened publishing to topic. Failed to construct kafka producer
2020-12-07 18:14:19.273 INFO 26651 --- [ main] ConditionEvaluationReportLoggingListener :
Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2020-12-07 18:14:19.281 ERROR 26651 --- [ main] o.s.boot.SpringApplication : Application run failed
java.lang.IllegalStateException: Failed to execute CommandLineRunner
at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:787) [spring-boot-2.2.8.RELEASE.jar:2.2.8.RELEASE]
at org.springframework.boot.SpringApplication.callRunners(SpringApplication.java:768) [spring-boot-2.2.8.RELEASE.jar:2.2.8.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:322) [spring-boot-2.2.8.RELEASE.jar:2.2.8.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1226) [spring-boot-2.2.8.RELEASE.jar:2.2.8.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1215) [spring-boot-2.2.8.RELEASE.jar:2.2.8.RELEASE]
at xxx.xxx.xxx.Application.main(Application.java:46) [classes/:na]
Caused by: java.util.concurrent.CompletionException: java.lang.NullPointerException
at java.util.concurrent.CompletableFuture.encodeThrowable(CompletableFuture.java:273) ~[na:1.8.0_144]
at java.util.concurrent.CompletableFuture.completeThrowable(CompletableFuture.java:280) ~[na:1.8.0_144]
at java.util.concurrent.CompletableFuture$AsyncSupply.run(CompletableFuture.java:1592) ~[na:1.8.0_144]
at java.util.concurrent.CompletableFuture$AsyncSupply.exec(CompletableFuture.java:1582) ~[na:1.8.0_144]
at java.util.concurrent.ForkJoinTask.doExec(ForkJoinTask.java:289) ~[na:1.8.0_144]
at java.util.concurrent.ForkJoinPool$WorkQueue.runTask(ForkJoinPool.java:1056) ~[na:1.8.0_144]
at java.util.concurrent.ForkJoinPool.runWorker(ForkJoinPool.java:1692) ~[na:1.8.0_144]
at java.util.concurrent.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:157) ~[na:1.8.0_144]
Caused by: java.lang.NullPointerException: null
at com.xxx.xxx.xxx.xxx.KafkaPublisher.publishData(KafkaPublisher.java:124) ~[classes/:na]
at com.xxx.xxx.xxx.xxx.lambda$0(Publisher.java:39) ~[classes/:na]
at java.util.ArrayList.forEach(ArrayList.java:1249) ~[na:1.8.0_144]
at com.xxx.xxx.xxx.xxx.publishData(Publisher.java:38) ~[classes/:na]
at com.xxx.xxx.xxx.xxx.Application.lambda$0(Application.java:75) [classes/:na]
at java.util.concurrent.CompletableFuture$AsyncSupply.run(CompletableFuture.java:1590) ~[na:1.8.0_144]
... 5 common frames omitted
Following is the code for publishing the message - line number 124 is when we actually call KafkaTemplate
public void publishData(Object object) {
ListenableFuture<SendResult<String, String>> future = null;
// Convert the Object to JSON
String json = convertObjectToJson(object);
// Generate unique key for the message
String key = UUID.randomUUID().toString();
// Post the JSON to Kafka
try {
future = kafkaConfig.kafkaTemplate().send(kafkaProperties.getTopicName(), key, json);
} catch (Exception e) {
logger.error("Exception happened publishing to topic. {}", e.getMessage());
}
future.addCallback(new ListenableFutureCallback<SendResult<String, String>>() {
#Override
public void onSuccess(SendResult<String, String> result) {
logger.info("Sent message with key=[" + key + "]");
}
#Override
public void onFailure(Throwable ex) {
logger.error("Unable to send message=[ {} due to {}", json, ex.getMessage());
}
});
kafkaConfig.kafkaTemplate().flush();
}
============================
I am not sure if this error is causing by those many network threads.
After posting the data, I have called KafkaTemplate flush method. It did not work.
I also called ProducerFactory closeThreadBoundProducer, reset, destroy methods. None of them seems working.
Am I missing any configuration?
The null pointer issue was not actually related to Spring Kafka. We were reading the topic name from a different location connected by a network. That network connection was failing for few cases and that caused null pointer issue which ultimately caused the above error.

ClasscastException cannot be cast to linearlayout ot Button.widget.button

Process: com.example.e_classroom, PID: 12427
java.lang.ClassCastException: android.widget.LinearLayout cannot be cast to android.widget.Button
at com.example.e_classroom.test_questions$3.onAnimationEnd(test_questions.java:116)
at android.view.ViewPropertyAnimator$AnimatorEventListener.onAnimationEnd(ViewPropertyAnimator.java:1136)
at android.animation.ValueAnimator.endAnimation(ValueAnimator.java:1171)
at android.animation.ValueAnimator$AnimationHandler.doAnimationFrame(ValueAnimator.java:722)
at android.animation.ValueAnimator$AnimationHandler.run(ValueAnimator.java:738)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:800)
at android.view.Choreographer.doCallbacks(Choreographer.java:603)
at android.view.Choreographer.doFrame(Choreographer.java:571)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:786)
at android.os.Handler.handleCallback(Handler.java:815)
at android.os.Handler.dispatchMessage(Handler.java:104)
at android.os.Looper.loop(Looper.java:194)
at android.app.ActivityThread.main(ActivityThread.java:5637)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:960)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
here found error can anyone help this how to solve this error
public void onAnimationEnd(Animator animation) {
if(value==0) {
try{
((TextView)v).setText(data);
questindicator.setText(position+1+"/"+list.size());
}catch (ClassCastException ex){
// here 116 line no
((Button)v).setText(data);
}
v.setTag(data);
playanimate(v, 1,data);
}
}

Gridgain serialization exception

I am trying out gridgain for the first time and facing some issues with serialization. While trying to use GridClosure to project jobs on different nodes I get marshaling exception . On debugging, It seems the failure is always for Apache log4j Logger object. Its difficult for me to avoid loggers and wanted to understand if this is a known issue or someway I can remedy this. I have requireSerializable set to false for my grid config.
Caused by: java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:601)
at org.gridgain.grid.marshaller.optimized.GridOptimizedObjectOutputStream.writeSerializable(GridOptimizedObjectOutputStream.java:292)
... 45 more
Caused by: java.io.IOException: java.lang.reflect.InvocationTargetException
at org.gridgain.grid.marshaller.optimized.GridOptimizedObjectOutputStream.writeSerializable(GridOptimizedObjectOutputStream.java:295)
at org.gridgain.grid.marshaller.optimized.GridOptimizedClassDescriptor.write(GridOptimizedClassDescriptor.java:849)
at org.gridgain.grid.marshaller.optimized.GridOptimizedObjectOutputStream.writeObject0(GridOptimizedObjectOutputStream.java:198)
at org.gridgain.grid.marshaller.optimized.GridOptimizedObjectOutputStream.writeObjectOverride(GridOptimizedObjectOutputStream.java:129)
at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:342)
at java.util.Hashtable.writeObject(Hashtable.java:988)
... 50 more
Caused by: java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:601)
at org.gridgain.grid.marshaller.optimized.GridOptimizedObjectOutputStream.writeSerializable(GridOptimizedObjectOutputStream.java:292)
... 55 more
Caused by: java.lang.NullPointerException
at org.gridgain.grid.marshaller.optimized.GridOptimizedObjectOutputStream$PutFieldImpl.value(GridOptimizedObjectOutputStream.java:1003)
at org.gridgain.grid.marshaller.optimized.GridOptimizedObjectOutputStream$PutFieldImpl.put(GridOptimizedObjectOutputStream.java:965)
at java.util.Vector.writeObject(Vector.java:1068)
... 60 more
Can you make Log4J object static so it does not affect serialization? If not, GridGain allows you to inject logger resource into pretty much anything, including closures. You can configure GridLogger as GridLog4jLogger in GridConfiguration.
Here is an example:
GridRunnable run = new GridRunnable() {
// Can be any logger, including GridLog4jLogger.
#GridLoggerResource
GridLogger log;
#Override public void run() {
log.info("Hello");
// Do some logic.
}
}
or
// Can be any logger, including GridLog4jLogger
final GridLogger log = grid.log();
GridRunnable run = new GridRunnable() {
#Override public void run() {
log.info("Hello");
// Do some logic.
}
}
For more information, refer to Resource Injection page in GridGain documentation.

getResourceAsStream() doesn't see resource

I want to unpack resources from my jar file. The structure of jar looks like this:
my.jar
META-INF
resources
my.exe
my.dll
my
namespace
UnpackResourceFromThisClass.class
I want to unpack my.exe and my.dll from jar file. I tried to unpack those files using this code:
try {
InputStream is = getClass().getResourceAsStream("/resources/my.exe")
OutputStream os = new FileOutputStream(new File(destDir))
Files.copy(is, os)
os.close()
is.close()
}
catch (NullPointerException e) {
e.printStackTrace();
}
catch (FileNotFoundException e) {
e.printStackTrace();
}
catch (SecurityException e) {
e.printStackTrace();
}
but it doesn't work. Any ideas? As a result I get this error:
java.lang.NullPointerException
at java.nio.file.Files.provider(Files.java:65)
at java.nio.file.Files.newInputStream(Files.java:106)
at java.nio.file.Files.copy(Files.java:2884)
at java_nio_file_Files$copy.call(Unknown Source)
at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:42)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:108)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:120)
at pl.ydp.gradle.Is2k8Task.getResources(Is2k8Task.groovy:84)
at pl.ydp.gradle.Is2k8Task.build(Is2k8Task.groovy:30)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:601)
at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:90)
at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:233)
at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:1047)
at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:877)
at org.gradle.api.internal.BeanDynamicObject$MetaClassAdapter.invokeMethod(BeanDynamicObject.java:216)
This is groovy code which will be used in gradle custom task.
You seem to be writing Java... Not sure if this will get you round your problem, but the above could be written in Groovy as:
this.getClass().getResource( '/resources/my.exe' ).withInputStream { ris ->
new File( destDir ).withOutputStream { fos ->
fos << ris
}
}
Delete the leading slash, getResourceAsStream will use the absolute path if the first character is a slash.

Resources