Java shutdown hook and writing to an Excel Sheet - java

So high level I have a program that reads data from an Excel SpreadSheet stores it in a list of hash maps, does dome operations, gets a status, and then writes the information back the Excel SpreadSheet. As of right now if an Exception of some sort causes the program to crash or the program finishes normally everything is fine.
Now I am trying to handle what if a user terminates the run by pressing Ctrl + C. I figured the best way to do this would be implementing my own shutdown hook.
private static class TerminationThread extends Thread
{
public void run() {
for(UserCredential user : creds)
{
Accel.setRow(user.getMap(), user.getRowIndex());
}
try {
Accel.writeToFile();
} catch (IOException e) {
e.printStackTrace();
}
}
}
In my Main() I register this as a shutdown hook
TerminationThread sH = new TerminationThread();
Runtime.getRuntime().addShutdownHook(sH);
When the program enters the shutdown hook everything is fine until it gets to the portion that writes to the file then it starts throwing a bunch of exceptions and at the end i get a corrupted spreadsheet.
Here is my write code:
public void writeToFile() throws IOException
{
try
{
fileOut = new FileOutputStream(theFile);
theWorkbook.write(fileOut);
}
catch (IOException e)
{
throw new IOException("Exception in writeToFile(). " + e);
}
finally
{
try
{
fileOut.flush();
fileOut.close();
}
catch(IOException e)
{
throw new IOException("Exception closing FileOutputStream. " + e);
}
}
}
What I am assume is happening is that the object that handles my excel spreadsheet is getting wiped out before the write is completed or the list of hash maps where i store the data before writing it is getting wiped out.
I have looked at Thread Joins as a possible solution to my problem but upon looking at them I don't think they are the solution to my problem.
So I guess my question is how do i get this to work the way I would like it to work
Cheers,
Meinert
EDIT: Here is the exception
Exception in thread "Thread-4" org.apache.xmlbeans.impl.values.XmlValueDisconnectedException
at org.apache.xmlbeans.impl.values.XmlObjectBase.check_orphaned(XmlObjectBase.java:1213)
at org.apache.xmlbeans.impl.values.XmlObjectBase.newCursor(XmlObjectBase.java:243)
at org.apache.xmlbeans.impl.values.XmlComplexContentImpl.arraySetterHelper(XmlComplexContentImpl.java:1073)
at org.openxmlformats.schemas.spreadsheetml.x2006.main.impl.CTDefinedNamesImpl.setDefinedNameArray(Unknown Source)
at org.apache.poi.xssf.usermodel.XSSFWorkbook.saveNamedRanges(XSSFWorkbook.java:1270)
at org.apache.poi.xssf.usermodel.XSSFWorkbook.commit(XSSFWorkbook.java:1291)
at org.apache.poi.POIXMLDocumentPart.onSave(POIXMLDocumentPart.java:313)
at org.apache.poi.POIXMLDocument.write(POIXMLDocument.java:173)
at com.cba.statuschecker.ExcelManager.writeToFile(ExcelManager.java:179)
at com.cba.statuschecker.Main$TerminationThread.run(Main.java:495)
EDIT 2: Well I figured out my own problem. I was approaching this the wrong way. My code is correct the only problem was that in my finally block I also have a file write. So when my program would execute all the way through without interruption it would execute the write twice which was the cause of the exception. I fixed this by checking if the finally block was executed and if it was the write in the shutdown hook is skipped.
Finally:
finally
{
// Print out statuses
printStatus();
for(UserCredential user : creds)
{
Accel.setRow(user.getMap(), user.getRowIndex());
}
try {
Accel.writeToFile();
} catch (IOException e) {
e.printStackTrace();
}
didFinally = true;
LOGGER.info(Log("Fin."));
System.exit(0);
}
Shutdown Hook
private static class TerminationThread extends Thread
{
public void run() {
System.out.println("Shutdown Initiated...");
if(!didFinally){
for(UserCredential user : creds)
{
Accel.setRow(user.getMap(), user.getRowIndex());
}
System.out.println("Writing to file...");
try {
Accel.writeToFile();
} catch (IOException e) {
e.printStackTrace();
}
}
System.out.println("Terminated!");
}
}

Well I figured out my own problem. I was approaching this the wrong way. My code is correct the only problem was that in my finally block I also have a file write. So when my program would execute all the way through without interruption it would execute the write twice which was the cause of the exception. I fixed this by checking if the finally block was executed and if it was the write in the shutdown hook is skipped.
Finally:
finally
{
// Print out statuses
printStatus();
for(UserCredential user : creds)
{
Accel.setRow(user.getMap(), user.getRowIndex());
}
try {
Accel.writeToFile();
} catch (IOException e) {
e.printStackTrace();
}
didFinally = true;
LOGGER.info(Log("Fin."));
System.exit(0);
}
Shutdown Hook
private static class TerminationThread extends Thread
{
public void run() {
System.out.println("Shutdown Initiated...");
if(!didFinally){
for(UserCredential user : creds)
{
Accel.setRow(user.getMap(), user.getRowIndex());
}
System.out.println("Writing to file...");
try {
Accel.writeToFile();
} catch (IOException e) {
e.printStackTrace();
}
}
System.out.println("Terminated!");
}
}

Related

How can i execute commands based on if the status of the previous one?

Basically i have 2 different commands i could possibly execute. If the first one does not work, I want to execute the second command.
Is there some easier, cleaner way to do this?
What would i even put in the if statement?
try
{
// code that may throw an exception
driver.findElement(By.id("component-unique-id-31")).click();
}
catch (Exception ex)
{
// Print to console
}
if(previous command threw an exception){
try
{
//Another command i want executed if the above fails
driver.findElement(By.xpath("//h3[normalize-space()='Something']")).click();
}
catch (Exception ex)
{
// handle
}
}
You can put the second command inside the catch block of the first try-catch, as following:
try
{
// code that may throw an exception
driver.findElement(By.id("component-unique-id-31")).click();
}
catch (Exception ex1)
{
try
{
//Another command i want executed if the above fails
driver.findElement(By.xpath("//h3[normalize-space()='Something']")).click();
}
catch (Exception ex2)
{
// handle
}
}
You can wrap up both the try-catch{} blocks within a single try-catch-finally{} block as follows:
try {
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.id("component-unique-id-31"))).click();
System.out.println("Within in try block.");
}
catch(TimeoutException e) {
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//h3[normalize-space()='Something']"))).click();
System.out.println("Within in catch block.");
} finally {
System.out.println("The 'try catch' is finished.");
}
PS: You must avoid catching the raw exception.

How to increase record and play volume with Java sound?

I use the following code to record and play sounds with Java, but the volume is too low, how to make it louder, at least 2,3 times louder ?
public void Record_Audio(String File_Path,AudioFileFormat.Type File_Type)
{
try
{
audioFormat=getAudioFormat(); // Get things set up for capture
DataLine.Info dataLineInfo=new DataLine.Info(TargetDataLine.class,audioFormat);
targetDataLine=(TargetDataLine)AudioSystem.getLine(dataLineInfo);
//Create a thread to capture the microphone data into an audio file and start the thread running. It will run
// until the Stop button is clicked. This method will return after starting the thread.
new Record_Thread(File_Path,File_Type).start();
}
catch (Exception e)
{
System.out.println(e);
System.exit(0);
}
}
private void Play_Audio_Recording()
{
File Audio_File=new File(Current_Folder_Path+File_Name_ComboBox.getSelectedItem().toString().trim()+"."+Get_Audio_File_Type());
try
{
Audio_Clip=Applet.newAudioClip(Audio_File.toURI().toURL());
Audio_Clip.play();
}
catch (Exception e) { e.printStackTrace(); }
}
class Record_Thread extends Thread
{
String File_Path;
AudioFileFormat.Type File_Type;
Record_Thread(String File_Path) { this(File_Path,AudioFileFormat.Type.WAVE); }
Record_Thread(String File_Path,AudioFileFormat.Type File_Type)
{
this.File_Path=File_Path;
this.File_Type=File_Type;
}
public void run()
{
Audio_File=new File(File_Path);
try
{
targetDataLine.open(audioFormat);
targetDataLine.start();
AudioSystem.write(new AudioInputStream(targetDataLine),File_Type,Audio_File);
}
catch (Exception e) { e.printStackTrace(); }
}
}
You would use the FloatControl.Type (https://docs.oracle.com/javase/7/docs/api/javax/sound/sampled/FloatControl.Type.html) to set either the volume or the master gain. Something like:
targetDataLine=(TargetDataLine)AudioSystem.getLine(dataLineInfo);
javax.sound.sampled.FloatControl c = (FloatControl)targetDataLine.getControl(FloatControl.Type.VOLUME);
c.setValue(c.getMaximum());
might work.

IllegalStateException [start called in an invalid state: 1] on restarting Android MediaRecorder

I am trying to implement simple logic to start/stop recording with MediaRecorder of Android.
The cycle is
connect to localSocket / set options / mRecorder.prepare();
mRecorder.start();
mRecorder.stop(); mRecorder.reset();
Then, loop between 2 and 3.
In the first cycle, 1,2,3 works fine as intended, however, I've got an error on the second start(restart) after the first stop.
com.example.app E/MediaRecorder﹕ start called in an invalid state: 1
What is the MediaRecorder state 1? What do I miss?
Thanks for your input.
if (cmd.equals("connect"))
{
try
{
sender.connect(new LocalSocketAddress(SOCKET_ADDRESS));
mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
mRecorder.setOutputFormat(MediaRecorder.OutputFormat.AAC_ADTS);
mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);
mRecorder.setOutputFile(sender.getFileDescriptor());
mRecorder.prepare();
}
catch (IOException e)
{ e.printStackTrace(); }
}
if (cmd.equals("start"))
{
try
{
mRecorder.start();
}
catch (IllegalStateException e)
{ e.printStackTrace(); }
}
if (cmd.equals("stop"))
{
try
{
mRecorder.stop();
mRecorder.reset();
}
catch (Exception e)
{ e.printStackTrace(); }
}
I'd had the same problem. I had to make a function initRecorder that sets up and prepares the media recorder. Then I called this function each time after the start button was pressed but before start was called. recreate() after stop also works.
StartRecording.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
toneG.startTone(ToneGenerator.TONE_CDMA_ALERT_CALL_GUARD, 100);
try {
try {
initRecorder(mHolder.getSurface());
} catch (IOException e) {
e.printStackTrace();
}
mMediaRecorder.start();
Log.e("mRecorder", "Started");
} catch (RuntimeException e) {
Log.e("mRecorder", "Start Failure");
e.printStackTrace();
}
}
});
private void initRecorder(Surface surface) throws IOException {
toneG.startTone(ToneGenerator.TONE_CDMA_ALERT_CALL_GUARD, 1000);
if (mMediaRecorder == null) mMediaRecorder = new MediaRecorder();
// mMediaRecorder.setCamera(mCamera);
mMediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
// mMediaRecorder.setOutputFormat(8);
mMediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
mMediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.MPEG_4_SP);
//mMediaRecorder.setVideoEncodingBitRate(512 * 1000);
mMediaRecorder.setVideoFrameRate(30);
// mMediaRecorder.setVideoSize(640,480);
mMediaRecorder.setPreviewDisplay(surface);
mMediaRecorder.setOutputFile(path);
// mMediaRecorder.setPreviewDisplay(mHolder.getSurface());
mMediaRecorder.setMaxDuration(10000); // 10 seconds
try {
mMediaRecorder.prepare();
Log.e("mRecorder", "Prepared");
} catch (IOException e) {
Log.e("mRecorder", "Prepare Failure");
e.printStackTrace();
}
mInitSuccesful = true;
}
In the scond cycle you have not called prepare, you need to call that before you can call start on media recorder
This is a self-answer, but I would not check as the answer because it's just work-around.
According to #Pulkit Sethi, state-1 means either MediaRecorder does not start properly or stop properly.
Perhaps, it's due to the local socket object sender.getFileDescriptor() as the target of setOutputFile.
So far, it's way too complicated and I could not find a way to stop gracefully enough to re-start or re-use MediaRecorder, I chose to dispose all everytime.
So
The cycle is
start localSocket/Server
connect to localSocket / set options / mRecorder.prepare();
mRecorder.start();
stop/close/release whole
This looks not the smartest way, but at least simple and stable, and I am happy with the result to start/stop/ & re-start as intended.
if (cmd.equals("stop"))
try
{
if (sender != null)
{
sender.close();
}
if (receiver != null)
{
receiver.close();
}
if (server != null)
{
server.close();
}
}
catch (IOException e)
{
e.printStackTrace();
}
sender = null;
receiver = null;
server = null;
}
for MediaRecorder
mRecorder.release();
The output file needs to be an actual file, not a socket. This is because MediaRecorder usually needs to be able to seek back in the file to update the header when the recording ends, and you can't seek in a socket.

Android / Java: how to define a code block and leave it without custom exceptions?

I seem to be stuck with a very simple task that would require GOTO statements and, in my opinion, would justify a use of those.
I have the very simple task to exit a void on different conditions. Within its code, several dozen operations are being done and most of them can fail. I test them with try {}.
Now, based on the criticality of the operation, I either need to exit immediately and do nothing else, or, I just need to interrupt control flow and jump to a final point to do some cleaning up and then exit the method.
MWE:
public void myMethod () {
try { op1(); } catch (Exception e) { return; } // Fail here: exit immediately
try { op2(); } catch (Exception e) { cleanUpFirst(); return; } // Fail here: do Cleaning up first, then exit
try { op3(); } catch (Exception e) { return; } // Fail here: exit immediately
try { op4(); } catch (Exception e) { cleanUpFirst(); return; } // Fail here: do Cleaning up first, then exit
try { op5(); } catch (Exception e) { cleanUpFirst(); return; } // Fail here: do Cleaning up first, then exit
// ....
}
public void cleanUpFirst() { /* do something to clean up */ }
For code readability, I'd like to a) avoid a separate function and b) do not have more than one statement within the catch block; it just blows up the code. So, in my opinion this would perfectly justify the use of a GOTO statement.
However, the only solution I came up with, given that only two outcomes are possible, is this:
public void myMethod () {
do {
try { op1(); } catch (Exception e) { return; }
try { op2(); } catch (Exception e) { break; }
try { op3(); } catch (Exception e) { return; }
try { op4(); } catch (Exception e) { break; }
try { op5(); } catch (Exception e) { break; }
// ....
} while (1==0);
/* do domething to clean up */
}
Yes, I have heard of exceptions and that is is the Java way. Is that not as overkilled as using the separate void? I do not need the specifics, I simply need a yes/no result from each operation. Is there a better way?
why not
boolean cleanupfirst = false;
try {
op1 ();
cleanupfirst = true;
op2 ();
cleanupfirst = false;
op3 ();
} catch (Exception e) {
if (cleanupfirst)
cleanup ();
return;
}
You're over-thinking it.
4 minor adjustments.
Let Opn() return a boolean for success or failure, rather than throwing an Excpetion.
Let CleanupFirst handle program termination (you can rename it to clean exit if you want). The new parameter passed to CleanExit is the System.exit code.
Use System.Exit to return a proper return code to the OS, so you can use it in scripting.
It does not seem like your program has a successful path.
if (!op1())
System.exit(1); // <- send a failed returncode to the OS.
if(!op2())
cleanExit(2);
if (!op3())
System.exit(3); // <- send a failed returncode to the OS.
if (!op4())
cleanExit(4);
if (!op5())
cleanExit(5);
cleanExit(0);
More methods for better readability:
public void myMethod() {
try {
tryOp1();
tryOp2();
...
} catch(Exception ignore) {}
}
public void tryOp1() throws Exception {
op1();
}
public void tryOp2() throws Exception {
try {
op1();
} catch (Exception e) {
cleanUp();
throw e;
}
}

Java Do X IF catch was not called from a try loop

Not sure if this has already been answered, but.
I know that in java there is the try, catch and finally blocks, but is there one which is only called if try has no errors/exceptions?
Currently after stating the command that needs to be run, I'm setting a boolean to true, and after the try and catch block, the program checks for if the boolean is true.
I'm pretty sure that there is a much simpler way, help is appreciated!
Just put your code after the try...catch block and return in the catch:
boolean example() {
try {
//dostuff
} catch (Exception ex) {
return false;
}
return true;
}
This would also work if you put the return true at the end of the try block as the code would jump to the catch on error and not execute the rest of the try.
void example() {
try {
//do some stuff that may throw an exception
//do stuff that should only be done if no exception is thrown
} catch (Exception ex) {
throw new RuntimeException(ex);
}
}
No, there is no block called only if no exceptions were raised.
The catch block is called if there were exceptions, finally is called regardless.
As stated, you can emulate such a beast with something like:
bool completed = false;
try {
doSomeStuff();
completed = true;
} catch (Exception ex) {
handleException();
} finally {
regularFinallyHandling();
if (completed) {
thisIsTheThingYouWant();
}
}
but there's nothing built into the language itself that provides this functionality.

Categories

Resources