Andrea Girardi - It's my blog!

Category: IT Stuff (page 1 of 8)

Warning about SSL connection when connecting to MySQL database

After a recent update of mySql, I get this warning:

WARN: Establishing SSL connection without server's identity verification is not recommended. 
According to MySQL 5.5.45+, 5.6.26+ and 5.7.6+ requirements SSL connection must be established 
by default if explicit option isn't set. For compliance with existing applications not using SSL 
the verifyServerCertificate property is set to 'false'. You need either to explicitly disable SSL 
by setting useSSL=false, or set useSSL=true and provide truststore for server certificate verification.

To disable disable SSL and also suppress the SSL warning, it’s possible to set to false the useSSL parameter on the connection string:

jdbc:mysql://localhost:3306/myDb?autoReconnect=true&useSSL=false

On applicationContext, something like this:

<!--  ############ MY SQL DATABASE SECTION ############ -->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">	
    <property name="driverClassName"><value>com.mysql.jdbc.Driver</value></property>
    <property name="url">
        <value>jdbc:mysql://${mysql.hostname}:${mysql.port}/${mysql.db}?useSSL=false</value>
    </property>
    <property name="username"><value>${mysql.user}</value></property>
    <property name="password"><value>${mysql.password}</value></property>
</bean>

Convert timestamp long to normal date format

One simply way to convert a Long time stamp into a formatted string is (time paramter is Long timestamp):

Date date = new Date(time);
Format format = new SimpleDateFormat("yyyy MM dd HH:mm:ss");
return format.format(date);

These packages must be included.

import java.sql.Date;
import java.text.Format;
import java.text.SimpleDateFormat;

Date and Time Patterns

Date and time formats are specified by date and time pattern strings. Within date and time pattern strings, unquoted letters from 'A' to 'Z' and from 'a' to 'z' are interpreted as pattern letters representing the components of a date or time string. Text can be quoted using single quotes (') to avoid interpretation. "''" represents a single quote.
All other characters are not interpreted; they’re simply copied into the output string during formatting or matched against the input string during parsing.

The following pattern letters are defined (all other characters from 'A' to 'Z' and from 'a' to 'z' are reserved):

Letter Date or Time Component Presentation Examples
G Era designator Text AD
y Year Year 1996; 96
M Month in year Month July; Jul; 07
w Week in year Number 27
W Week in month Number 2
D Day in year Number 189
d Day in month Number 10
F Day of week in month Number 2
E Day in week Text Tuesday; Tue
a Am/pm marker Text PM
H Hour in day (0-23) Number 0
k Hour in day (1-24) Number 24
K Hour in am/pm (0-11) Number 0
h Hour in am/pm (1-12) Number 12
m Minute in hour Number 30
s Second in minute Number 55
S Millisecond Number 978
z Time zone General time zone Pacific Standard Time; PST; GMT-08:00
Z Time zone RFC 822 time zone -0800

Date based query using milliseconds (Java long) time on MongoDB

Let’s suppose I need to search all records that match a date condition. On MongoDB I’ve a bunch of data like this:

{
    "_id" : "9ed3b937-0f43-4613-bd58-cb739a8c5bf6",
    "userModels" : {
        "5080" : {
            "generated_date_timestamp" : NumberLong(1413382499442),
            "model_id" : 5080,
        },
    }
    "values" : {}
}

This is the query:

db.anonProfile.find({ 
   "userModels.5080.generated_date_timestamp" : { 
      "$gte" : ISODate("2013-10-01T00:00:00.000Z").getTime() 
   }
});
.getTime()

allows to translate ISODate into a NumberLong timestamp.

Gap di competenze per Industria 4.0

«Non vedo un rischio di disoccupazione maggiore provocato dalle tecnologie, il saldo non sarà negativo», spiega ancora l’economista Ocse che però vede una minaccia nell’aumento delle diseguaglianze – «sia come stipendi che come prospettive di carriera» – tra chi ha competenze adeguate e chi no: «I lavori intermedi già negli ultimi venti anni sono stati colpiti, ma negli ultimi anni questo processo si è velocizzato».

Gap di competenze per Industria 4.0

Allarme Ocse: in Italia un lavoratore su due ha scarsissime o nulle conoscenze in ambito Ict

Source: www.ilsole24ore.com/art/impresa-e-territori/2016-11-19/gap-competenze-industria-40-171518.shtml?uuid=AD3EaMyB

The Noble Art of Maintenance Programming

Yes, I am a big fan of code readability and refactoring. I always have in mind this:

Everyone knows that debugging is twice as hard as writing a program in the first place. So if you’re as clever as you can be when you write it, how will you ever debug it? – Brian Kernighan

Refactoring is a big challenge, you must be twice smarter of the developer who debugged is code (and the challenge of rewriting other people’s code makes it fun as hell).

Read this: The Noble Art of Maintenance Programming

Delete Files Older Than x Days on Linux

Command Syntax

find /folder/files* -mtime +15 -exec rm {} \;

The first argument is the path to the files. This can be a path, a directory, or a wildcard as in the example above. The second argument, -mtime, is used to specify the number of days old that the file is. If you enter +15, it will find files older than 15 days. The third argument, -exec, allows you to pass in a command such as rm. The {} \; at the end is required to end the command.

Another way to do it is:

/bin/rm `find /var/tmp/stuff -mtime +15 -print`

How do I fix a 65535 bytes limit Stacktrace?

It is possible that after an upgrade you may encounter this error on some of the more complex pages and root cause provided by tomcat console is :

Unable to compile class for JSP
The code of method _jspService(HttpServletRequest, HttpServletResponse) is exceeding the 65535 bytes limit

To solve the issue you need to locate the file [Tomcat_Home]/conf/web.xml and search the file for ‘JspServlet’. This should return an xml node of containing some values. You will need to add an additional the same as the below.

<init-param>
     <param-name>mappedfile</param-name>
     <param-value>false</param-value>
</init-param>

The resulting block of the web.xml file, once you have inserted the above, should look like the code below.

<servlet>
	<servlet-name>jsp</servlet-name>
	<servlet-class>org.apache.jasper.servlet.JspServlet</servlet-class>
	<init-param>
	    <param-name>fork</param-name>
	    <param-value>false</param-value>
	</init-param>
	<init-param>
	    <param-name>xpoweredBy</param-name>
	    <param-value>false</param-value>
	</init-param>
	<init-param>
	     <param-name>mappedfile</param-name>
	     <param-value>false</param-value>
	</init-param>         
	<load-on-startup>3</load-on-startup>
</servlet>

Save the file and restart the Tomcat service.

How to kill all processes with a given (partial) name?

Usually I open more than on instance of tail -f to monitor my application, so, to kill all with one single command I can use this:

ps -ef | grep tail | grep -v grep | awk '{print $2}' | xargs kill -9

MappingJacksonHttpMessageConverter not found

I was trying to run a java batch that call an application context without success (it’s a java app that calls a Camel spring context). This is what I get during the startup:

ERROR ApplicationProperties @ addApplicationProperty [28] org.springframework.beans.factory.BeanDefinitionStoreException: Unexpected exception parsing XML document from file [/MessageRouting/src/test/resources/META-INF/spring/LOCALHOST-db-context.xml]; nested exception is java.lang.NoClassDefFoundError: org/springframework/http/converter/json/MappingJacksonHttpMessageConverter
Fatal error! java.lang.RuntimeException: Error loading ClassPathXmlApplicationContext file - src/test/resources/META-INF/spring/LOCALHOST-db-context.xml

I’m using spring 4.2.3 (updated yesterday, probably the reason for which it doesn’t work. It was 4.0.9). I know that MappingJacksonHttpMessageConverter has been replaced by MappingJackson2HttpMessageConverter but, how can I tell to spring to use the new version???

I followed some suggestion and I added this to my pom.xml:

<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-core</artifactId>
    <version>2.6.3</version>
</dependency>
<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>2.6.3</version>
</dependency>   
<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-annotations</artifactId>
    <version>2.6.3</version>
</dependency>

But this didn’t fix so, I finally resolved the issue adding this dependency to my pom.xml:

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-webmvc</artifactId>
    <version>${org.springframework-version}</version>
</dependency>

Which log4j configuration are you using?

Sometimes log4j is not working properly and you need to verify where it is actually writing the log entries. This can be easily verified turning on the log4j.debug:

-Dlog4j.debug

It will print to System.out lots of helpful information about which file it used to initialize itself, which loggers / appenders got configured and how etc.

Olderposts

Copyright © 2017 Andrea Girardi – It's my blog!

Theme by Anders NorenUp ↑