I've recently made the switch from elasticsearch 1.7 to 2.0 and I noticed the way you setup the client has changed. I went through the documentation and for some reason the client is always null. I was wondering if I have set it up correctly.
Here is my code:
Client client = null;
try {
client = TransportClient.builder().build().addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("localhost"), 9300));
} catch (Exception e) {
Logger.log(e);
} finally {
client.close();
try {
conn.close();
} catch (SQLException e) {
Logger.log(e);
}
}
As noted in the comments, but a little bit more in detail: Elasticsearch 2.0 uses Guava 18.0 (see https://github.com/elastic/elasticsearch/pull/7593). So to fix errors like java.lang.NoSuchMethodError: com.google.common.util.concurrent.MoreExecutors.directExecutor()Ljava/util/concu‌rrent/Executor;, make sure to use Guava 18.0 as dependency and not other versions.
Related
I am trying to get the archived messages using below code as described in this document
try {
MamManager mamManager = MamManager.getInstanceFor(connection);
boolean isSupported = mamManager.isSupportedByServer();
if (isSupported) {
MamManager.MamQueryResult mamQueryResult = mamManager.queryArchive(500);
List<Forwarded> forwardedMessages = mamQueryResult.forwardedMessages;
Forwarded d = forwardedMessages.get(0);
}
} catch (Exception e) {
e.printStackTrace();
}
But it's throwing org.jivesoftware.smack.XMPPException$XMPPErrorException: XMPPError: feature-not-implemented - cancel exception on executing queryArchive() function. Does anyone have any idea how to resolve this issue? Any help would be appreciated.
This issue is resolved now after updating openfire server to latest version (4.1.5)
I use NCache java API for connecting to NCache server but it throws an exception:
com.alachisoft.ncache.runtime.exceptions.GeneralFailureException: No server is available to process the request.
I also tried some suggestion from this site
but cannot solve the problem.
Here are the java codes.
String cacheId = "mycache";
try {
cache = NCache.initializeCache(cacheId);
} catch (Exception ex) {
ex.printStackTrace();
log.log(Level.SEVERE, "NCacheConnection()", ex.getMessage());
}
As per my understanding, I want to follow the best practice for releasing the resources at the end to prevent any connection leaks. Here is my code in HelperClass.
public static DynamoDB getDynamoDBConnection()
{
try
{
dynamoDB = new DynamoDB(new AmazonDynamoDBClient(new ProfileCredentialsProvider()));
}
catch(AmazonServiceException ase)
{
//ase.printStackTrace();
slf4jLogger.error(ase.getMessage());
slf4jLogger.error(ase.getStackTrace());
slf4jLogger.error(ase);
}
catch (Exception e)
{
slf4jLogger.error(e);
slf4jLogger.error(e.getStackTrace());
slf4jLogger.error(e.getMessage());
}
finally
{
dynamoDB.shutdown();
}
return dynamoDB;
}
My doubt is, since the finally block will be executed no matter what, will the dynamoDB returns empty connection because it will be closed in finally block and then execute the return statement? TIA.
Your understanding is correct. dynamoBD.shutdown() will always execute before return dynamoDB.
I'm not familiar with the framework you're working with, but I would probably organize the code as follows:
public static DynamoDB getDynamoDBConnection()
throws ApplicationSpecificException {
try {
return new DynamoDB(new AmazonDynamoDBClient(
new ProfileCredentialsProvider()));
} catch(AmazonServiceException ase) {
slf4jLogger.error(ase.getMessage());
slf4jLogger.error(ase.getStackTrace());
slf4jLogger.error(ase);
throw new ApplicationSpecificException("some good message", ase);
}
}
and use it as
DynamoDB con = null;
try {
con = getDynamoDBConnection();
// Do whatever you need to do with con
} catch (ApplicationSpecificException e) {
// deal with it gracefully
} finally {
if (con != null)
con.shutdown();
}
You could also create an AutoCloseable wrapper for your dynamoDB connection (that calls shutdown inside close) and do
try (DynamoDB con = getDynamoDBConnection()) {
// Do whatever you need to do with con
} catch (ApplicationSpecificException e) {
// deal with it gracefully
}
Yes,dynamoDB will return an empty connection as dynamoBD.shutdow() will be executed before return statement, Always.
Although I am not answering your question about the finally block being executed always (there are several answers to that question already), I would like to share some information about how DynamoDB clients are expected to be used.
The DynamoDB client is a thread-safe object and is intended to be shared between multiple threads - you can create a global one for your application and re-use the object where ever you need it. Generally, the client creation is managed by some sort of IoC container (Spring IoC container for example) and then provided by the container to whatever code needs it through dependency injection.
Underneath the hood, the DynamoDB client maintains a pool of HTTP connections for communicating the DynamoDB endpoint and uses connections from within this pool. The various parameters of the pool can be configured by passing an instance of the ClientConfiguration object when constructing the client. For example, one of the parameters is the maximum number of open HTTP connections allowed.
With the above understanding, I would say that since the DynamoDB client manages the lifecycle of HTTP connections, resource leaks shouldn't really be concern of code that uses the DynamoDB client.
How about we "imitate" the error and see what happens ? This is what I mean:
___Case 1___
try{
// dynamoDB = new DynamoDB(new AmazonDynamoDBClient(new ProfileCredentialsProvider()));
throw new AmazonServiceException("Whatever parameters required to instantiate this exception");
} catch(AmazonServiceException ase)
{
//ase.printStackTrace();
slf4jLogger.error(ase.getMessage());
slf4jLogger.error(ase.getStackTrace());
slf4jLogger.error(ase);
}
catch (Exception e)
{
slf4jLogger.error(e);
slf4jLogger.error(e.getStackTrace());
slf4jLogger.error(e.getMessage());
}
finally
{
//dynamoDB.shutdown();
slf4jLogger.info("Database gracefully shutdowned");
}
___Case 2___
try{
// dynamoDB = new DynamoDB(new AmazonDynamoDBClient(new ProfileCredentialsProvider()));
throw new Exception("Whatever parameters required to instantiate this exception");
} catch(AmazonServiceException ase)
{
//ase.printStackTrace();
slf4jLogger.error(ase.getMessage());
slf4jLogger.error(ase.getStackTrace());
slf4jLogger.error(ase);
}
catch (Exception e)
{
slf4jLogger.error(e);
slf4jLogger.error(e.getStackTrace());
slf4jLogger.error(e.getMessage());
}
finally
{
//dynamoDB.shutdown();
slf4jLogger.info("Database gracefully shutdowned");
}
These exercise could be a perfect place to use unit tests and more specifically mock tests. I suggest you to take a close look at JMockit, which will help you write such tests much more easily.
I have recently upgraded opensaml dependency from 2.5.3 to 2.6.1 and xmlutil from
1.3.0 to 1.4.1.
It compiles without any errors but while running the application i get the following exception:
java.lang.NullPointerException
org.opensaml.xml.io.AbstractXMLObjectMarshaller.marshall(AbstractXMLObjectMarshaller.java:84)
Calling Code:
final MarshallerFactory marshallerFac = SAMLUtil.getMarshallerFactory();
final org.opensaml.xml.io.Marshaller authnStatementMarshaller = marshallerFac.getMarshaller(assertion);
Element assertionElement = null;
try {
assertionElement = authnStatementMarshaller.marshall(assertion);
try {
// Sign assertion and query signature
Signer.signObject(signature);
}
catch (final SignatureException e) {
LOGGER.error("Fout opgetreden bij ondertekenen Assertion", e);
}
}
I've noticed that if you don't initialize ("bootstrap") the SAML configuration, you get a NullPointerException (rather unhelpfully, I might add) when you try to construct the SAML.
import org.opensaml.DefaultBootstrap;
import org.opensaml.xml.ConfigurationException;
try {
DefaultBootstrap.bootstrap();
}
catch (ConfigurationException ce) {
}
The above is just a snippet of code to illustrate what I'm talking about. Did you maybe forget to bootstrap the configuration? That has to be done before you do anything.
I want to use the latest release of smack 4.0.0 in my project for xmpp with android studio 0.6.0,the code as below:
try{
ConnectionConfiguration config = new ConnectionConfiguration("wooxonline.com",4000);
XMPPConnection connection = new XMPPTCPConnection(config);
connection.connect();
connection.login("cliff","cliff123");
}
catch (SmackException e) {
e.printStackTrace();
}
catch (java.io.IOException e)
{
e.printStackTrace();
}
catch (XMPPException e) {
e.printStackTrace();
Log.i(TAG, "login failed!");
}
this almost like sample with the smack sample code,but I have a compile issue that can't be solved as below trace info:
Error:(58, 29) error: cannot access SaslException
class file for javax.security.sasl.SaslException not found
I'm stuck here and anyone can help to have a look?
I ran into the same problem. This is the appropriate import sentence:
import org.apache.harmony.javax.security.sasl.SaslException;
According to the official forum "Smack does not work on Android. You need aSmack." So you should try to change the library to ASmack.
You can use maven to get the jar or download it from here
Reference:https://community.igniterealtime.org/thread/52833