Jolokia client from eclipse throw null pointer exception

classic Classic list List threaded Threaded
2 messages Options
pranavg123@gmail.com pranavg123@gmail.com
Reply | Threaded
Open this post in threaded view
|

Jolokia client from eclipse throw null pointer exception

I have configured jolokia on websphere and its running fine and when I hit the url on browser it works fine as required.

Output-
{"request":{"type":"version"},"value":{"agent":"1.6.2","protocol":"7.2","config":{"listenForHttpService":"true","maxCollectionSize":"0","authIgnoreCerts":"false","agentId":"160.70.20.103-2580-51814e9d-servlet","agentType":"servlet","policyLocation":"classpath:\/jolokia-access.xml","agentContext":"\/jolokia","mimeType":"text\/plain","discoveryEnabled":"false","streaming":"true","historyMaxEntries":"10","allowDnsReverseLookup":"true","maxObjects":"0","debug":"false","serializeException":"false","detectorOptions":"{}","dispatcherClasses":"org.jolokia.http.Jsr160ProxyNotEnabledByDefaultAnymoreDispatcher","maxDepth":"15","authMode":"basic","authMatch":"any","canonicalNaming":"true","allowErrorDetails":"true","realm":"jolokia","includeStackTrace":"true","useRestrictorService":"false","debugMaxEntries":"100"},"info":{"product":"websphere","vendor":"IBM","version":"7.1.3.40","extraInfo":{"buildDate":"7\/20\/16"}}},"timestamp":1566303030,"status":200}


But when I hit the same url from eclipse it throw null pointer exception.


My eclipse code -
import org.jolokia.client.*;
import org.jolokia.client.exception.J4pException;
import org.jolokia.client.request.*;
import java.util.Map;
import org.apache.http.*;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import javax.management.MalformedObjectNameException;
@SuppressWarnings("deprecation")
public class jolokiaDemo {

        /**
         * @param args
         * @throws MalformedObjectNameException
         * @throws J4pException
         */
        public static void main(String[] args) throws MalformedObjectNameException, J4pException
        {
                new J4pClient("https:/.../:9443/jolokia");
                System.out.println("----");
                J4pClient j4pClient = J4pClient.user("gan").password("***")
                .authenticator(new BasicAuthenticator().preemptive())
                .connectionTimeout(3000000)
                .build();
                System.out.println("----"+j4pClient);
        J4pReadResponse response = null;
        try {  
       response = j4pClient.execute(new J4pReadRequest("java.lang:type=Memory","HeapMemoryUsage","used"),"POST");
       } catch (J4pException e) {
       // TODO Auto-generated catch block
       e.printStackTrace();
       }
           
        System.out.println("Memory used: " + response.getValue());
     }

error -
Exception in thread "main" java.lang.NullPointerException
        at java.net.URI$Parser.parse(URI.java:3023)
        at java.net.URI.<init>(URI.java:595)
        at org.jolokia.client.request.J4pRequestHandler.<init>(J4pRequestHandler.java:58)
        at org.jolokia.client.J4pClient.<init>(J4pClient.java:91)
        at org.jolokia.client.J4pClientBuilder.build(J4pClientBuilder.java:415)
        at com.ibm.jolokia.jolokiaDemo.main(jolokiaDemo.java:37)
pranavg123@gmail.com pranavg123@gmail.com
Reply | Threaded
Open this post in threaded view
|

Re: Jolokia client from eclipse throw null pointer exception

Used code in addition to above code, simply called method from above code and accepted the return value and it worked like charm.
 public static J4pClient createJolokiaClient(String jolokiaUrl, String username, String password) {
        J4pClientBuilder builder = J4pClientBuilderFactory.url(jolokiaUrl);
        boolean auth = false;
        if (isNotEmpty(username)) {
            builder = builder.user(username);
            auth = true;
        }
        if (isNotEmpty(password)) {
            builder = builder.password(password);
            auth = true;
        }
        if (auth) {
            builder = builder.authenticator(new BasicAuthenticator(true));
        }
        return builder.build();
    }

    private static boolean isNotEmpty(String text) {
        return text != null && !text.isEmpty();
    }