In this post you will learn how to use a micro framework called Spark to build a RESTful backend. The RESTful backend is consumed by a single page web application using AngularJS and MongoDB for data storage. I’ll also show you how to run Java 8 on OpenShift. Read the full blog here https://www.openshift.com/blogs/developing-single-page-web-applications-using-java-8-spark-mongodb-and-angularjs
Author: shekhargulati
Using Siege for quick and dirty load test — Apache Benchmark Alternative
Today, I wanted to run a quick and dirty load test on one of my applications. One of the alternative that most of the developers are aware of is Apache Benchmark(or ab). For some reason, ab does not work on my mac book because of Connection reset by peer errors. So, I looked around and found Siege. It is very similar to ab and works well on mac.
To test the GET request, you would run
siege http://example.com/rest/todos/3 -c 100 -r 100
To test the POST request, you would run
siege -H 'Content-Type:application/json' "http://example.com/rest/todos POST < ./data.json" -c 10 -r 1000
XWiki on OpenShift
1. Create an OpenShift application using following command
$ rhc app-create xwiki jbosseap –gear large
2. Downloaded the xwiki war file from the official web site.
3. Extracted the war file using $ jar xfv xwiki.war
4. Downloaded three jars — guice-servlet, guice, and h2 from http://mvnrepository.com/ and placed the jars in WEB-INF/lib directory. The application expects these jars but don’t bundle them.
5. Update the hibernate.cfg.xml. Basically, we have commented out hsql and uncommented h2. The NullPointerException that you were seeing was because xwiki does not work with JNDI datasource so you have to bind the url manually. If you want to use postgres or mysql then use proper connection url, username, password etc.
6. Pack the war file again using $jar cfv ROOT.war .
7. Copy the artifact to deployments folder in your app source code. Please delete src/ and pom.xml as you are deploying war file.
8. Git commit and push the war.
9. Check the logs.
Using Python Flask Jinja2 with Mustache
Today I was building a single page web application using Python Flask framework and Backbone.js and faced a problem where Jinja2 was parsing the mustache template. Both Jinja2 and Mustache use {{}} in their templates. When a user makes a first request, I render index.html that contains all my mustache templates as well. The solution to avoid Jinja2 from parsing Mustache templates is to put all the templates inside {% raw %} and {% endraw %} as shown below.
{% raw %}
<script type="text/template" id="company-template">
<a href="#companies/{{id}}/jobs" class="list-group-item">
<h4 class="list-group-item-heading">{{name}}</h4>
<p class="list-group-item-text">{{description}}</p>
</a>
</script>
{% endraw %}
How to exclude a package from CDI scanning using @Vetoed annotation
Create a new Java file called package-info.java in the package you want to exclude. Then add the following code
@Vetoed package org.shekhar.cdidemo.example; import javax.enterprise.inject.Vetoed;
PostgreSQL learning
To find out the location of PostgreSQL configuration files, just connect with your PostgreSQL database and run the following command.
SELECT name, setting FROM pg_settings WHERE category = 'File Locations';
To describe a table, you should use
\d+ table_name
To list all the databases,you should run following command.
\list \l
To view all the tables in a schema
\dt+ pg_catalog.pg_t*
To view core settings of a PostgreSQL server, you should use
SHOW ALL;
To view value of a specific setting like effective_cache_size
SHOW effective_cache_size;
To reload the configuration changes
SELECT pg_reload_conf();
To create a new LOGIN user
CREATE ROLE shekhar LOGIN PASSWORD 'p@ssw0rd';
To create a new database
CREATE DATABASE myappdb;
Schema are logical compartments in a database. You can divide your database into small logical schemas.
CREATE SCHEMA etcs;
To view all the available extensions
SELECT * from pg_available_extensions;
To view details about an extension
To view all the available extensions
\dx+ plpgsql;
To install an extension and view its details
postgres=# CREATE EXTENSION fuzzystrmatch;
CREATE EXTENSION
postgres=# \dx+ fuzzystrmatch;
Objects in extension "fuzzystrmatch"
Object Description
----------------------------------------------------------------------------
function difference(text,text)
function dmetaphone(text)
function dmetaphone_alt(text)
function levenshtein(text,text)
function levenshtein(text,text,integer,integer,integer)
function levenshtein_less_equal(text,text,integer)
function levenshtein_less_equal(text,text,integer,integer,integer,integer)
function metaphone(text,integer)
function soundex(text)
function text_soundex(text)
(10 rows)
To view the active running processes
postgres=# SELECT pid, usename from pg_stat_activity; pid | usename ------+---------- 1982 | postgres 2742 | postgres 2236 | postgres 2723 | shekhar 2744 | postgres (5 rows)
To kill a connection
postgres=# SELECT pg_terminate_backend(2723); pg_terminate_backend ---------------------- t (1 row)
To kill all the connection by user shekhar
SELECT pg_terminate_backend(pid) FROM pg_stat_activity where usename = 'shekhar';
To run a script file, run the following command.
psql -f <path_to_filefile>
You can also run commands with psql non-interactively
psql -d mydb -c "CREATE SCHEMA test;"
The \set command can be used to create user defined shortcuts like as shown below.
\set connections_check 'SELECT pid, usename from pg_stat_activity;'
The .psqlrc file can be used to define configurations for a session. You can use PSQLRC environment variable to control the location of the startup file.
\pset null 'Null' \encoding latin1 \set PROMPT1 '%n@%M:%>%x %/#' \set PROMPT2 \timing on \pset pager always
To turn on timing for the queries execution time
\timing
You can call OS shell commands from within psql as shown below.
admin2pnvtk8:myapp#\! env|grep POSTGRES
To view uptime of your PostgreSQL database in minutes, run the following query.
myapp=# select date_trunc('minute',current_timestamp - pg_postmaster_start_time()) as "postgresql_uptime";
postgresql_uptime
-------------------
00:12:00
(1 row)
How to Host your Java EE Application with Auto-scaling
OpenShift is an auto-scalable Platform as a Service. Auto-scalable means OpenShift can horizontally scale your application up or down depending on the number of concurrent connections. OpenShift supports the JBoss application server, which is a certified platform for Java EE 6 development. As an OpenShift user, you have access to both the community version of JBoss and JBoss EAP 6(JBoss Enterprise Application Platform) for free. In this blog post, we will learn how to host a scalable Java EE 6 application using a JBoss EAP 6 server cluster running on OpenShift. Read the full blog here https://www.openshift.com/blogs/how-to-host-your-java-ee-application-with-auto-scaling
Solving Pusher HTTPS Issue
If you use Pusher then you might face following error. The error happens when you try to use Pusher client library from https.
The page at 'https://shekhargulati.com/blog/1#/' was loaded over HTTPS, but ran insecure content from 'http://js.pusher.com/2.1/pusher.min.js': this content should also be loaded over HTTPS.
To fix this error, you should use Pusher Cloudfront CDN version as shown below. In your HTML or other template use following. Replace 2.1 with your own version.
<script src="//d3dy5gmtp8yhk7.cloudfront.net/2.1/pusher.min.js" type="text/javascript"></script>
How to run Grails Application with Jenkins on OpenShift
Yes, you can run Grails applications on OpenShift. Follow the steps mentioned below to deploy Grails apps via Jenkins on OpenShift.
Step 1 : Create Tomcat 7 application with Jenkins
$ rhc app-create grailsapp tomcat-7 --enable-jenkins
Step 2 : Delete template code
$ cd grailsapp $ git rm -rf src/ pom.xml $ git commit -am "deleted template code"
Step 3: Generate Grails app
Use grails command line or IDE to generate a Grails project.
Step 4: Copy the Grails app
Copy the source code of your grails app in the grailsapp folder. The grailsapp corresponds to OpenShift application.
Step 5: Create pre_build action hook
Create an OpenShift action hook
touch .openshift/action_hooks/pre_build chmod +x .openshift/action_hooks/pre_build
Copy the following in pre_build hook
#!/bin/bash
# This is a simple script and will be executed on your CI system if
# available. Otherwise it will execute while your application is stopped
# before the build step. This script gets executed directly, so it
# could be python, php, ruby, etc.
set -x
if [ ! -d $OPENSHIFT_DATA_DIR/grails-2.3.4 ]
then
mkdir $OPENSHIFT_DATA_DIR/.grails
cd $OPENSHIFT_DATA_DIR
wget http://dist.springframework.org.s3.amazonaws.com/release/GRAILS/grails-2.3.4.zip
unzip grails-2.3.4.zip
rm -f grails-2.3.4.zip
fi
Step 6: Create build action hook
Create an OpenShift action hook
touch .openshift/action_hooks/build chmod +x .openshift/action_hooks/build
Copy the following in build hook
#!/bin/bash # This is a simple script and will be executed on your CI system if # available. Otherwise it will execute while your application is stopped # before the build step. This script gets executed directly, so it # could be python, php, ruby, etc. set -x export GRAILS_HOME=$OPENSHIFT_DATA_DIR/grails-2.3.4 export PATH=$GRAILS_HOME/bin:$PATH cd $OPENSHIFT_REPO_DIR export GRAILS_OPTS="-Xmx512m -Xms256m -XX:MaxPermSize=256m" grails -Dgrails.work.dir=$OPENSHIFT_DATA_DIR.grails prod war
Step 7: Commit and push the changes
Commit and push the changes
$ git add . $ git commit -am "app" $ git push
Now watch your Jenkins build. If it fails, I guess it would be because of memory issues. Try and use bigger gear sizes. Grails is memory hungry.
Github repository with sample app code https://github.com/shekhargulati/grails-jenkins-openshift-example
2013 in review
The WordPress.com stats helper monkeys prepared a 2013 annual report for this blog.
Here’s an excerpt:
The Louvre Museum has 8.5 million visitors per year. This blog was viewed about 110,000 times in 2013. If it were an exhibit at the Louvre Museum, it would take about 5 days for that many people to see it.
