Wednesday, March 4, 2020

Java clone – SerializationUtils



pom.xml
<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-lang3</artifactId>
    <version>3.7</version>
</dependency>

SerializationUtils example
Object1 cloned = (Object1) org.apache.commons.lang.SerializationUtils.clone(object);

Monday, January 13, 2020

Java Generics Example

private <E> E getUpdatedObject(E oldItem, E newItem) {
    Gson gson = new Gson();
    Type type = new TypeToken<E>() {
    }.getType();
    E item = null;
    if (Objects.isNull(oldItem) && Objects.nonNull(newItem)) {
        item = newItem;
    } else if (Objects.nonNull(oldItem) && Objects.isNull(newItem)) {
        item = oldItem;
    } else {
        try {
            ObjectMapper objectMapper = new ObjectMapper();
            
            String elements = gson.toJson(newItem, type);
            item = objectMapper.readerForUpdating(oldItem).readValue(elements);
        } catch (IOException e) {
            LOGGER.error("error updating object: {}", e);
        }
    }
    return item;
}

Friday, February 8, 2019

Docker remove/compose and push an image


mvn clean install -DskipTests to build the executable jar file to be dockerized.

docker-compose.yml

version: "2" 
services:
    camunda-demo:
        container_name: ordermanager
        build:
            context: ./
            args:
                JAR_FILE: target/ordermanager-0.0.1-SNAPSHOT.jar
                PORT: 8080
            dockerfile: Dockerfile
        image: mschassisdial/camunda-demo:dev
        ports:
            - 8080:8080
        networks:
            - camunda-network
networks:

    camunda-network:
        driver: bridge
 
 
Dockerfile 

FROM openjdk:8-jdk
VOLUME /tmp
ARG JAR_FILEARG PORTARG CONFIGCOPY ${JAR_FILE} app.jar
COPY ${CONFIG} config.properties
ENTRYPOINT ["java","-jar","/app.jar"]
EXPOSE ${PORT}

docker images

REPOSITORY                                 TAG                 IMAGE ID            CREATED             SIZE
mschassisdial/camunda-demo         dev                 b1a0d11f8a8d        3 minutes ago       693MB
mschassisdial/external-service        dev                 d496ef00abfd        2 hours ago           680MB
mschassisdial/ordermanager       dev                 a37c119aa03c        2 hours ago          693MB
mschassisdial/external-service         qa                  b85c41a54ef6        19 hours ago        680MB
mschassisdial/external-service         <none>              748115d11fb6    20 hours ago       680MB
dialcamundabpm/ordermanager   latest              1052ba8d4e08       20 hours ago       693MB
mschassisdial/camunda-demo         qa                  1052ba8d4e08        20 hours ago        693MB
mschassisdial/camunda-demo         <none>              a3e6620465b0        7 days ago        695MB

chown -R admin:admin .

docker image rm mschassisdial/camunda-demo:dev
docker-compose build
docker push mschassisdial/camunda-demo:dev

Thursday, February 7, 2019

Java Multithreading - Threadsafe Counter

https://stackoverflow.com/questions/29883719/java-multithreading-threadsafe-counter

The traditional way to achieve thread synchronization in Java is by the use of synchronized keyword. While it provides a certain basic synchronization, the synchronized keyword is quite rigid in its use. For example, a thread can take a lock only once. Synchronized blocks don’t offer any mechanism of a waiting queue and after the exit of one thread, any thread can take the lock. This could lead to starvation of resources for some other thread for a very long period of time.
Reentrant Locks are provided in Java to provide synchronization with greater flexibility.

What are Reentrant Locks?
The ReentrantLock class implements the Lock interface and provides synchronization to methods while accessing shared resources. The code which manipulates the shared resource is surrounded by calls to lock and unlock method. This gives a lock to the current working thread and blocks all other threads which are trying to take a lock on the shared resource.


public class ThreadsExample implements Runnable {
    static int counter = 1; // a global counter

    static ReentrantLock counterLock = new ReentrantLock(true); // enable fairness policy

    static void incrementCounter(){
        counterLock.lock();

        // Always good practice to enclose locks in a try-finally block
        try{
            System.out.println(Thread.currentThread().getName() + ": " + counter);
            counter++;
        }finally{
             counterLock.unlock();
        }
     }

    @Override
    public void run() {
        while(counter<1000){
            incrementCounter();
        }
    }

    public static void main(String[] args) {
        ThreadsExample te = new ThreadsExample();
        Thread thread1 = new Thread(te);
        Thread thread2 = new Thread(te);

        thread1.start();
        thread2.start();          
    }
}

Tuesday, February 5, 2019

Setting up a Home VPN Server Using Your Raspberry Pi

https://www.sitepoint.com/setting-up-a-home-vpn-using-your-raspberry-pi/

https://www.noip.com/support/knowledgebase/install-ip-duc-onto-raspberry-pi/

When are connections returned to the connection pool with Spring JPA (Hibernate) Entity Manager?



https://stackoverflow.com/questions/27486104/when-are-connections-returned-to-the-connection-pool-with-spring-jpa-hibernate

https://vladmihalcea.com/a-beginners-guide-to-transaction-isolation-levels-in-enterprise-java/

https://vladmihalcea.com/the-anatomy-of-connection-pooling/

Monday, January 28, 2019

Erase and Install firmware to Ruizu X02 MP3 player from Windows 7/10 Software

Hi Everyone,

Today I faced an issue of my Ruizu X02 Mp3 player malfunctioning due to a virus. It had Hungarian words in the welcome screen and it froze on that screen. So I decided to flash the player using the official firmware.

I searched in the internet and was able to find the http://www.ruizutek.com/manuals-firmware.html
site which contained the Ruizu X02 firmware.

Then I searched a software to flash the mp3 player from my windows 10 system. It took a while because some of the software were outdated and couldn't run on windows 10 as well as some of the software link locations returned 404.
Finally I found this http://www.sweetpeatoyco.com site to download a software called Audio Product Tool 1.03_01.


 Then I followed this http://www.agptek.com/forum/viewtopic.php?f=103&p=68#p68 forum to install the firmware on X02. As I have learned this agptek site is the counter part of ruizu company in Europe. Ruizu is sold mainly in China.

That blog was written nicely and I was able to troubleshoot and install the firmware in X02 successfully.
First when I connected the player to usb slot using a usb cable software didn't detect my player. So I was so frustrated and almost gave up until I saw the 4th step.
It says,
Note:
If your player failed to be recognized or updated, please connect it to your computer the way below:
① For those player designed with soft power button:
Long press the power button to turn off the player
Plug the bigger end of the USB cable into the computer interface first
Then hold down the power button and plug the other end of the USB cable into the player at the same time.

I did exactly the same and tadaahh it detected my player.
It took around 10 mins to install so be patient until it shows successful installation.