Sequence number generation in Java [closed] - java

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I need to generate a sequence number which is in ranges of 1000 to 3000. Every day at 12 midnight this sequence should reset to 1000 and for each request this should be incremented. Is there any way we can achieve this. I need to implementation in one of web application which uses Spring Boot and MongoDB.
I know in Java we cant achieve as my application will be run as multiple instance also it will be deployed in Cloud docker container.

1) To generate the sequence number, you can have the sequence value stored in a Mongo document and use findAndModify operation to increment it everytime. You might want to use the option that returns the modified document, to get the latest value.
https://docs.mongodb.com/manual/reference/method/db.collection.findAndModify/
2) To reset the value at midnight, you can use spring boot scheduling. You can annotate the method which would reset the value in the mongo document with #Scheduled and specify a cron expression to run at the exact time.
Since the sequence is actually stored in the database, you should not be having issues with multiple instances of your application.

Related

spring batch Random access in a file to implement binary search [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
I am implementing a spring batch program, my scenario is
I am having an file called A which is having an unique keyfield called RollNumber
The rollNumber is in sorted order(ASC)
I wanted to get the row which is having rollNumber as:101
Is it possible any search algorithm can be implemented on this.
I am able to read the file using Itemreader and find the row but the problem is i am having 1 million Record to process,so the time complexity is very high.
i)Having linear search takes more time,since the file is having large volume of data
Because the records are ordered by key, the natural way of implementing such thing would be Binary Search but I'm not sure is it possible to do with Spring Batch.
The sense of the Spring Batch is to read whole file and process it somehow, not to "jump" in file and do some specific stuff. For such operation you should implement your own mechanism (however it can be implemented e.g. in the Tasklet instance if this operation must be performed in the scope of the Spring Batch process).
The fact is that FlatFileItemReader implementation of ItemReader includes method
public void setLinesToSkip(int linesToSkip) {
this.linesToSkip = linesToSkip;
}
but still - after skipping those line will not allow you to get back or skip another ones during this Spring Batch process

Can any one please suggest me the best way of retrieving the records in hibernate [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I am working on a batch application where I have to retreive millions of records and need toprocess it individually. We are using hibernate to connect to DB.
I have tried using HQL and Criteria API to fetch the records(using the query Select * from table_name).
But it is taking more than 5 min .Sometimes it is getting hanged.
Can anyone suggest me the best approach for the data retrieving.
You may want to employ cursors, and if your queries are as simple as you indicated, pure JDBC may be sufficient - like here: https://docs.oracle.com/javase/tutorial/jdbc/basics/retrieving.html
Also, consider adding the WHERE clause to eliminate rows that can be recognized as irrelevant for your purposes.
And depending on your table and queries, adding an index may also help.
If you need to use hibernate, you can use ScrollableResults as mentioned here: https://stackoverflow.com/a/9891008/455449

how to implement a variable whose value is not lost after every run? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I wish to implement a variable (in Java) whose value is either stored somewhere or is not reset every time I run the program.
It's related to a "Booking reference Number" for a flight program. I know database connectivity but make a new data base for one variable is pretty pointless. Any ideas as to what I should/can do?
Also I don't want the numbers to be random I want them in order like if the first booking ID is 100 then the next one should be 101 and so on.
Organize your data in a structure and then serialize it.When you re-run your program, look for that serialized version in the file system, if there is any, read it. Viola.!
You should write the variable to a file and then read it from the file the next time you run the program.

What happens if a method is called too many times at the same time [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
Let us say that I have a method that writes to a file or database. What if different parts of application calls this method too many times at the same time or in same interval of time. All those method calls are maintained in some stack/queue in memory and wait for previous requests to be served ?
Writing to the same file is platform dependent, like Unix allows concurrent writes to the same file.
You need to see the synchronization techniques - how you want to manage the read write operations.
If you see from DB perspective the db engine handles it properly - whichever comes first will be served. The next insert would depend on the first insert(in case you already inserted with the same key in the previous operation - then obviously it ll throw an exception)
Also I would say if different parts of your application are appending data to the same file at the same time - there could be design flaw and you need to reconsider the design

How to monitor particular column in database [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I have an Oracle Database and I want to monitor a particular table/column in the DB . If the column value crosses threshold value I want to call my java application code to perform some operation LIKE EXAMPLE SCALE UP OR SHUTDOWN. How can I achieve this? Any pointers or help needed.
Set Serverout On
Declare
comm Varchar2(2000);
Begin
comm := OSCommand_Run('/home/jguy/runJavaApp.sh')--for calling commands
DBMS_OUTPUT.Put_Line(comm);
End;
I suggest you to use triggers, they are designed to do stuff like this and I really wouldn't mess up it with Java in this case.
You can use triggers (i this case an update trigger) that will execute a particular function/stored procedure on certain conditions.
There are multiple ways you can do this:
Trigger on the table, which will check column value upon each DML statement and if value crosses threshold.
As trigger may have some performance impact on table, you can create a scheduler job calling a stored procedure which will invoke on periodic basis (every min or anything you like) and then check the column value and call java app if it crosses threshold.
I prefer trigger but check the performance impact on the table.

Categories

Resources