Thursday, August 20, 2015

Monday, August 17, 2015

Make Virtualbox display full Linux client


References:
https://help.ubuntu.com/community/VirtualBox/GuestAdditions
http://www.binarytides.com/vbox-guest-additions-ubuntu-14-04/

Also:  Try going to Software and Updates -> Additional Drivers and choose: Use x86 visualization solution

After all remember choosing: View -> Auto-resize Guest Display

Batch insert with JDBC and Spring

JDBC:

String sql = "insert into employee (name, city, phone) values (?, ?, ?)";
Connection connection = new getConnection();
PreparedStatement ps = connection.prepareStatement(sql);
final int batchSize = 1000;
int count = 0;
for (Employee employee: employees) {
     ps.setString(1, employee.getName());
     ps.setString(2, employee.getCity());
     ps.setString(3, employee.getPhone());
     ps.addBatch();

     if(++count % batchSize == 0) {
          ps.executeBatch();
     }
}
ps.executeBatch(); // insert remaining records

ps.close();
connection.close();

Spring Data:

public class EmployeeRepository extends JdbcDaoSupport { 

    public void insertEmployeeBatch(List<Employee> employees) {

        try {
            String sql = "
insert into employee (name, city, phone) values (?, ?, ?)";

            getJdbcTemplate().batchUpdate(sql, new BatchPreparedStatementSetter() {
                public void setValues(PreparedStatement ps, int i) throws SQLException {
                     Employee employee = employees.get(i);                    

                     ps.setString(1, employee.getName());
                     ps.setString(2, employee.getCity());
                     ps.setString(3, employee.getPhone());
                }

                public int getBatchSize() {
                    return
employees.size();
                }
            });
        } catch (Exception ex) {
            LOGGER.error(ex.getMessage(), ex);
        }
    } 

}

Thursday, August 13, 2015

Read big XML file in Java

Tool:

LTFViewer

Maven:

<dependency>
        <groupId>org.codehaus.woodstox</groupId>
        <artifactId>stax2-api</artifactId>
        <version>3.1.2</version>
</dependency>

Java:

public void parseFile(String filename, Long jobId) throws Exception {
        XMLStreamReader2 xmlr = null;
        FileInputStream is = null;
        List<Future> trackingTask = null;

        try {
            XMLInputFactory2 xmlif = ((XMLInputFactory2) XMLInputFactory.newInstance());
            xmlif.configureForSpeed();

            // Start init executor service
            trackingTask = initExecutorService();

            is = new FileInputStream(filename);
            xmlr = (XMLStreamReader2) xmlif.createXMLStreamReader(is);

            // Parse into typed objects
            JAXBContext ctx = JAXBContext.newInstance(PGWSSchedulePOJO.class, PGWSChannelPOJO.class...);
            Unmarshaller um = ctx.createUnmarshaller();
            while (xmlr.hasNext()) {
                xmlr.next();
                if (xmlr.isStartElement()) {
                    if ((xmlr.getLocalName().equals("publishedTitles"))) {
                        NlpgwsTitleInfoPOJO ti = um.unmarshal(xmlr, NlpgwsTitleInfoPOJO.class).getValue();
                        if (ti != null) {
                            //Add to queue "parsedTitleInfo"
                            parsedTitleInfo.add(ti);
                        }
                    } else if ((xmlr.getLocalName().equals("providers"))) {
                        NlpgwsProviderPOJO pr = um.unmarshal(xmlr, NlpgwsProviderPOJO.class).getValue();
                        if (pr != null) {
                            //Add to queue "parsedProviders"
                            parsedProviders.add(pr);
                        }
                    }
                }
            }

        } catch (XMLStreamException ex) {
            LOGGER.error(ex.getMessage(), ex);
            throw ex;
        } catch (Exception ex) {
            LOGGER.error(ex.getMessage(), ex);
            throw ex;
        } finally {
            if (xmlr != null) {
                try {
                    xmlr.close();
                } catch (Exception ex) {
                    LOGGER.error(ex.getMessage());
                }
            }

            if (is != null) {
                try {
                    is.close();
                } catch (Exception ex) {
                    LOGGER.error(ex.getMessage());
                }
            }
        }

        // Waiting for all task to completed and shutdown the executor server
        try {
            if (trackingTask != null) {
                for (Future future : trackingTask) {
                    future.get();
                }
            }
        } catch (Exception ex) {
            LOGGER.error(ex.getMessage(), ex);
        } finally {
            // clean job
        }
    }

With:
protected static BlockingQueue<NlpgwsProviderPOJO> parsedProviders = new LinkedBlockingDeque<NlpgwsProviderPOJO>();
    protected static BlockingQueue<NlpgwsTitleInfoPOJO> parsedTitleInfo = new LinkedBlockingDeque<NlpgwsTitleInfoPOJO>();

Algorithm here:
- Read XML file
- Put data object to a BlockingQueue
- Init Executor -> create thread to scan the Blocking queue to do the business logic

Declare executor:
protected ExecutorService ingestionBatchInsert;
ThreadFactory batchInsertThreadFactory = new ThreadFactoryBuilder().setNameFormat("dls-batch-insert-%d").build();
ingestionBatchInsert = Executors.newFixedThreadPool(totalInsertThread + 1, batchInsertThreadFactory);

Add thread in trackingList:
for (int count = 0; count < totalInsertThread; count++) {
      Future task = ingestionBatchInsert.submit(othersBatchInsertThread);
      trackingTask.add(task);
}

OthersBatchInsertThread thread:
public class OthersBatchInsertThread implements Runnable {
        public void run() {
              //Get out object from BlockingQueue
             DLSFileParserService.parsedProviders.poll()
             //process
        }
}

Check file is open (lock) or not in Java

RandomAccessFile rf = new RandomAccessFile(file, "rw");
FileChannel fileChannel = rf.getChannel();
FileLock lock = null;
try {
        // let us try to get a lock. If file already has an exclusive lock by another process
        LOGGER.info("Trying to acquire lock");
        lock = fileChannel.tryLock();
        if (lock != null) {
              success = true;
        }
} catch (Exception ex) {
         LOGGER.error(ex.getMessage());
} finally {
         if (lock != null) {
               lock.release();
         }
         if(fileChannel != null){
               fileChannel.close();
         }
          if(rf != null){
               rf.close();
          }
}

Observe folder to pick file when it's available


public class MMTServerStartListener implements ApplicationListener<ContextRefreshedEvent> {

    @Autowired
    YourMonitorListenerImpl fileMonitor;

    @Override
    public void onApplicationEvent(ContextRefreshedEvent context) {
        try {
            String filePath = "<file path>";
            startMonitor(filePath, fileMonitor);
        } catch (Exception ex) {
            LOGGER.error(ex.getMessage(), ex);
        }
    }

    private void startMonitor(String filePath, YourMonitorListenerImpl fileMonitorImpl) {
        try {
            if (filePath != null && filePath.length() > 0) {

                final File directory = new File(filePath.trim());
                FileAlterationObserver fao = new FileAlterationObserver(directory);
                fao.addListener(fileMonitorImpl);

                final FileAlterationMonitor monitor = new FileAlterationMonitor();
                monitor.addObserver(fao);

                LOGGER.info("Starting monitor. CTRL+C to stop.");
                monitor.start();

                Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() {
                    public void run() {
                        try {
                            LOGGER.info("Stopping monitor.");
                            monitor.stop();
                        } catch (Exception ignored) {
                            LOGGER.error(ignored.getMessage(), ignored);
                        }
                    }
                }));
            } else {
                LOGGER.error("Invalid input the monitor folder");
            }
        } catch (Exception ex) {
            LOGGER.error(ex.getMessage(), ex);
        }
    }
}

With "YourMonitorListenerImpl" implements "FileAlterationListener" interface.

Wednesday, August 12, 2015

Thread with ExecutorService

ThreadFactory buildCache = new ThreadFactoryBuilder().setNameFormat("thread-name-%d").build();
ExecutorService executorService = Executors.newFixedThreadPool(totalThreads, buildCache);

List<Future> trackingTask = new ArrayList<Future>();

for (int index = 0; index < totalThreads; index++) {
        trackingTask.add(executorService.submit(new Runnable() {
              @Override
               public void run() {
                      //run your code
               }
        }));
}

// Run and waiting for task to finish
for (Future task : trackingTask) {
        task.get();
}

// Shutdown executor:
executorService.shutdownNow();

Using Redis cache

Installation:

Follow the instruction at: Install Redis cache in linux - DigitalOcean

Using Redis cache:

Create bean:
<bean id="jedisConnFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"
          p:hostName="${atm.cache.redis.host}"
          p:port="${atm.cache.redis.port}"
          p:poolConfig-ref="jedisPoolConfig"
          p:usePool="true"/>
 <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate"
          p:connectionFactory-ref="jedisConnFactory" p:keySerializer-ref="stringRedisSerializer"/>

 Autowired in java implement:
@Autowired
private RedisTemplate<String, ProgramBasicInfo> programRedisTemplate;

Delete all cache: (in this case, it get connection and then flush all data)
programRedisTemplate.getConnectionFactory().getConnection().flushAll();

Delete cache:
programRedisTemplate.opsForHash().getOperations()
                                    .delete(Constants.CACHE_PROGRAM_GUIDE_KEY);

Put data to cache:
programRedisTemplate.opsForHash().put(Constants.CACHE_PROGRAM_GUIDE_KEY, program.getProgramId(),       programBasicInfo);

Get data from cache:
(ProgramBasicInfo) programRedisTemplate.opsForHash().get(Constants.CACHE_PROGRAM_GUIDE_KEY, programId);

References:

http://blog.joshuawhite.com/java/caching-with-spring-data-redis/
http://caseyscarborough.com/blog/2014/12/18/caching-data-in-spring-using-redis/


Tuesday, August 11, 2015

Synchronize SimpleDateFormat object in Java

SimpleDateFormat object does not work properly in a multi threaded environment. It may output a wrong date when parsing. So the safest way is to synchronize it.

private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
public String formatDate(Date d) {
    synchronized(sdf) {
        return sdf.format(d);
    }
}
 
Hope that help.

Sunday, August 9, 2015

Check if a class is loaded and lib file location in JVM

Example below is to see if JVM using ojdbc6 version 11 or version 12.

final ClassLoader loader = Thread.currentThread().getContextClassLoader();
ClassPath clazzPath = ClassPath.from(loader);
Set<ClassInfo> classes = clazzPath.getTopLevelClasses();
for (final ClassPath.ClassInfo classInfo : classes) {
    if (classInfo.getName().startsWith("oracle")) {
         if (classInfo.getName().contains("oracle.jdbc.babelfish")) {
                 isVersion12 = true;
         }

    }
}
            

if(isVersion12) {
        LOGGER.info("Ojdbc version 12");

} else {
        LOGGER.info("Ojdbc version 11");

}

Class klass = OracleConnection.class;
URL location = klass.getResource('/' + klass.getName().replace('.',  '/') + ".class");
LOGGER.info(location.toString());

Read package name and version from Manifest file

Bean: 

public class BuildVersion {
   
    /** The build name. */
    private String buildName;
   
    /** The version. */
    private String version;

    /**
     * Gets the version.
     *
     * @return the version
     */
    public String getVersion() {
        return version;
    }

    /**
     * set version.
     *
     * @param version the new version
     */
    public void setVersion(String version) {
        this.version = version;
    }

    /**
     * Gets the builds the name.
     *
     * @return the builds the name
     */
    public String getBuildName() {
        return buildName;
    }

    /**
     * Sets the builds the name.
     *
     * @param buildName the new builds the name
     */
    public void setBuildName(String buildName) {
        this.buildName = buildName;
    }
}

VersionServiceImpl:

private BuildVersion getBuildVersion() {
        BuildVersion buildVersion = new BuildVersion();

        Class clazz = this.getClass();
        String className = clazz.getSimpleName() + ".class";
        String classPath = clazz.getResource(className).toString();
        try {
            String manifestPath = classPath.substring(0, classPath.lastIndexOf("/WEB-INF")) +
                    "/META-INF/MANIFEST.MF";
            Manifest manifest = new Manifest(new URL(manifestPath).openStream());
            Attributes attr = manifest.getMainAttributes();
            String title = attr.getValue("Specification-Title");
            String version = attr.getValue("Specification-Version");

            buildVersion.setBuildName(title);
            buildVersion.setVersion(version);
        } catch (Exception ex) {
            LOGGER.error(ex.getMessage(), ex);
        }
        return buildVersion;
    }

VersionEndpoint:

@Component
@Path ("/")
public class VersionEndpoint {
   
    /** The version service. */
    @Autowired
    private VersionService versionService;
   
    /**
     * getVersion service use to retrieve the current release version.   
     *
     * @param format the format
     * @return Version response
     */
    @GET
    @Path ("/version")
    @Produces ({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
    public Response getVersion(@DefaultValue (WSConstants.RESPONSE_TYPE_JSON) @QueryParam ("format") String format) {
        try {
            return Response.ok().entity(versionService.getVersion()).type(WSUtil.getWSResponseType(format)).build();
        } catch (Exception e) {
            return Response.ok().entity("Error: "  + e.getMessage()).type(WSUtil.getWSResponseType(format)).build();
        }       
    }                                       
}

Install Ibus-unikey in Linux for Vietnamese

Uninstall all preinstalled ibus package and its dependencies
    sudo apt-get remove --auto-remove ibus
Purging config/data too
    sudo apt-get purge --auto-remove ibus
Maybe restart is needed.

Install new ibus-unikey
    sudo apt-get install ibus-unikey

After installing done, run:
    ibus-setup

to open Ibus Preferences. 
Choose Input method tab, check 'Customize active input methods'. In selection box, click Show all input methods, choose Vietnamese->Unikey.

Logout your system to active the new input method.

Now in top right corner of your screen, you are able to select 'Unikey' to input Vietnamese.

Thursday, August 6, 2015

Some good Linux scripts

Change test in file

sed -i s/helloword/HELLOWORLD/g file.txt

Copy file from Linux server to Linux server

scp source_file_name username@destination_host:destination_folder
     or
scp [-r] [[user1@]hostname1:]file1 ... [[user2@]hostname2:]file2

Ex:
scp pluto:/home/jones/letter.doc .
copy the file letter.doc from the /home/jones directory of the remote system pluto to the working directory on the local system 

scp notice.doc pluto:/home/jones
copy the file notice.doc from current directory of the local system to the /home/jones directory of the remote system, pluto

Find file match name in a folder

cd <your-folder>
for f in *; do
        case $f in
                abc*.xml)
                        echo $f
                        ;;
               
def*.xml)
                        echo $f
                        ;;
        esac
done


Check file is open in linux

Check if file prefix by 'filename' is open by 'cp' process:

while :
do
    if [[ `lsof -c cp | grep filename*.xml` ]]
    then
    printf ".";
    sleep 0.5
       
    else
        break   
    fi
done
    echo "Done!"

Auto deploy script for Weblogic

Some Configuration (deploy.conf)

#Enter the package location here
source=/data/Jenkins_builds/MMT
#Enter the server IP here
FEServer=<fronend-ip-server>
BEServer=<backend-ip-server>

#Enter the target name here
FETarget=Server-0
BETarget=Server-2

#Enter package name
FEPackage=atm-ui-portlet
BEPackage=mmt-ws

#Enter the source path here
FEPath=/data/deployment/portal_destination
BEPath=/data/deployment

#Enter the portal deploy path here
PortalDeploy=/data/deployment/portal_deploy

Auto deploy script

#!/bin/bash
Logfile="logAutoDeploy.txt"

. ./deploy.conf

echo [$(date)]: ============Starting to auto deploy========== >> $Logfile

if [ -f $source/$BEPackage.war ]; then
    echo [$(date)]: ============Deploy Backend package========== >> $Logfile
    echo undeploy the package $BEPackage
    java -classpath /data/weblogic/wlserver_10.3/server/lib/weblogic.jar  weblogic.Deployer -adminurl t3://$BEServer:7001 -user <user-name> -password <password> -undeploy -name $BEPackage
    echo the package $BEPackage is undeployed >> $Logfile

    echo delete the package $BEPath/$BEPackage >> $Logfile
    cd $BEPath
    rm -rf $BEPackage

    mkdir $BEPackage
    cd $BEPackage

    echo copy $BEPackage package to $BEPath/$BEPackage >> $Logfile
    cp $source/$BEPackage.war ./
   
    echo unzip $BEPackage package >> $Logfile
    unzip $BEPackage.war
    rm -f $BEPackage.war
    echo deploy $BEPackage to back end server >> $Logfile
    java -classpath /data/weblogic/wlserver_10.3/server/lib/weblogic.jar  weblogic.Deployer -adminurl t3://$BEServer:7001 -user <user-name> -password <password> -deploy $BEPath/$BEPackage -targets $BETarget

    echo remove $BEPackage war file in Jenkins build >> $Logfile
    cd $source
    mv $BEPackage.war tmp
fi

if ls $source/$FEPackage* 1> /dev/null 2>&1; then
    echo [$(date)]: ============Deploy Frontend package========== >> $Logfile
    echo undeploy the package $FEPackage >> $Logfile
    java -classpath /data/weblogic/wlserver_10.3/server/lib/weblogic.jar  weblogic.Deployer -adminurl t3://$FEServer:7001 -user <user-name> -password <password> -undeploy -name $FEPackage
    echo the package $FEPackage is undeployed >> $Logfile

    echo delete the package $FEPackage >> $Logfile
    cd $FEPath
    rm -rf $FEPackage*

    echo copy the $FEPackage to $PortalDeploy >> $Logfile
    scp $source/$FEPackage* $PortalDeploy
    echo waiting to Liferay extracts the package >> $Logfile
    sleep 10
      
    echo deploy $FEPackage to server >> $Logfile
    java -classpath /data/weblogic/wlserver_10.3/server/lib/weblogic.jar  weblogic.Deployer -adminurl t3://$FEServer:7001 -user <user-name> -password <password> -deploy $FEPath/$FEPackage -targets $FETarget   

    echo remove $FEPackage war file in Jenkins build >> $Logfile
    cd $source
    mv $FEPackage* tmp
fi