Posts

Showing posts from 2016

Automatic MongoDB backup in windows

It's a very good idea to take backups of our MongoDB periodically. We can use the following script to create a folder with current date time and dump the database in it. The script also runs 7zip and compresses the backup directory. When compression is done, this script will delete the backup directory. This will save a lot of disk space. @echo OFF :: This will create a timestamp like yyyy-mm-dd-hh-mm-ss. set BACKUPNAME=E:\mongo-db-backup set BACKUPNAME=%BACKUPNAME%\%DATE:~10,4%-%DATE:~4,2%-%DATE:~7,2%-%TIME:~0,2%-%TIME:~3,2%-%TIME:~6,2% @echo BACKUPNAME=%BACKUPNAME% :: Create a new directory md "%BACKUPNAME%" echo Running backup "%BACKUPNAME%" mongodump -h localhost -d stipend_icr -u stipend_icr_user -p pr0g0t1 -o "%BACKUPNAME%" REM ZIP the backup directory echo Running 7zip on backup "%BACKUPNAME%" "C:\Program Files\7-Zip\7z.exe" a -tzip "%BACKUPNAME%.zip" "%BACKUPNAME%" REM Delete the back

Map GET parameters to Object in Spring

It's possbile to map GET parameters to an object in Spring. It's really useful when we need to pass lots of parameters as query string e.g. submitting a search form with many parameters. The following controller demonstrates this.   @RestController @RequestMapping("hello") public class MyController {     @RequestMapping(value = "data", method = RequestMethod.GET)     public MyRequest getData(MyRequest request){             return request;     }     public static class  MyRequest {         private String name;          private int age;         public String getName(){return this.name;}         public int getAge(){return this.name;}         public void setName(String name){this.name=name;}          public void setAge(int age){this.age=age;}     } } Following GET request will create a MyRequest object with age=10 and name=Nayan GET hello/data?age=10&name=Nayan As we're sending the request object as response we'll get the follo

Pass state parameter from url in angular-ui-router

URL parameters are automatically sent as state parameters in angular-ui-router . All we need to do is declare the parameter names in state configuration. Here is an example of state configuration: .state('home.profile', {     url: '/profile?id',     templateUrl: 'templates/profile.html',     controller: 'ProfileCtrl' }) Here we've declared a parameter id in url attribute.  Now if we hit the url with parameter id e.g /profile?id=1234343 , w e'll be able to receive id=1234343 in state params.  We'll also be able to pass state parameter through ui-sre f lik e the following: <a ui-sref="home.profile({id: '1234343' })">Go to profile</a> Here is an example controller that demonstrates how to receive the parameter : .controller('ProfileCtrl', ['$scope', '$stateParams',     function ($scope, $stateParams) {         $scope.id = $stateParams.id; // id will be 1234343          } ])

Run tasks in background in Spring

Why background task? There are tons of examples where we often need to run a task in background e.g. sending an email, sms or notification, writing some log in file etc. If a task is not required to maintain atomicity and can be executed after some delay, it's a good candidate to run in background.   How to run tasks in background in Java? In Java, the easiest way to run a task in background is to create a new thread and execute it. It's quite simple. Here is how to do this: new Thread(new Runnable(){ @Override public void run(){ // your background task } }).start(); It's easy! But there are some maintenance problem with the above code. The major problem is - there is no way to control how many background tasks we want to run simultaneously. For example, if 1000 users registers in your application at a time and you create 1000 background threads to send them a confirmation email, maybe you'll be in trouble. So you ne

How to convert doc files to docx files in batch

The process converts all documents, not just Word documents. Say you got tons of Excel sheets, PowerPoint presentations and Word documents on your computer that were written in Office XP or 2003. How do you convert all these files to the new Office 2007 format. One option is that you open all of them in the associated Office program and manually save them in the newer (docx, xlsx or pptx) format. Or follow these steps and convert all documents in one go. Step 1: Download Migration Manager kit and extract it into a new folder - say: c:\office. office-pack Step 2: Download and install the Office Pack - this step is required even if you have Microsoft Office 2007 already installed on your computer. Step 3: Assuming that you extracted the Office Manager files in c:\office directory, go c:\office\tools, open ofc.ini using notepad and add the following line. fldr=c:\users\labnol\documents This refers to the folder location that holds your office files. I am po

How to configure Wildfly 10 to use MySQL

Follow these steps to configure MySQL in Wildfly 10 Step 1 : Download MySQL connector jar from mysql downloads page . Step 2 : Go to modules\system\layers\base\com of your Wildfly home Step 3 : Create folder like mysql\main and go to mysql\main Step 4 : Place the downloaded connector jar in the main folder  Step 5 : Create a file named module.xml in the main folder and put the following content in it. Set the path variable so that it matches the name of the jar file. <?xml version="1.0" encoding="UTF-8"?> <module xmlns="urn:jboss:module:1.1" name="com.mysql">     <resources>         <resource-root path="mysql-connector-java-5.1.39-bin.jar"/>                  </resources>     <dependencies>         <module name="javax.api"/>         <module name="javax.transaction.api"/>     </dependencies> </module> Step 6 : Go to standalone\configuration dir