Posts

Java Array Interview Questions for Entry-Level Developers Part 01

1. What is a Java array? (Definition, purpose, characteristics: fixed size, contiguous memory) Definition A Java array is a data structure that allows storing multiple values of the same data type in a contiguous block of memory. It is a fixed-size collection of elements, each identified by an index. Purpose Arrays are used to efficiently store and manage a collection of data. They provide: Fast access to elements using an index. Memory efficiency since elements are stored contiguously. Characteristics Fixed Size When an array is declared, its size must be specified. The size cannot be changed dynamically (for dynamic collections, use ArrayList or other collections). Contiguous Memory Allocation Arrays are stored in a continuous block of memory, making access faster compared to linked lists. Indexed Access Elements are accessed using zero-based indexing, e.g., array[0] refers to the first element. Homogeneous Elements All elements in an array must be of the same data type. Efficien...

Message pubish in goolge cloud pubsub topic with java client

Image
Table Of Contents What is Pub/Sub? What are the benefits of using Pub/Sub? Core concepts of Pub/sub. How to publish a message in pub/sub topic in java with credentials? Shutdown publisher when application stop or no need publisher.  GitHub project link of reusable module.  How to publish and receive a message in pub/sub in cloud console? Conclusion.  What is Pub/Sub? Cloud Pub/Sub is a fully-managed messaging service provided by Google Cloud Platform. It is designed to provide reliable, scalable, and real-time messaging between applications and services. Cloud Pub/Sub allows you to publish and consume messages from any application or service, regardless of the language or platform they are built on. It provides a simple and flexible architecture that decouples your systems and allows them to work independently. In Cloud Pub/Sub, you can create one or more topics to which you can publish messages. Subscribers can then subscribe to the topic and receive all published messag...

JPA vs Spring JPA vs Spring Data JPA vs Hibernate

JPA is the Java Persistence API , which is Java 's standard API for object-relational mapping . JPA is only an specification - you need an implementation of it to be able to use it. Hibernate is one of the most well-known and most used implementations of JPA, but there are others, such as EclipseLink JPA . The Spring Framework is a large framework to help you write enterprise-grade software easier. It contains support for many Java technologies, including JPA. The Spring Framework consists of a collection of projects , and one of these projects is Spring Data . The goal of Spring Data is to make it easier to work with different kinds of databases, from traditional relational databases to NoSQL databases. Spring Data supports JPA via the Spring Data JPA subproject. To write a program that uses JPA, you need at least a JPA implementation, such as Hibernate. If you are using the Spring Framework for your application, you will most likely want to use Spring Data JP...

Building Jersey Restful Web Services in Spring Boot.

What you need? Following are needed: java 8 maven 3.2 spring boot 1.5.8.RELEASE Maven pom.xml We will add  spring-boot-starter-parent  as a parent of our maven-based project. The added benefit of this is version management for Spring dependencies. < parent > < groupId > org.springframework.boot </ groupId > < artifactId > spring-boot-starter-parent </ artifactId > < version > 1.5.0.RELEASE </ version > </ parent > Adding the Spring-Boot-Starter-Jersey Dependency This will add/configure the Jersey-related dependencies. < dependency > < groupId > org.springframework.boot </ groupId > < artifactId > spring-boot-starter-jersey </ artifactId > </ dependency > The Full pom.xml <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apach...

About Spring @Component, @Repository, @Service and @Controller Annotations.

Spring @Component , @Service , @Repository and @Controller annotations are used for automatic bean detection using classpath scan in Spring framework. These four  annotations are same logically. Differences between @Component, @Repository, @Controller and @Service: @Component  This is a general-purpose stereotype annotation(auto scan) indicating that the class is a spring component. @Component annotation picks them up and registers their following classes as beans, just as if they were annotated with @Component . @Repository This is special type of @Component annotation.   This is to indicate that the class defines a data repository.  @Controller This is special type of @Component annotation. The @Controller annotation indicates that a particular class serves the role of a controller. The @Controller annotation acts as a stereotype for the annotated class, indicating its role.  What’s special about @Controller? We cannot...

Change Liferay Default User Portrait

Step 1: Add the below given properties in portal-ext.properties file. portal-ext.properties file location : %LIFERAY_HOME%\tomcat-7.0.62\webapps\ROOT\WEB-INF\classes If there no file exists with this name, please create this. And then add below lines. image.default.user.female.portrait=com/images/user_female_portrait.png image.default.user.male.portrait=com/images/user_male_portrait.png Step 2: Add the default custom image under below given path %LIFERAY_HOME%\tomcat-7.0.62\webapps\ROOT\WEB-INF\classes\com\images\ Step 3: Restart the server Enjoy !!!!

Deploy Spring Boot application on Tomcat

Deploy Spring Boot application on Tomcat Step #1 Add the following dependency to pom.xml in order to tell Spring Boot not to use its embedded Tomcat. < dependency >      < groupId > org . springframework . boot < / groupId >      < artifactId > spring - boot - starter - tomcat < / artifactId >      < scope > provided < / scope > < / dependency > Step #2 Change the packaging property to war in pom.xml. < properties >        < packaging > war < / packaging > < / properties > Step #3 Replace your initializer class with the following: package com.oms; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.builder.SpringApplicationBuilder; import org.springframework.boot.web.support.SpringBootServ...

Localization in liferay.

Liferay support several localization.You can easily localize your application to support more than one language. You can override existing language key and also add language key in hook. Configure your hook as below. <?xml version="1.0"?> <!DOCTYPE hook PUBLIC "-//Liferay//DTD Hook 6.2.0//EN" "http://www.liferay.com/dtd/liferay-hook_6_2_0.dtd"> <hook>     <portal-properties>portal.properties</portal-properties>     <language-properties>content/Language_en.properties</language-properties>     <language-properties>content/Language_nl.properties</language-properties>     <language-properties>content/Language_pt.properties</language-properties> </hook> Here above hook show three language(nl,pt and en) support. Add above three language files in src/content folder. You can also say in hook, how many languages support by your application. For...

Cosuming Liferay JSON Web Services

In liferay you can write json web service  easily. You can also consume those web service from other client such in java httpclient, javascript(like ajax jquery), dukescript and other client . For example you have a portlet name test-portlet and you write a json webservice as below:     @JSONWebService(value = "testService", method = "POST")     public JSONObject testService(String test) throws SystemException {         JSONObject result = JSONFactoryUtil.createJSONObject();         result.put("test", test);         return result;     } For consuming this service from several clients you can follow below steps: 1. In java client Here you can use jersy.  Client code given below:        Client    client = ClientBuilder.newClient();         ...

How to remove POST size limit

The connector section has the parameter maxPostSize   The maximum size in bytes of the POST which will be handled by the container FORM URL parameter parsing. The limit can be disabled by setting this attribute to a value less than or equal to 0. If not specified, this attribute is set to 2097152 (2 megabytes). Another Limit is: maxHttpHeaderSize The maximum size of the request and response HTTP header, specified in bytes. If not specified, this attribute is set to 4096 (4 KB). You find them in $TOMCAT_HOME / conf / server . xml   1.Find the tag entry for the HTTP 8080 connector 2.Add the maxPostSize="0" attribute and value to this tag such that it looks like:      maxHttpHeaderSize="8192"     maxThreads="150"     minSpareThreads="25"     maxSpareThreads="75"     enableLookups="false"     redirectPort="8443"     acceptCount="100" ...

How to find the companyId in Liferay.

In liferay many services require comapanyId. In lifeay company means portal instance. You can find this easily. Get comapanyId in code: PortalUtil.getDefaultCompanyId(); this give you default companyId. PortalUtil.getCompanyId(request); here request means HttpServletRequest. PortalUtil.getCompanyId(portletRequest);here portletRequest means any portletRequest(Render,Action or Resource).  ThemeDisplay themeDisplay=(ThemeDisplay)request.getAttribute(WebKeys.THEME_DISPLAY);themeDisplay.getCompanyId();  Get current companyId from specific thread local storage by:             CompanyThreadLocal.getCompanyId(); Get comapanyId manually: To find the company ID (portal instance ID), follow these steps: 1/ Sign into portal as admin user 2/ Navigation the Portal Instances admin page Navigation Path is "Dockbar > Admin > Control Panel > (Configuration) Portal Instances 3/ Review first column  "Instance ID" The i...

How to set ant_opts in ubuntu

Edit ~/.bashrc Open terminal and execute> $ sudo gedit .bashrc Above command open .bashrc file in edit mode. Now add the below two lines: ANT_OPTS="-Xmx1024m -XX:MaxPermSize=256m" export ANT_OPTS Now save the file and close terminal. Enjoy......

How to make Back Up and Restore from backup in MySQL Database.

In this tutorial i will show you easy ways to backup and restore the data in your MySQL database. Back up From the Command Line:   Here is the proper  syntax to backup in command> $ mysqldump --opt -u [uname] -p[pass] [dbname] > [backupfile.sql] Here: [uname] Your database username [pass] The password for your database (note there is no space between -p and the password) [dbname] The name of your database [backupfile.sql] The filename for your database backup [--opt] The mysqldump option  For example, to backup a database named 'ranger' with the username 'root' and with password 'root' to a file ranger_backup.sql, using command: $ mysqldump -uroot -proot ranger > ranger_backup.sql   Above command store your back file in home directory. If you want to backup in specific directory then go to your desired directory and execute the above command.   With mysqldump command you can specify certain tables of ...

How disable anonymous access to MongoDB OR Enable Client Access Control in MongoDB.

Securing MongoDB with Authentication and Authorization: Step 1) Login using shell and Create an admin account: Open terminal and login using below command>  mongo  Switch to admin db using below command> use admin;    Create Admin user using below command> db.createUser({ user : "admin", pwd : "pass", roles: [ { role: 'root', db: 'admin' } ] });   Now Give grand Roles to Admin User using below command:   db.grantRolesToUser( " admin ", [{ "role" : "readWriteAnyDatabase", "db" : "admin" },{ role: "userAdminAnyDatabase", db: "admin" }] ) Step 2) Test your admin account using below command> mongo -u admin -p pass --authenticationDatabase admin   Step 4) Force authorization  FOR MONGODB 3.0 AND ABOVE add these lines in /etc/mongod.conf>  security:   authorization: enabled FOR MONGO...

How to install mongodb in ubuntu 15.04

Step 1: If you install any packages of mongodb yet, firstly remove all. To remove all existing packages execute the below command: sudo apt-get purge mongodb-org sudo apt-get autoremove Remove the old mongodb source list you created in below folder. sudo rm /etc/apt/sources.list.d/   Remove db and logs: sudo rm -r /var/lib/mongodb sudo rm -r /var/log/mongodb Step 2: Add debian repository: echo "deb http://repo.mongodb.org/apt/debian wheezy/mongodb-org/3.0 main" |  sudo tee /etc/apt/sources.list.d/mongodb-org-3.0.list   Step 3: Update and install: sudo apt-get update sudo apt-get install -y mongodb-org Step 4: Start mongo db: sudo service mongod start or systemctl start mongod  Step 5: Configure MongoDB username and password: open mongo shell : (cntrl+alt+t) open termianl and type > mongo   then the shell is open. then switch the database typing> use admin then Create the r...