Friday, December 14, 2018

Print linux memory every 30 seconds, half a minute

 while true; do cat /proc/meminfo | grep MemFree ; sleep 30;  done

Friday, November 9, 2018

CodecConfigurationException in mongo-java-driver when using $elemMatch in $all


https://stackoverflow.com/questions/53223909/codecconfigurationexception-in-mongo-java-driver-when-using-elemmatch-in-all

https://groups.google.com/forum/#!forum/mongodb-user


Who has the same error and how to fix it?
Java code:
// For simplify code I left one criteria elemMatch in all criteria.
Criteria expectedCriteria = Criteria.where("tags").all(new Criteria().elemMatch(Criteria.where("localization").is("EN")));
String json = expectedCriteria.getCriteriaObject().toJson();



Exception:
Exception in thread "main" org.bson.codecs.configuration.CodecConfigurationException: Can't find a codec for class org.springframework.data.mongodb.core.query.Criteria.
    at org.bson.codecs.configuration.CodecCache.getOrThrow(CodecCache.java:46)
    at org.bson.codecs.configuration.ProvidersCodecRegistry.get(ProvidersCodecRegistry.java:63)
    at org.bson.codecs.configuration.ProvidersCodecRegistry.get(ProvidersCodecRegistry.java:37)
    at org.bson.codecs.DocumentCodec.writeValue(DocumentCodec.java:184)
    at org.bson.codecs.DocumentCodec.writeIterable(DocumentCodec.java:207)
    at org.bson.codecs.DocumentCodec.writeValue(DocumentCodec.java:180)
    at org.bson.codecs.DocumentCodec.writeMap(DocumentCodec.java:199)
    at org.bson.codecs.DocumentCodec.writeValue(DocumentCodec.java:182)
    at org.bson.codecs.DocumentCodec.writeMap(DocumentCodec.java:199)
    at org.bson.codecs.DocumentCodec.encode(DocumentCodec.java:141)
    at org.bson.codecs.DocumentCodec.encode(DocumentCodec.java:45)
    at org.bson.Document.toJson(Document.java:313)
    at org.bson.Document.toJson(Document.java:286)
    at org.bson.Document.toJson(Document.java:273)


Version of mongo-java-driver in reproduce: (in all) - 3.4.3 - 3.5.0 - 3.6.4 - 3.7.1 - 3.8.2 - 3.9.0
Mongo DB script which works for code:
db.getCollection('devices').find({ 
    "tags": {
        "$all": [{
                "$elemMatch": {
                    "key": "localization",
                    "value": {
                        "$eq": "EN"
                    }
                }
            }
        ]
    }
})

Friday, November 2, 2018

mvnrepository links

https://mvnrepository.com/open-source/http-clients
https://mvnrepository.com/open-source/logging-frameworks
https://mvnrepository.com/open-source/core-utilities

all - https://mvnrepository.com/open-source
netty - https://mvnrepository.com/search?q=netty&sort=popular

Tuesday, October 30, 2018

Netty NoClassDefFoundError JdkAlpnApplicationProtocolNegotiator

PROBLEM:
Caused by: java.lang.NoClassDefFoundError: Could not initialize class io.netty.handler.ssl.JdkAlpnApplicationProtocolNegotiator
 at io.netty.handler.ssl.JdkSslContext.toNegotiator(JdkSslContext.java:285)
 at io.netty.handler.ssl.JdkSslClientContext.(JdkSslClientContext.java:261)
 at io.netty.handler.ssl.SslContext.newClientContextInternal(SslContext.java:751)
 at io.netty.handler.ssl.SslContextBuilder.build(SslContextBuilder.java:418)
 at com.relayrides.pushy.apns.ApnsClientBuilder.build(ApnsClientBuilder.java:408)

c.r.p.a.ApnsClientBuilder Native SSL provider not available; will use JDK SSL provider.

OpenSSL available? false
ALPN supported?    false
Reason for unavailability:
java.lang.IllegalArgumentException: Failed to load any of the given libraries: [netty-tcnative-linux-x86_32, netty-tcnative-linux-x86_32-fedora, netty-tcnative]
        at io.netty.util.internal.NativeLibraryLoader.loadFirstAvailable(NativeLibraryLoader.java:178)
        at io.netty.handler.ssl.OpenSsl.loadTcNative(OpenSsl.java:384)
        at io.netty.handler.ssl.OpenSsl.(OpenSsl.java:84)
        at OpenSslZav.main(OpenSslZav.java:7)


JDK vs OPENSSL
new JdkSslClientContext
new OpenSslClientContext

netty-tcnative-boringssl-static-2.0.0.Final.jar

netty-tcnative-boringssl-static-2.0.0.Final.jar\META-INF\native\
libnetty-tcnative-linux-x86_64.so
libnetty-tcnative-osx-x86_64.jnilib
netty-tcnative-windows-x86_64.dll

SOLUTION:
Update Java to arch 64
ONLY WORKS IN JAVA 64 !!!


Monday, October 29, 2018

Wednesday, October 24, 2018

YourKit on remote server

https://www.yourkit.com/docs/java/help/profiling_j2ee_remote.jsp



export PROFILER="-agentpath:/opt/yjp/bin/linux-x86-64/libyjpagent.so=disablestacktelemetry,disableexceptiontelemetry,delay=10000,listen=all"

export JAVA_OPTS="-Xmx3G -Xms3G -Xmn1G -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=/opt/xxx/log/xxx-oom.hpref $GC_OPTS $PROFILER $JMX_OPTS"

Wednesday, October 17, 2018

Docker: Error response from daemon, Exception caught, Bad Request - FOR PROXY USERS


PROBLEM:
C:\Users\alexeyz> docker run hello-world
Unable to find image 'hello-world:latest' locally
docker: Error response from daemon: Get https://registry-1.docker.io/v2/: Bad Request.
See 'docker run --help'.
-OR-
[ERROR] Failed to execute goal com.spotify:docker-maven-plugin:0.4.10:build (myproject.all) on project com.myproject.docker: Exception caught: Get https://registry-1.docker.io/v2/: Bad Request

SOLUTION:
1. Switch off proxy in Docker
2. Find non-proxy internet.


Friday, October 12, 2018

Mongo notes

use admin
db.runCommand( {buildInfo: 1} )
db.adminCommand({setParameter: 1, internalQueryExecMaxBlockingSortBytes:67108864})

Friday, October 5, 2018

Mask credit card number in java


String result;
if (string.length() > 4) {
result = string.replaceAll("(?<=.{4})(\\w)", "*");
} else {
result = string.replaceAll("(\\w)", "*");
}


Friday, July 13, 2018

How to include class name property when serializing json

import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.TypeAdapter;
import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonWriter;

/**
 * Only for List type fields.
 * @author alexeyz
 */
public class RuntimeClassNameTypeAdapter extends TypeAdapter> {

private static final String CLASS_NAME_PROPERTY = "type";

@Override
public void write(JsonWriter out, List src) throws IOException {
if (src == null) {out.nullValue();return;}

Gson gson = new Gson();
out.beginArray();
for (Object srcBlocker : src) {
        JsonElement serializedBlocker = gson.toJsonTree(srcBlocker);
        JsonObject blocker = (JsonObject) serializedBlocker;
        blocker.addProperty(CLASS_NAME_PROPERTY, srcBlocker.getClass().getSimpleName());
        out.jsonValue(blocker.toString());
}
out.endArray();
}



Usage:
    @JsonAdapter(RuntimeClassNameTypeAdapter.class)

    protected List book;


Monday, May 7, 2018

Http java links

https://www.programcreek.com/java-api-examples/?api=io.reactivex.netty.protocol.http.client.HttpClient

https://blogs.oracle.com/java/jdk-http-client#synchronousGet

http://www.baeldung.com/java-http-request

https://stackoverflow.com/questions/46387896/how-can-i-use-http-client-apisince-java-9-in-java8-project

https://www.mkyong.com/java/how-to-send-http-request-getpost-in-java/

http://openjdk.java.net/groups/net/httpclient/intro.html

https://alvinalexander.com/java/java-apache-httpclient-restful-client-examples

https://github.com/timboudreau/netty-http-client

https://github.com/cloudbow/netty-http-client

TODO mongodb query toJson



Operator $all produces error on toJson() method.

http://api.mongodb.com/java/current/org/bson/codecs/package-summary.html

https://mvnrepository.com/artifact/org.mongodb/mongo-java-driver

http://api.mongodb.com/java/3.0/?com/mongodb/client/model/Filters.html

http://mongodb.github.io/mongo-java-driver/3.4/bson/codecs/


Criteria criteria = Criteria.where("props").all(
new Criteria().elemMatch(Criteria.where("key").is("lang").and("value").is("EN")),
new Criteria().elemMatch(Criteria.where("key").is("photo").and("value").exists(true)));
String json = criteria.getCriteriaObject().toJson();



org.bson.codecs.configuration.CodecConfigurationException: Can't find a codec for class org.springframework.data.mongodb.core.query.Criteria.
at org.bson.codecs.configuration.CodecCache.getOrThrow(CodecCache.java:46)
at org.bson.codecs.configuration.ProvidersCodecRegistry.get(ProvidersCodecRegistry.java:63)
at org.bson.codecs.configuration.ProvidersCodecRegistry.get(ProvidersCodecRegistry.java:37)
at org.bson.codecs.DocumentCodec.writeValue(DocumentCodec.java:184)


Documentation: $all with $elemMatch

If the field contains an array of documents, you can use the $all with the $elemMatch operator.
The following operation queries the inventory collection for documents where the value of the qty field is an array whose elements match the $elemMatch criteria:
db.inventory.find( {
                     qty: { $all: [
                                    { "$elemMatch" : { size: "M", num: { $gt: 50} } },
                                    { "$elemMatch" : { num : 100, color: "green" } }
                                  ] }
                   } )

Thursday, May 3, 2018

Count how much unique field values in mongo

db.getCollection('myCollection').aggregate( 
    {$group: {"_id":"$username"}},
    {$group: {"_id":1, count:{$sum:1}}
})

db.getCollection('myCollection').aggregate( [
    {$match: { "_id.appId":"23893259736", "status" : 1 } },
    {$group: {"_id":"$_id.deviceId"}},
    {$group: {"_id":1, count:{$sum:1}}}

])


Criteria criteria = SearchQueryBuilder.parse(query);
Aggregation aggregation = Aggregation.newAggregation(
Aggregation.match(criteria),
Aggregation.group("username"),
Aggregation.group("username").count().as("count")
);
try {
AggregationResults aggregationResults = mongoTemplate.aggregate(aggregation, Devices.class, GroupAndCount.class);
GroupAndCount cbg = aggregationResults.getMappedResults().iterator().next();
return cbg.count;
} catch (Exception e) {
return 0;
}

private static final class GroupAndCount {
@Id
private Integer _id;
private Integer count;
}

Wednesday, April 25, 2018

Java 8 - create array ))

Criteria[] cc = ((IntFunction)Criteria[]::new).apply(0);

Tuesday, February 27, 2018

BigDecimal to String :)

public static void main(String[] args) {
double d = 10.00001;
BigDecimal bigDecimal = new BigDecimal(d);
System.out.println(bigDecimal);
// result: 10.000009999999999621422830387018620967864990234375
System.out.println(bigDecimal.toPlainString());
// result: 10.000009999999999621422830387018620967864990234375
System.out.println(bigDecimal.toString());
// result: 10.000009999999999621422830387018620967864990234375
System.out.println(bigDecimal.stripTrailingZeros().toString());
// result: 10.000009999999999621422830387018620967864990234375
System.out.println(bigDecimal.stripTrailingZeros().toPlainString());
// result: 10.000009999999999621422830387018620967864990234375

DecimalFormat df = new DecimalFormat();
df.setMaximumFractionDigits(2);
df.setMinimumFractionDigits(5);
df.setGroupingUsed(false);
String format = df.format(bigDecimal);
System.out.println(format);
// result: 10.00001
}


public static final String FORMAT_DEFAULT = "#0.00";
public static final NumberFormat formatter = new DecimalFormat(FORMAT_DEFAULT);

public static final String FormatDefault = "0.##";
public static final NumberFormat Formatter = new DecimalFormat(FormatDefault);

public static void main(String[] args) {

double d = 1000000.00001;
BigDecimal bigDecimal = new BigDecimal(d);
System.out.println(bigDecimal);

String format1 = formatter.format(d);
System.out.println(format1);

String format2 = Formatter.format(d);
System.out.println(format2);

}