jitendra vishnoi

Greenhorn
+ Follow
since Jan 19, 2010
Merit badge: grant badges
For More
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
0
Received in last 30 days
0
Total given
2
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by jitendra vishnoi

Hi Jeanne

You wrote such a good book which covered mostly areas of exam. I guess I started your book from last month and before that I was going through oracle video tutorial and thank God I got this book otherwise not a chance. Apart form certification I learnt lots of new things in depth because of this certification exam.
And thanks again for creating this thread which was included in monthly JavaRanch mail and because of that only I got news that exam cost is 25$ only and which pushed me to prepare for it.

regarding the exam questions, They were much simpler than practice question of books. I faced some issue with test delivery system because of net connectivity issue and I didn't had my phone with me as they are trying to approach me but after all hiccups I qualified exam.

Thanks,


Just stop casting object references. If you need references of type Pencil, create a Consumer<String, Pencil> and not a Consumer<String, KafkaRecord>.



Actually its like we are getting different type of objects and that's why wanted to keep common part in some super class. Its like everyone will override their playWithRecord in thier own way and we dont have write duplicate code.


If you don't, and your Pencil really does represent a Kafka record, then why not let it extend KafkaRecord.



Yes Pencil is a Kafka record and it was accedently missed but the problem is its working fine.
If I have to ask someone to update the code I am not sure about the logic I would give as its working now and after the change it will keep working.
3 years ago
Hi Stephan,

I added below method(GIT code also updated) and pencilRecords.forEach(bean -> playWithRecord(bean.value()));


I tried with private void playWithRecord(KafkaRecord record) but its failing in runtime while receiving message -


Looks to me only reason why Consumer<String, KafkaRecord> pencilConsumer = getPencilConsumer(); is successful if type is erased as any futher line which trying to consider record as KafkaRecord is ClassCastException.
Let me know about your thoughts or if there is way to check what is code at runtime.
3 years ago
Yes it has been called and return Pencil object.
Also could you please share your thoughts why you dont think its not Generic Erasure thing.

I was suspecting its possible as ConsumerRecords<String, KafkaRecord> lost its type(Generic Erasure) at run time and all works.
3 years ago
Hi Stephan,

I am sending Pencil object form Kafka producer to Kafka as message and it sending those to consumer and its picking those message at line 27 in https://github.com/jitendraVishnoi/kafka/blob/master/src/main/java/CustomExecutor.java its able to print those Pencil messages. If there is method say void sellPencil(Pencil pencil), I need to just cast it like (Pencil) bean.value() and pass it. Everything works fine.
I understand I am missing some basic concepts. I got it how its not giving any error at compile time but if its not generic related how its working, Could you please help me understand.
Appreciate all your support.
3 years ago
Hi All,

I had one query regarding some piece of code I worked and got confused with Java Generic behaviour. I asked query in https://stackoverflow.com/questions/65607322/java-how-to-in-force-generic-type and I got the response that how it could have prevented.
I have another query which is not answered and that's why asking here.
Link of code sample - https://github.com/jitendraVishnoi/kafka

// Pencil does not implement KafkaRecord
ConsumerRecords<String, KafkaRecord> records = consumer.poll(Duration.ofMillis(getTimeout()));// consumer.poll will return ConsumerRecords<String,Pencil>

Could you please help me understand how above line running without issue.

3 years ago
Thank you Tim.

3 years ago
Happy new year to all and thanks for looking into my query.

Les - I am assuming(correct me if you meant for all) that you are talking about checked exception(not RunTimeException or the one @Tim suggessed - OutOfMemoryException) and yes we always log checked exception and based on logic either re-throw or continue with rest of logic.


1. GET /uri/<storeId>/orders, This api will return list of store orders(array of JSON object). Internal logic of API is like that it will collect stores order ids from different source(step 1) and start collecting details(step 2) for each order and while collecting order detail for any order it may get exception(may be order got corrupted by some process)
...


Here exception is RunTimeException as we noticed it we put a try-catch block and logged this exception, The flag which we created just to control whether whole API should fail(error in api response + log the culprit order) or just log the culprit order and return rest of orders detail . I feel kind of agree with Les that its business who can decide whats expected behavior.

2. User object is injected by framework in our code. We can get user attributes by its getProperty method...


Campbell - As I understood we can have doc updated when particular method can throw NPE or any RunTimeException. What about some filed which is promised by framework to be injected. Framework would most probably able to inject those object unless there is some issue with application startup(or may be issue within in framework/some config issue) and framework was not proper initialized. What is the point of having a NPE check when application is not started properly.
3 years ago
Hi All,

I was going through Stack Exchange - Why should 'boneheaded' exceptions not be caught, especially in server code?(in my case it will not cause server crash)
and it reminded me few scenario which I faced some time back and looking for what should be correct solution/feedback.
I will try to explain scenario and my understanding.

1. GET /uri/<storeId>/orders, This api will return list of store orders(array of JSON object). Internal logic of API is like that it will collect stores order ids from different source(step 1) and start collecting details(step 2) for each order and while collecting order detail for any order it may get exception(may be order got corrupted by some process) it will cause whole API to fail and API will return respective error so that store(client) can inform back to us and we should investigate the issue and fix it. But investigation can take time so we created a flag which will change API behavior and force it log the exception for corrupted order and return rest of orders.
If flag is true then PROS of this approach is that store will able to keep processing order(consider 10 order a day) but we have rely on someone who monitor logs and if missed we will never notice that something wrong happening with some orders.
If flag is OFF, API will fail and return error and we have to rely on stores exception handling(whether they inform us back or not) + someone from team to monitor logs. If none notice the issue those order will never get processed.
Query - Should we go with flag value as true/false ?

2. User object is injected by framework in our code. We can get user attributes by its getProperty method, eg. user.getProperty("firstName"). I have seen people put a NPE check before getting any value form user object with explanation that it will not cause any issue(which is correct). But as my understanding if user is null then something is already wrong with application and we should not put NPE as we will never know that it happened(Obviously customer will never get "firstName" if user is null).
Query - Shouldn't we avoid these checks ?
3 years ago
I got solution for my problem.



If it is not working on your machine just add more new ChangeListener().start(); to make CPU enough busy to force its threads to use local copy of variable.
Hi Seetharaman,

Thanks for reply. As per my understanding If a variable is declared as volatile it means when it used by a thread, then thread will go to main memory for getting its value not its local copy of that variable.
Hi All,

I need a code example for understanding of volatile keyword. I found some example on net but they are not working on my machine. Is there are any universal example for volatile that can run on all machines with same result.
Thanks in advance.


Jitendra Vishnoi.
Hi Henry,

I tried to get name of thread in main method by using firstThread.getName(), It is giving me same name as given by run method of ThreadUsingExtends.
I made the follwing change at line number 10 in main method:



and the output is

I am Thread First Thread with priority 5
Priority of First Thread 8
Exit from main
I am Thread First Thread with priority 5
I am Thread First Thread with priority 5
I am Thread First Thread with priority 5
I am Thread First Thread with priority 5

when I am trying Thread.currentThread().getName() in run() and firstThread.getName() in main(). Both are giving same result : First Thread. Then What is the problem when I am trying to getPriority().
Appreciate all your support.


Hi All,
I am trying to setPriority of a thread after it has been started. Main thread is showing that priority of firstThread has been changed but same is not reflecting in the firstThread run method.
PFB the code.




output :

Priority of first thread 8
Exit from main
I am Thread First Thread with priority 5
I am Thread First Thread with priority 5
I am Thread First Thread with priority 5
I am Thread First Thread with priority 5
I am Thread First Thread with priority 5