Saturday, 1 February 2020

Error - CuratorConnectionLossException: KeeperErrorCode = ConnectionLoss


20/01/31 09:58:42 ERROR ConnectionState: Connection timed out for connection string (zookeeper-prelive:2181) and timeout (15000) / elapsed (15086)org.apache.curator.CuratorConnectionLossException: KeeperErrorCode = ConnectionLoss        at org.apache.curator.ConnectionState.checkTimeouts(ConnectionState.java:197)        at org.apache.curator.ConnectionState.getZooKeeper(ConnectionState.java:87)        at org.apache.curator.CuratorZookeeperClient.getZooKeeper(CuratorZookeeperClient.java:115)        at org.apache.curator.framework.imps.CuratorFrameworkImpl.getZooKeeper(CuratorFrameworkImpl.java:477)        at org.apache.curator.framework.imps.ExistsBuilderImpl$2.call(ExistsBuilderImpl.java:172)        at org.apache.curator.framework.imps.ExistsBuilderImpl$2.call(ExistsBuilderImpl.java:161)        at org.apache.curator.RetryLoop.callWithRetry(RetryLoop.java:107)        at org.apache.curator.framework.imps.ExistsBuilderImpl.pathInForeground(ExistsBuilderImpl.java:158)        at org.apache.curator.framework.imps.ExistsBuilderImpl.forPath(ExistsBuilderImpl.java:148)        at org.apache.curator.framework.imps.ExistsBuilderImpl.forPath(ExistsBuilderImpl.java:36)        at org.apache.spark.deploy.SparkCuratorUtil$.mkdir(SparkCuratorUtil.scala:48)        at org.apache.spark.deploy.master.ZooKeeperPersistenceEngine.<init>(ZooKeeperPersistenceEngine.scala:41)        at org.apache.spark.deploy.master.ZooKeeperRecoveryModeFactory.createPersistenceEngine(RecoveryModeFactory.scala:71)        at org.apache.spark.deploy.master.Master.onStart(Master.scala:174)        at org.apache.spark.rpc.netty.Inbox$$anonfun$process$1.apply$mcV$sp(Inbox.scala:122)        at org.apache.spark.rpc.netty.Inbox.safelyCall(Inbox.scala:205)        at org.apache.spark.rpc.netty.Inbox.process(Inbox.scala:101)        at org.apache.spark.rpc.netty.Dispatcher$MessageLoop.run(Dispatcher.scala:221)        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)        at java.lang.Thread.run(Thread.java:748)


When you get above error in Spark Master check whether the ZooKeeper is running. If you see zoo keeper is working fine. Then it is because of zookeeper temporal memory issue. So restart zookeeper , also the spark master.

You might need to consider increasing RAM for this system.

Sunday, 10 November 2019

Spring Rest and Spring Profile in Docker

Hi All,

While new thing docker images and hosting the docker images is getting picked every where.

* There was a requirement how to change spring profile in docker in every stage - https://github.com/krishnaram/hello-world-profile
* Also made project for using docker hosted mongo db with rest client - https://github.com/krishnaram/restImage
Using --spring.profiles.active=prod in compose file.

Thanks...

Saturday, 17 December 2016

ui:repeat in Java class

Hi,

To achieve ui:repeat in Java class.
The object to be looped on must be list or an array.

 //It is the context called by current threads request  
 FacesContext context = FacesContext.getCurrentInstance();  
 // Application instance associated with this web application.  
 Application application = context.getApplication();  
 //Here component type will be facelets.ui.Repeat  
 UIRepeat repeat= (UIRepeat) application  
                 .createComponent(UIRepeat.COMPONENT_TYPE);  
                 //Here the ID is set  
                 repeat.setId("repeatGrid");  
                 //The variable name is set  
                 repeat.setVar("value");  
 //used to calculate the value for the specified attribute or property name, if any  
 //Here the value is set for “value” attribute  
                 repeat  
                 .setValueExpression(  
                                 "value",  
                                 getValueExpression(fc,  
                                                 "#{valueList.value}"));  

You can add “repeat” variable to "parentObject"(parent object) by using

 parentObject.getChildren().add(repeat);  

This implementation was done for the dynamic population through methods inside java classes.
You can implement in jsf simply through this.
 <ui:repeat var=" value " value=""#{valueList.value}">  
         <h:outputText value="#{ value }”/>  
 </ui:repeat>  

Thanks.

Thursday, 3 December 2015

Spring 4 MultipartFile and Angular File Upload with Spring Security

Hi,

In Angular file upload is a bit tricky as ng-model doesn't support file and in JSON we can't send file.
In Spring with File Multipart accepting with other values is complex to implement.

It was time consuming to find approaches to Upload file from Angular and Accepting as File MultiPart in Spring.

Angular side

1. Create a directive to fetch the file.
2. Inject all required things to the Controller.

Here in Directive restrict type 'A' is used. You can change with any type if required.

I have used JSP to ease with csrf tokens.

 <!--  
 Author : Ramakrishna Panni  
 name: fileUpload.jsp  
 details: To test file upload with a File,Pic and a String   
 -->  
 <%@ page language="java" contentType="text/html; charset=ISO-8859-1"  
   pageEncoding="ISO-8859-1"%>  
   <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>  
 <html>  
 <head>  
      <meta name="_csrf" content="${_csrf.token}"/>  
      <!-- default header name is X-CSRF-TOKEN -->  
      <meta name="_csrf_header" content="${_csrf.headerName}"/>  
  <script src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.11.3.min.js"></script>  
  <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.2.24/angular.min.js"></script>  
 </head>  
 <body ng-app='fileUpload' ng-controller='UploadController'>  
      <form enctype="multipart/form-data"  
           ng-submit="submitForm()">  
           File to upload: <input type="file" file-model="cvBlob"><br /> Name: <input  
                type="text" name="name" ng-model="result.success"><br /> <br />   
           Pic to Upload:     <input type="file" file-model="picBlob"><br /> <br /><input type="submit"  
                value="Upload"> Press here to upload the file!  
                <input type="hidden" id="csrf" name="${_csrf.parameterName}" value="${_csrf.token}" />  
      </form>  
      <script type="text/javascript">  
      var upload = angular.module('fileUpload', []);  
      upload.controller('UploadController', ['$scope', '$window','$http', function($scope, $window,$http,fileUpload) {  
            $scope.result = {};  
            var token = $("meta[name='_csrf']").attr("content");  
         var header = $("meta[name='_csrf_header']").attr("content");  
            $scope.submitForm = function(){  
                 var pic = $scope.picBlob;  
                 var cv = $scope.cvBlob;  
                 var uploadUrl = "/upload?_csrf="+token  
                 console.log(pic);  
                 var fd = new FormData();  
                 fd.append('pic', pic);  
               fd.append('cv', cv);  
               fd.append('result',JSON.stringify($scope.result));  //You should make it to String while sending
               $http.post(uploadUrl, fd, {  
                    transformRequest: function (data, headersGetterFunction) {  
                   return data;  
                  },  // To take Http Request and Headers and making it Serialized, without serizlized object Spring will give Media Not Supported error
                    headers: {'Content-Type': undefined}  
               }) .success(function(data){  
                    if (data.error === "") {  
                       // Showing errors.  
                            $scope.success = data.success;  
                      } else {  
                           $scope.error = data.error;  
                      }   
               })  
               .error(function(){  
               });       
            };  
       }]);  
      upload.directive('fileModel', ['$parse', function ($parse) {  
             return {  
               restrict: 'A',  
               link: function(scope, element, attrs) {  
                 var model = $parse(attrs.fileModel);  
                 var modelSetter = model.assign;  
                 element.bind('change', function(){  
                   scope.$apply(function(){  
                     modelSetter(scope, element[0].files[0]);  
                   });  
                 });  
               }  
             };  
           }]);  
      </script>  
 </body>  
 </html>  

Points to look on
  • Append every data in FormData object using property tags.
  • Stringify  JSON  append in FormData
  • Transform the request with headers as Serialized 
  • Make Content Undefined as Angular will convert it to right one

Spring Side

  1. Create Bean of  "MultipartResolver"
  2. Add Multipart Filter in "AbstractSecurityWebApplicationInitializer"
JAR's needed commons-io,commons-fileupload and jackson.

MultipartResolver Bean

In this you can set the max upload size,encoding, lazy resolve,temp directory and others.
Define in Application configuration class.
 @Bean(name = "filterMultipartResolver")  
       public CommonsMultipartResolver commonsMultipartResolver(){  
            CommonsMultipartResolver commonsMultipartResolver = new CommonsMultipartResolver();  
            commonsMultipartResolver.setDefaultEncoding("utf-8");  
            commonsMultipartResolver.setMaxUploadSize(50000000);  
            return commonsMultipartResolver;  
       }  

MultipartFilter


Define in Application configuration class.
 protected void beforeSpringSecurityFilterChain(ServletContext servletContext) {  
        insertFilters(servletContext, new MultipartFilter());  
       }  

Controller Class


 package com.mindfiresolutions.springmaven.controller;  
 import java.io.BufferedOutputStream;  
 import java.io.File;  
 import java.io.FileOutputStream;  
 import org.codehaus.jackson.map.ObjectMapper;  
 import org.springframework.stereotype.Controller;  
 import org.springframework.web.bind.annotation.RequestMapping;  
 import org.springframework.web.bind.annotation.RequestMethod;  
 import org.springframework.web.bind.annotation.RequestParam;  
 import org.springframework.web.bind.annotation.ResponseBody;  
 import org.springframework.web.multipart.MultipartFile;  
 import com.mindfiresolutions.springmaven.models.Result;  
 /**  
  * Author: Ramakrishna Panni  
  * Class: FileUploadController  
  * Details: Created to handle Sample file Upload  
  */  
 @Controller  
 public class FileUploadController {  
   @RequestMapping(value="/upload", method=RequestMethod.GET)  
   public @ResponseBody Result provideUploadInfo() {  
        Result res = new Result();  
        res.setError("You can upload a file by posting to this same URL.");  
     return res;  
   }  
   @RequestMapping(value="/upload", method=RequestMethod.POST, consumes = {"multipart/form-data"})  
   public @ResponseBody Result handleFileUpload( @RequestParam("result") String res, @RequestParam("cv") MultipartFile file, @RequestParam("pic") MultipartFile pic){  
        Result result = new Result();  
        ObjectMapper mapper = new ObjectMapper();  
           Result resNew = new Result();  
           try {  
                resNew = mapper.readValue(res,Result.class);  
           } catch (Exception e) {  
                result.setError("JSON mapping failed"+e.getMessage());  
                return result;  
           }   
        if (!pic.isEmpty()) {  
             try {  
         byte[] bytes = pic.getBytes();  
         BufferedOutputStream stream =  
             new BufferedOutputStream(new FileOutputStream(new File("pic.jpg")));  
         stream.write(bytes);  
         stream.close();  
         result.setSuccess("You successfully uploaded "+resNew.getSuccess());  
         return result;  
       } catch (Exception e) {  
            result.setError("You failed to upload " + e.getMessage());  
       }  
     } else {  
          result.setError("You failed to upload because the file was empty.");  
        }  
     if (!file.isEmpty()) {  
       try {  
         byte[] bytes = file.getBytes();  
         BufferedOutputStream stream =  
             new BufferedOutputStream(new FileOutputStream(new File("new.txt")));  
         stream.write(bytes);  
         stream.close();  
         result.setSuccess("You successfully uploaded "+resNew.getSuccess());  
         return result;  
       } catch (Exception e) {  
            result.setError("You failed to upload " + e.getMessage());  
         return result;  
       }  
     } else {  
          result.setError("You failed to upload because the file was empty.");  
       return result;  
     }  
   }  
 }  

Points to look on

  • @RequestParam with the property tag name should be used to get Stringified JSON.
  • @RequestPart with the property tag name should be used to get MultiPartFile.
  • Object Mapper will help to make String to Object.
Thanks,

Sunday, 22 November 2015

Spring Security 4 Login Example using Hibernate & Annotations without using XML Configuration

Hi,

I faced some difficulties while setting up login using Spring Security 4 login without XML configuration. I thought of writing it, so others can refer and save time while developing.

Environment used
  • IDE: STS
  • Database: MySql 
  • Project Management: Maven
  • Server: Jetty (Default Server looks for Web.xml)
ImportantJAR's used
  • Jackson JSON Mapper JAR version 1.9.10
  • Spring Security JAR version 4.0.2 (Web,Config,Data & Taglibs)
  • Spring webMVC JAR
  • JPA, Spring Data, HSQL, MySQL JAR's
  • Other Web Dependency JAR's
STS latest release is 3.7.1 it will create web.xml file for Spring project by default. So use 
File->New->Maven Project. It won't create web.xml

Add the above mentioned JAR's to POM.

I have explained Security Config and CustomAuthenticationProvider class here, get whole project from GIT.

SecurityConfig.java
 package com.springsecurityblog.config;  
 /**  
  * Author: Ramakrishna Panni  
  * Class: SecurityConfig  
  * Details: It does configure for Login page, Logout page, Failure page and CSRF token management  
  */  
 import org.springframework.beans.factory.annotation.Autowired;  
 import org.springframework.context.annotation.Bean;  
 import org.springframework.context.annotation.Configuration;  
 import org.springframework.security.authentication.AuthenticationManager;  
 import org.springframework.security.authentication.AuthenticationProvider;  
 import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;  
 import org.springframework.security.config.annotation.web.builders.HttpSecurity;  
 import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;  
 import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;  
 import org.springframework.security.data.repository.query.SecurityEvaluationContextExtension;  
 import org.springframework.security.web.csrf.CsrfTokenRepository;  
 import org.springframework.security.web.csrf.HttpSessionCsrfTokenRepository;  
 @Configuration  
 @EnableWebSecurity  
 public class SecurityConfig extends WebSecurityConfigurerAdapter{  
   @Autowired  
   private AuthenticationProvider customAuthenticationProvider;   
      @Autowired  
      public void configure(AuthenticationManagerBuilder auth) throws Exception {  
            auth  
            .authenticationProvider(customAuthenticationProvider);  
   }  
       @Override  
        protected void configure(HttpSecurity http) throws Exception {  
          http.authorizeRequests().antMatchers("/assets/**").permitAll()  
          .and()  
            .formLogin().loginPage("/loginPage")  
              .defaultSuccessUrl("/homePage")  
              .failureUrl("/loginPage?error")  
              .usernameParameter("username").passwordParameter("password")     
              .and().csrf().csrfTokenRepository(csrfTokenRepository())  
            .and()  
              .logout().logoutSuccessUrl("/loginPage?logout");   
        }  
   @Bean  
   public SecurityEvaluationContextExtension securityEvaluationContextExtension() {  
     return new SecurityEvaluationContextExtension();  
   }  
   @Override  
   public AuthenticationManager authenticationManagerBean() throws Exception {  
    return super.authenticationManagerBean();  
   }  
   private CsrfTokenRepository csrfTokenRepository()   
   {   
     HttpSessionCsrfTokenRepository repository = new HttpSessionCsrfTokenRepository();   
     repository.setSessionAttributeName("_csrf");  
     return repository;   
   }  
 }  

What does Security Config do?

It  configures Authentication provider of configure method where the Role and Authentication of request is done. It does http security configuration with which URL to permit, login URL's and it also configures whether the application needs CSRF token generation. We can configure Success handlers for login and others here too. It extends "WebSecurityConfigurerAdapter" of Spring Framework Security.

CustomAuthenticationProvider.java
 package com.springsecurityblog.config;  
 /**  
  * Author: Ramakrishna Panni  
  * Class: CustomAuthenticationProvider  
  * Details: It is used to get credentials and authorize the request with roles  
  */  
 import java.util.HashSet;  
 import java.util.Set;  
 import org.springframework.beans.factory.annotation.Autowired;  
 import org.springframework.security.authentication.AuthenticationProvider;  
 import org.springframework.security.authentication.BadCredentialsException;  
 import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;  
 import org.springframework.security.core.Authentication;  
 import org.springframework.security.core.AuthenticationException;  
 import org.springframework.security.core.GrantedAuthority;  
 import org.springframework.security.core.authority.SimpleGrantedAuthority;  
 import org.springframework.stereotype.Component;  
 import com.springsecurityblog.model.Employee;  
 import com.springsecurityblog.model.Role;  
 import com.springsecurityblog.service.AccountService;  
 @Component("AuthenticationProvider")  
 public class CustomAuthenticationProvider implements AuthenticationProvider {  
       @Autowired  
       private AccountService accountService;  
       public Authentication authenticate(Authentication authentication)   
           throws AuthenticationException {  
                      String username = authentication.getName();  
                      String password = (String) authentication.getCredentials();  
            Employee member = accountService.findAccountByUsername(username);  
            //For username not valid   
         if (member == null || !member.getEmployeeUserName().equalsIgnoreCase(username)) {  
           throw new BadCredentialsException("Username not found.");  
         }  
         //For password not valid  
         if (!password.equals(member.getPassword())) {  
           throw new BadCredentialsException("Wrong password.");  
         }  
         Set<GrantedAuthority> setAuths = new HashSet<GrantedAuthority>();  
         //Employee can have many roles  
         for (Role employeeRole : member.getEmpRoles()) {  
                    setAuths.add(new SimpleGrantedAuthority(employeeRole.getAuthority()));  
               }  
         return new UsernamePasswordAuthenticationToken(member, password, setAuths);   
          }  
           public boolean supports(Class<?> authentication) {  
                   return (UsernamePasswordAuthenticationToken.class.isAssignableFrom(authentication));  
                }  
 }  

What does CustomAuthenication Provider do?
Here we will get the login credential entered, which is captured by SecurityConfig class. By using Service methods we can get to know whether the credential is correct or wrong. Roles extends Authority, there can be many roles.

Here is the table created for Employee and Role Bean.


 CREATE TABLE `tblemployee` (  
   `ntEmpID` bigint(20) NOT NULL AUTO_INCREMENT,  
   `vcEmployeeUserName` varchar(30) NOT NULL,  
   `vcEmailIdOff` varchar(50) NOT NULL,  
   `vcEmailIdPer` varchar(50) NOT NULL,  
   `vcEmployeeFirstName` varchar(30) NOT NULL,  
   `vcEmployeeMiddleName` varchar(30) DEFAULT NULL,  
   `vcEmployeeLastName` varchar(30) NOT NULL,  
   `vcPresentAdd` varchar(150) NOT NULL,  
   `vcPermanentAdd` varchar(150) NOT NULL,  
   `vcHomePhoneNumber` varchar(10) DEFAULT NULL,  
   `vcOfficeNumber` varchar(10) NOT NULL,  
   `vcSkills` varchar(100) DEFAULT NULL,  
   `vcPassword` varchar(4000) NOT NULL,  
   `fsCV` tinyblob,  
   `fsEmployeePic` tinyblob,  
   PRIMARY KEY (`ntEmpID`),  
   UNIQUE KEY `UQ__tblEmplo__993FFD3B15DA3E5D` (`vcEmailIdPer`),  
   UNIQUE KEY `UQ__tblEmplo__96FFB61918B6AB08` (`vcEmailIdOff`),  
   UNIQUE KEY `UQ__tblEmplo__ED923BE51B9317B3` (`vcEmployeeUserName`)  
  ) ENGINE=InnoDB AUTO_INCREMENT=9 DEFAULT CHARSET=latin1  
 CREATE TABLE `tblemproles` (  
   `ntEmpRoleID` bigint(20) NOT NULL AUTO_INCREMENT,  
   `ntEmpID` bigint(20) NOT NULL,  
   `vcRole` varchar(45) NOT NULL,  
   PRIMARY KEY (`ntEmpRoleID`),  
   UNIQUE KEY `uni_empID_role` (`vcRole`,`ntEmpID`),  
   KEY `ntEmpID` (`ntEmpID`),  
   CONSTRAINT `tblemproles_ibfk_1` FOREIGN KEY (`ntEmpID`) REFERENCES `tblemployee` (`ntEmpID`) ON DELETE NO ACTION ON UPDATE NO ACTION  
  ) ENGINE=InnoDB AUTO_INCREMENT=11 DEFAULT CHARSET=latin1  
loginPage.jsp


 <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>  
 <html>  
 <head>  
      <script src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.11.3.min.js"></script>  
  <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.5.0-beta.1/angular.min.js"></script>  
 </head>  
 <body ng-app='Login' onload='document.loginForm.username.focus();'>  
   <h3>TMS</h3>  
   <c:if test="${not empty error}"><div>${error}</div></c:if>  
   <c:if test="${not empty message}"><div>${message}</div></c:if>  
   <form name='login' action="<c:url value='/loginPage' />" method='POST'>  
     <table>  
       <tr>  
         <td>UserName:</td>  
         <td><input type='text' name='username' ng-model="login.username" value=''></td>  
       </tr>  
       <tr>  
         <td>Password:</td>  
         <td><input type='password' name='password' ng-model="login.password" /></td>  
       </tr>  
       <tr>  
         <td colspan='2'><button type="submit">Submit</button></td>  
       </tr>  
     </table>  
     <input type="hidden" name="${_csrf.parameterName}" value="${_csrf.token}" />  
   </form>  
 </body>  
 </html>  
homePage.jsp


 <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>  
 <%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>  
 <%@ taglib uri="http://www.springframework.org/security/tags" prefix="sec"%>  
 <!DOCTYPE html>  
 <html>  
 <head>  
  <meta charset="utf-8">  
  <meta http-equiv="X-UA-Compatible" content="IE=edge">  
  <meta name="_csrf" content="${_csrf.token}"/>  
      <!-- default header name is X-CSRF-TOKEN -->  
      <meta name="_csrf_header" content="${_csrf.headerName}"/>  
      <!-- ... -->  
  <title>Home</title>  
  <!-- Tell the browser to be responsive to screen width -->  
  <meta content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no" name="viewport">  
  <!-- Bootstrap 3.3.5 -->  
  <!--Favicon Image -->  
 </head>  
 <body class="hold-transition skin-blue sidebar-mini">  
 <sec:authorize access="hasRole('ANONYMOUS')">  
      <c:redirect url="/loginPage"/>  
 </sec:authorize>  
 <p>Welcome, <sec:authentication property="principal.employeeUserName"/></p><br/>  
 <c:url value="/logout" var="Signout" />  
      <form id="logout" action="${Signout}" method="post" >  
            <input type="hidden" name="${_csrf.parameterName}" value="${_csrf.token}" />  
      </form>  
 </body>  
 </html>  
After Login
We land in Home page. There we can get all values through "principal"object using Security tags.

Thanks,

Monday, 2 November 2015

Spiral Value Assigned Matrix

Hi,


I got a question from my friend, if user gives input 6 or  any number it should show matrix in the form of normal and Spiral assigned values.

Matrix Size is: 6

Normal Matrix is:

    1    2    3    4    5    6
    7    8    9   10   11   12
   13   14   15   16   17   18
   19   20   21   22   23   24
   25   26   27   28   29   30
   31   32   33   34   35   36
Spiral Matrix is:

    1     2        3     4      5      6
   20    21    22    23    24     7
   19    32    33    34    25     8
   18    31    36    35    26     9
   17    30    29    28    27    10
   16    15    14    13    12    11

I had gone through the problem, got it can be solved in 4 recurring  steps.

 package newlearn;  
 import java.util.Scanner;  
 /**  
  *   
  * @author Ramakrishna Panni  
  *  
  */  
 public class SpiralMatrix {  
      public static void main(String[] args) {  
           // TODO Auto-generated method stub  
           try{  
                System.out.println("Enter matrix size to get spiral matrix");  
                Scanner sc = new Scanner(System.in);  
                String[] inter = sc.nextLine().trim().split(" ");  
                int size = Integer.parseInt(inter[0]);  
                System.out.println("Matrix Size is: "+size);  
                sc.close();  
                int higestValue = size*size;  
                int spiralMatrix[][] = new int[size][size];  
                int normalMatrix[][] = new int[size][size];  
                int normalValue = 0;  
                for(int i=0;i<size;i++)  
                {  
                     for(int j=0;j<size;j++)  
                     {  
                          normalMatrix[i][j] = ++normalValue;  
                          spiralMatrix[i][j] = 0;  
                     }  
                }  
                System.out.println("Normal Matrix is:\n");  
                for(int i=0;i<size;i++)  
                {  
                     for(int j=0;j<size;j++)  
                     {  
                          System.out.printf("%5d",normalMatrix[i][j]);  
                     }  
                     System.out.println("");  
                }  
                int lastValue=0;  
                int step = 1;  
                int step1Col = 0;  
                int step1Row = 0;  
                int step2Col = 0;  
                int step2Row = 0;  
                int step3Col = 0;  
                int step3Row = 0;  
                int step4Col = -1;  
                int step4Row = 0;  
                while (lastValue < higestValue) {  
                     step = 1;  
                     //this is the step where value assigned in Horizontal incrementing positional way  
                     if(step == 1)  
                     {  
                          step1Row = step4Row;  
                          step1Col = ++step4Col;  
                          for(int j=step1Col;j<size;j++)  
                          {  
                               if(spiralMatrix[step1Row][j] == 0)  
                               {  
                                    spiralMatrix[step1Row][j] = ++lastValue;  
                                    step1Col = j;  
                               }  
                               else{  
                                    break;  
                               }  
                          }  
                          step = ++step;  
                     }  
                     //this is the step where value assigned in Vertical incrementing positional way  
                      if (step == 2)  
                     {  
                          step2Col = step1Col;  
                          step2Row = step1Row+1;  
                          for(int i=step2Row;i<size;i++)  
                          {  
                               if(spiralMatrix[i][step1Col] == 0)  
                                    {  
                                         spiralMatrix[i][step1Col] = ++lastValue;  
                                         step2Row = i;  
                                    }  
                                    else{  
                                         break;  
                                    }  
                          }  
                          step = ++step;  
                     }  
                     //this is the step where value assigned in Horizontal decrementing positional way  
                      if(step==3)  
                     {  
                          step3Row = step2Row;  
                          step3Col = step2Col-1;  
                               for(int j=step3Col;j>=0;j--)  
                               {  
                                    if(spiralMatrix[step3Row][j] == 0)  
                                    {  
                                         step3Col = j;  
                                         spiralMatrix[step3Row][j] = ++lastValue;  
                                    }  
                                    else{  
                                         break;  
                                    }  
                               }  
                          step = ++step;  
                     }  
                     //this is the step where value assigned in Vertical incrementing decrementing positional way  
                     if(step==4)  
                     {  
                               step4Row = --step3Row;  
                               step4Col = step3Col;  
                               for(int i=step4Row;i>=0;i--)  
                               {  
                                    if(spiralMatrix[i][step4Col] == 0)  
                                    {  
                                         step4Row = i;  
                                         spiralMatrix[i][step4Col] = ++lastValue;  
                                    }  
                                    else{  
                                         break;  
                                    }  
                               }  
                          step = ++step;  
                     }  
                }  
                System.out.println("Spiral Matrix is:\n");  
                for( int i=0;i<size;i++)  
                {  
                     for( int j=0;j<size;j++)  
                     {  
                          System.out.printf("%5d ",spiralMatrix[i][j]);  
                     }  
                     System.out.println("");  
                }  
           }  
           catch(Exception e)  
           {  
                e.printStackTrace();  
                System.out.println("Something Went wrong............Please run again.....");  
           }  
      }  
 }  

Friday, 10 July 2015

CF Ehcache and Saving Object

Hi,


I came across a situation where Session had too many variables which was getting through a Query. Logging was taking a long time and most of the Session Variables had Limited used only to show Charts on user activity just after Login.

I looked into 2 options for Limited Use Query.
1. Ehcache.
2. Saving as binary data file by using ObjectSave and ObjectLoad.


Ehcache

You can enable Ehcache through CF Admin-> Server Settings->Caching. 



Number of queries cached is 100 by default. You can increase it. If it increases maximum it will delete from the older one and save the new one in Ehcache region.
In my version CF 10 I have Ecache 2.6.6 JARS. Can have max 10000 Queries in Memory




 <cfquery datasource="dsn" name="VARIABLES.getClient"  cachedwithin="#createTimespan(1,0,0,0)#" >  
      SELECT TOP 10 cCompanyName FROM CLIENTS WHERE cCompanyName = 'company'  
 </cfquery>  
 <cfdump var="#VARIABLES.getClient#" >  


Cached will be true. In Normal Queries it will be false




It will save in 'Ehcache' cache region with id '1234' for 1 day

Working
If Same query is ran within 1 day Database layer hitting is surpassed and will get a query object from Ehcahe region. Parameter used should be same & query plan should be same, then only it will get from Cache.

Putting in Different cache region
We can save in different cache region too.
 <cfset cacheRegionNew('clients')>  
 <cfquery datasource="dsn" name="VARIABLES.getClient" cacheregion="clients" cachedwithin="#createTimespan(1,0,0,0)#" >  
      SELECT TOP 10 cCompanyName FROM CLIENTS WHERE cCompanyName = 'company'  
 </cfquery>  
 <cfdump var="#VARIABLES.getClient#" >  
We can't get the above Cached query  by using 'cacheGet' function. It will show 'undefined'.

We can put in CacheRegion with CacheId using 
 <cfset cachePut(1234,getClient,createTimespan(1,1,1,0),'','clients')>  

To retrieve Cached Query object 

We can verify whether the CacheRegion or CacheID exists before getting Query object.
 <cfif cacheRegionExists('Ehcache')>  
      <cfif cacheIdExists('1234','Ehcache')>  
           <cfdump var="#cacheGet(1234,'Ehcache')#" >  
      </cfif>  
 </cfif>  

Saving Query as Binary In Data File

We can save Query object in Data File as Binary Format.We can relative path for saving it in file.
 <cfquery datasource="rsa" name="VARIABLES.getClient">  
      SELECT TOP 10 cCompanyName FROM CLIENTS WHERE cCompanyName = 'company'  
 </cfquery>  
 <cfset objectSave(getClient,'../parent/client.txt')>  
To retrieve it 
 <cfdump var="#objectload('../parent/client.txt')#" >  

In 'Ehcahe' Cache region will be in Server Space. If the parameter is changing the Cached query will be used less. The Object Saved will be on Physical file as the Query runs it will be updated if we are saving it in Same page.
I went for 'Ehcache' option as it was very helpful in this particular case.