MongoDb and JAVA 4.9 driver-sync driver - Exception in thread "BufferPoolPruner-1-thread-1" java.lang.IllegalMonitorStateException - java

Purpose - connect via Java (not using maven) to an Atlas mongo cluster, get some values and print them out. IT DOES WORK, it gets the values and displays them but there is no clean exit right now. Over a few exectutions, a mem leak seems to happen and the maxheap gets exhausted. Have to restart the agentmanager.
This is HCL DOMINO 11 (Formerly IBM DOMINO) Java Agent. (using the 4.9 Mongodb Driver Sync Driver and core)
Is there a better way to terminate the connection and have threads terminate properly before domino shuts down the jvm so we dont get the exception:
Exception in thread "BufferPoolPruner-1-thread-1" java.lang.IllegalMonitorStateException
Source -
public class JavaAgent extends AgentBase {
public void NotesMain() {
try {
Session session = getSession();
AgentContext agentContext = session.getAgentContext();
String DB_USERNAME="username";
String DB_PASSWORD="pw";
String DB_HOST_PROD="host";
String DB_DATABASE="db";
ConnectionString connectionString = new ConnectionString("mongodb+srv://"+DB_USERNAME+":"+DB_PASSWORD+DB_HOST+"/?readPreference=Primary&retryWrites=true&w=majority");
MongoClientSettings settings = MongoClientSettings.builder()
.applyConnectionString(connectionString)
.serverApi(ServerApi.builder()
.version(ServerApiVersion.V1)
.build())
.build();
MongoClient mongoClient = MongoClients.create(settings);
MongoDatabase database = mongoClient.getDatabase(DB_DATABASE);
MongoCollection<Document> collection = database.getCollection("users");
Document query = new Document("_id", new ObjectId("*******SOMEID******"));
Document result = collection.find(query).iterator().next();
System.out.println("Firstname: "+result.getString("first_name"));
System.out.println("The count is:");
System.out.println(collection.estimatedDocumentCount());
mongoClient.close();
} catch(Exception e) {
e.printStackTrace();
}
}
}
This is the error thrown after calling mongo client close. It appears that because its connecting to a cluster there are multiple monitor threads and these BufferPoolPruner threads. Closing the connection doesn't seem to kill them and researching MonitorStateException seems to indicate use of synchronization and locks....
Not sure what to try next.
Exception in thread "BufferPoolPruner-1-thread-1" java.lang.IllegalMonitorStateException
at java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject.signal(Unknown Source)
at java.util.concurrent.ScheduledThreadPoolExecutor$DelayedWorkQueue.take(Unknown Source)
at java.util.concurrent.ScheduledThreadPoolExecutor$DelayedWorkQueue.take(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor.getTask(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
Error cleaning up agent threads
Exception in thread "BufferPoolPruner-1-thread-2" java.lang.IllegalMonitorStateException
at java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject.signal(Unknown Source)
at java.util.concurrent.ScheduledThreadPoolExecutor$DelayedWorkQueue.take(Unknown Source)
at java.util.concurrent.ScheduledThreadPoolExecutor$DelayedWorkQueue.take(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor.getTask(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
I have tried connecting to primary only (doesn't seem to care that its specified) so threads to clustermates are not instantiated.

Related

Microsoft Translator API error retrieving translation

I'm simply trying to run this sample code below:
import com.memetix.mst.language.Language;
import com.memetix.mst.translate.Translate;
public class Translator {
public static void main(String[] args) throws Exception {
Translate.setClientId("ID GOES HERE");
Translate.setClientSecret("SECRET GOES HERE");
String translatedText = Translate.execute("Bonjour le monde",
Language.FRENCH, Language.ENGLISH);
System.out.println(translatedText);
}
}
and I'm getting the following Exception:
Exception in thread "main" java.lang.Exception: [microsoft-translator-api] Error retrieving translation : datamarket.accesscontrol.windows.net
at com.memetix.mst.MicrosoftTranslatorAPI.retrieveString(MicrosoftTranslatorAPI.java:202)
at com.memetix.mst.translate.Translate.execute(Translate.java:61)
at Translator.main(Translator.java:10)
Caused by: java.net.UnknownHostException: datamarket.accesscontrol.windows.net
at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
at java.net.PlainSocketImpl.connect(Unknown Source)
at java.net.SocksSocketImpl.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at sun.security.ssl.SSLSocketImpl.connect(Unknown Source)
at sun.security.ssl.BaseSSLSocketImpl.connect(Unknown Source)
at sun.net.NetworkClient.doConnect(Unknown Source)
at sun.net.www.http.HttpClient.openServer(Unknown Source)
at sun.net.www.http.HttpClient.openServer(Unknown Source)
at sun.net.www.protocol.https.HttpsClient.<init>(Unknown Source)
at sun.net.www.protocol.https.HttpsClient.New(Unknown Source)
at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.getNewHttpClient(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.plainConnect0(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.plainConnect(Unknown Source)
at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.connect(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.getOutputStream0(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.getOutputStream(Unknown Source)
at sun.net.www.protocol.https.HttpsURLConnectionImpl.getOutputStream(Unknown Source)
at com.memetix.mst.MicrosoftTranslatorAPI.getToken(MicrosoftTranslatorAPI.java:133)
at com.memetix.mst.MicrosoftTranslatorAPI.retrieveResponse(MicrosoftTranslatorAPI.java:160)
at com.memetix.mst.MicrosoftTranslatorAPI.retrieveString(MicrosoftTranslatorAPI.java:199)
... 2 more
I know it seems like I'm not even trying to figure this out on my own but I'm a complete beginner and can't really understand the Exception trace at all by myself. I'm pretty sure I got the right client Secret. In my azure account I only see an application ID and an Object ID. I'm using the application ID as the client ID.
Does anyone have any ideas on what might be causing this? Any help is greatly appreciated.
Thank you!
The third party Java wrapper boatmeme/microsoft-translator-java-api for MS Azure Translator API is too old & unavailable, because it wrappered the old Microsoft Translator - Text Translation which is old & unavailable now.
There is a notice at the page top of the site Azure datamarket.
DataMarket and Data Services are being retired and will stop accepting new orders after 12/31/2016. Existing subscriptions will be retired and cancelled starting 3/31/2017. Please reach out to your service provider for options if you want to continue service.
For using the new Azure Translator API on Azure portal, you need to refer to the document Announcements: Microsoft Translator Moves to the Azure portal to know how to create the new one on Azure portal and use it via the new REST APIs. Meanwhile, just as reference, you can see my answer in Java for the other SO thread Microsoft Translator API Java, How to get client new ID with Azure.
Hope it helps.

java.net.SocketException: Permission denied: connect. What could be the cause and how to avoid it?

I'm running into a java.net.SocketException (Permission denied: connect) when sending a lot of requests to a server. I've tried the -Djava.net.preferIPv4Stack=true option as mentioned in other threads.
This issue only occurs after a lot of connections have already been made. The following code can be used to reproduce the issue:
public class Example {
public static void main(String[] args) {
if(args.length == 1) {
System.out.println(args[0]);
for(int i = 0; i < Integer.MAX_VALUE; i++) {
requestURL(args[0]);
}
}
}
public static void requestURL(String targetUrl) {
URL url = new URL(targetUrl);
HttpURLConnection httpCon = (HttpURLConnection)url.openConnection();
httpCon.setDoInput(true);
BufferedReader rd = new BufferedReader(new InputStreamReader(httpCon.getInputStream()));
//handle response here
rd.close();
httpCon.disconnect();
}
}
The code will successfully send the requests until the Exception occurs. This has been verified in the server's log files.
Please run the sample code only against a server that you are permitted to, as it generates quite a bit of traffic / load on the server.
So the question now is: How to avoid that Exception? Or any workarounds? And what is the actually issue here?
EDIT
added httpCon.disconnect(). (doesn't solve the problem)
EDIT #2 (StackTrace)
java.net.SocketException: Permission denied: connect
at java.net.DualStackPlainSocketImpl.connect0(Native Method)
at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source)
at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source)
at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source)
at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
at java.net.PlainSocketImpl.connect(Unknown Source)
at java.net.SocksSocketImpl.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at sun.net.NetworkClient.doConnect(Unknown Source)
at sun.net.www.http.HttpClient.openServer(Unknown Source)
at sun.net.www.http.HttpClient.openServer(Unknown Source)
at sun.net.www.http.HttpClient.<init>(Unknown Source)
at sun.net.www.http.HttpClient.New(Unknown Source)
at sun.net.www.http.HttpClient.New(Unknown Source)
at sun.net.protocol.http.HttpURLConnection.getNewHttpClient(Unknown Source)
at sun.net.protocol.http.HttpURLConnection.plainConnect0(Unknown Source)
at sun.net.protocol.http.HttpURLConnection.plainConnect(Unknown Source)
at sun.net.protocol.http.HttpURLConnection.connect(Unknown Source)
at Example.requestUrl(Example.java)
at Example.main(Example.java)
When the issue occurs no further requests are sent to the server (no log entries on the server and no SYN packets (sniffed with tcpdump)).
EDIT #3
I've forgotten to run the application with the -Djava.net.preferIPv4Stack=true argument.
It hasn't thrown an exception yet when using both, the aforementioned argument and the httpCon.disconnect() method.
Try to use httpCon.disconnect() to be sure to realease the socket after you finish to read the response.

Why is Java returning an error rather than an IOException when try and get a file lock

This code running with Java 1.8.0_72 25.72-b15 64bit on Windows 8.1
protected FileLock getFileLockForWriting(FileChannel fileChannel, String filePath) throws IOException
{
logger.finest("locking fileChannel for " + filePath);
FileLock fileLock;
try
{
fileLock = fileChannel.tryLock();
}
//Assumes locking is not supported on this platform so just returns null
catch (IOException exception)
{
return null;
}
//Couldnt getFields lock because file is already locked by another application
if (fileLock == null)
{
throw new IOException(ErrorMessage.GENERAL_WRITE_FAILED_FILE_LOCKED.getMsg(filePath));
}
return fileLock;
}
is reporting this error for a particular user
java.lang.Error: java.io.IOException: The specified server cannot perform the requested operation
at sun.nio.ch.FileKey.create(Unknown Source)
at sun.nio.ch.SharedFileLockTable.<init>(Unknown Source)
at sun.nio.ch.FileLockTable.newSharedFileLockTable(Unknown Source)
at sun.nio.ch.FileChannelImpl.fileLockTable(Unknown Source)
at sun.nio.ch.FileChannelImpl.tryLock(Unknown Source)
at java.nio.channels.FileChannel.tryLock(Unknown Source)
at org.jaudiotagger.tag.id3.AbstractID3v2Tag.getFileLockForWriting(AbstractID3v2Tag.java:1080)
at org.jaudiotagger.tag.id3.AbstractID3v2Tag.writeBufferToFile(AbstractID3v2Tag.java:1462)
at org.jaudiotagger.tag.id3.ID3v23Tag.write(ID3v23Tag.java:751)
at org.jaudiotagger.audio.mp3.MP3File.save(MP3File.java:1011)
at org.jaudiotagger.audio.mp3.MP3File.save(MP3File.java:920)
at org.jaudiotagger.audio.mp3.MP3File.commit(MP3File.java:932)
at com.jthink.songkong.analyse.analyser.SongSaver.realSave(SongSaver.java:798)
at com.jthink.songkong.analyse.analyser.SongSaver.saveSongToFile(SongSaver.java:623)
at com.jthink.songkong.analyse.analyser.SongSaver.saveChanges(SongSaver.java:185)
at com.jthink.songkong.analyse.analyser.SongSaver.call(SongSaver.java:160)
at com.jthink.songkong.analyse.analyser.SongSaver.call(SongSaver.java:54)
at java.util.concurrent.FutureTask.run(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
Caused by: java.io.IOException: The specified server cannot perform the requested operation
at sun.nio.ch.FileKey.init(Native Method)
... 21 more
It seems it is returning an Error because file locking is not supported, but it should be returning an IOException, (my code expects an IOException), why should this be
Update
I case relevent it’s a Western Digital MyBook Live 3TBNAS, Firmware version is 02.43.10-048 which running Debian 5.0.4
All code above FileKey.create can throw a IOException so your assumption that this behaviour is not ok is correct.
Indeed this was already filed as bug. In Java 9 the Error will be gone and a IOException will be thrown instead.
Therefore for now you should be prepared to catch the error and evaluate the inner IOException.

java.net.UnknownHostException when using apache http client

I am using HttpClient to connect to some third party url.Its working fine.but some times its giving java.net.UnknownHostException and also connection Reset Excetion. Please help to avoid the exception.
Below is my code snippet.
PostMethod post = null;
try
{
for(int i =0 ; i < 72 ; i++)
{
String url ="http://www.accuweather.com/en/in/amritsar/205593//hourly-weather-forecast/205593?hour=0";
post = new PostMethod(url);
post.setRequestHeader("Content-type", "application/x-www-form-urlencoded;charset=" + "UTF-8");
post.setRequestHeader("Accept-Charset", "UTF-8");
HttpClient httpclient = new HttpClient();
httpclient.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 120000);
httpclient.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, 120000);
result = httpclient.executeMethod(post);
}
}
catch(Exception e){}
finaly
{
post.releaseConnection();
}
Belowe is the exception trace:
java.net.UnknownHostException: www.accuweather.com
at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
at java.net.PlainSocketImpl.connect(Unknown Source)
at java.net.SocksSocketImpl.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.<init>(Unknown Source)
at java.net.Socket.<init>(Unknown Source)
at org.apache.commons.httpclient.protocol.DefaultProtocolSocketFactory.createSocket(DefaultProtocolSocketFactory.java:80)
at org.apache.commons.httpclient.protocol.ControllerThreadSocketFactory$1.doit(ControllerThreadSocketFactory.java:91)
at org.apache.commons.httpclient.protocol.ControllerThreadSocketFactory$SocketTask.run(ControllerThreadSocketFactory.java:158)
at java.lang.Thread.run(Unknown Source)
More than likely, you just have a flaky Internet connection. I'm guessing this is on a mobile phone, in which case flaky Internet connections are a common reality, and you'll just have to deal with it in some way.
Exactly what is the best way to deal with it depends on your program and context, of course. Since you state you run this regularly, one way to deal with it could be to simply ignore an error (catch the exception and just silently abort the attempt if it happens) and then wait for the next regular run. Another option could be to repeat the attempt a set number of times. I'm sure there are other conceivable ways as well.

Java FTP not working inside SwingWorker Thread

I have a simple GUI program that does some interactions with a database then once its completed ftps up some files to a server. This has worked perfectly until I added a SwingWorker thread to keep the GUI responsive.
The code I am using works fine if I put it in its standalone project but inside this project (SwingWorker thread) it gives me the following error:
java.net.SocketException: Software caused connection abort: recv failed
at java.net.SocketInputStream.socketRead0(Native Method)
at java.net.SocketInputStream.read(Unknown Source)
at java.net.SocketInputStream.read(Unknown Source)
at sun.nio.cs.StreamDecoder.readBytes(Unknown Source)
at sun.nio.cs.StreamDecoder.implRead(Unknown Source)
at sun.nio.cs.StreamDecoder.read(Unknown Source)
at sun.nio.cs.StreamDecoder.read0(Unknown Source)
at sun.nio.cs.StreamDecoder.read(Unknown Source)
at java.io.InputStreamReader.read(Unknown Source)
at it.sauronsoftware.ftp4j.NVTASCIIReader.readLine(NVTASCIIReader.java:105)
at it.sauronsoftware.ftp4j.FTPCommunicationChannel.read(FTPCommunicationChannel.java:142)
at it.sauronsoftware.ftp4j.FTPCommunicationChannel.readFTPReply(FTPCommunicationChannel.java:187)
at it.sauronsoftware.ftp4j.FTPClient.openActiveDataTransferChannel(FTPClient.java:3511)
at it.sauronsoftware.ftp4j.FTPClient.openDataTransferChannel(FTPClient.java:3475)
at it.sauronsoftware.ftp4j.FTPClient.upload(FTPClient.java:2641)
at it.sauronsoftware.ftp4j.FTPClient.upload(FTPClient.java:2550)
at it.sauronsoftware.ftp4j.FTPClient.upload(FTPClient.java:2421)
at GUI$MatchFiles.doInBackground(GUI.java:1602)
at GUI$MatchFiles.doInBackground(GUI.java:1)
at javax.swing.SwingWorker$1.call(Unknown Source)
at java.util.concurrent.FutureTask$Sync.innerRun(Unknown Source)
at java.util.concurrent.FutureTask.run(Unknown Source)
at javax.swing.SwingWorker.run(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
Any ideas?
EDIT:
I assume you mean something like this? Please note this is a very trimmed down version, I have removed a lot of superfluous code. I cant see any of it affecting this.
class Worker extends SwingWorker<Integer, Integer>{
protected Integer doInBackground() throws Exception{
FTPClient client = new FTPClient();
client.connect(url);
client.login(username, password);
client.setPassive(false);
client.changeDirectory(uploaddirectory);
client.upload(new File(fileuploadpath));
client.disconnect(true);
}
protected void done() {
System.out.println("Done");
}
}
As said, I take this code inside the swingworker and put it in its own class and it runs perfect.
It looks like your socket is cut-off by your firewall or anti-virus... Try disabling the firewall and anti-virus and re-run your program...

Categories

Resources