Published on August 28, 2010 by Amir Sedighi
Introduction
Here you will find how to monitor an application which is hosted in Apache Tomcat. There are many reasons for monitoring an application in run time. In our case the client was looking for some measurable factors that shows slap of business ratios on demand. Moreover, the technical department was looking for fine-grain informations such as user uptime, most visited pages, idle time and any possible exception that occurs in run time. JMX is the perfect answer for this kind of requirements. Connecting to the Tomcat using JMX has a straight forward procedure.
Let Tomcat listens to the out side world
We should declare multiple environmental variables that notify Tomcat for using JMX. They also specify the listener port.
There are different ways to introduce environment variables to Tomcat. But, I’v found that using setenv.sh in Linux or setenv.bat in Windoes is the best one:
Create “setenv.sh” with exactly below content:
export CATALINA_OPTS="-Dcom.sun.management.jmxremote
-Dcom.sun.management.jmxremote.port=9898
-Dcom.sun.management.jmxremote.authenticate=false
-Dcom.sun.management.jmxremote.ssl=false
-Djava.rmi.server.hostname=100.101.102.103"
Now make it executable:
chmod +x setenv.sh
Note: 9898 is the JMX listener on tomcat. 100.101.102.103 is the IP of tomcat server.
2. Put “setenv.sh” in “bin” directory of tomcat.
3. Turn the fire wall off or run below command on the Tomcat machine:
sudo /sbin/iptables -I INPUT -s 100.101.102.150 -p tcp --destination-port 9898 -j ACCEPT
Note:
100.101.102.150 is the JConsole machine. Also 9898 is the same as port that you defined in setenv,sh
4. Re-start tomcat.
5. Run jconsole and connect to a remote process using : 100.101.102.103:9898
The MBean objects are observe able now. Also you may call remote behaviors that are implemented the MBean interface.
Trouble Shooting
The procedure seems to be easy. However, I have some stupid troubles:
-First of all be sure you there is no any space around “=” in setenv.sh because, the syntax is pretty important.
-Also for checking the listener use telnet to find out the port is open and is listening.
-Check /etc/hosts if it is correct or not. In my case a wrong definition made troubles.
Posted in Uncategorized
Published on August 25, 2010 by Amir Sedighi
Introduction.
A business company was looking for a proper method to monitor it’s business applications. They were almost implemented by Java. Firstly I was going to implement another application for this purpose. Fortunately a friend of mine shows the way. We planned to use JMX. Here is a tutorial that clearly shows you how to let a Java application to be exposed using JMX.
At first have a look on JMX technical introductions. I used to send SNMP messages for making a communication between applications during run time JMX has many advantages over SNMP. JMX simply enables you to start a listener in the application that could be watched. This makes the monitor side enable to touch and trace public methods of every classes that implements MBean interface. Dont worry if you have a legacy application that is not armored with MBean interfaces. You need just develop an API that expose the critical variables for the out side.
MBean Interface
I developed an interface that its name finished by MBean. This is a contract that should be respected to continue.
package com.datispars.monitoring;
public interface JmxSampleMBean {
public void getMessage();
public int add(int x, int y);
public String getTitle();
public int getTimeOut();
public void setTimeOut(int size);
}
MBean Implementation
Now I just implemented an implementation of the MBean. The sample is as much easy as talk without a comment.
package com.datispars.monitoring;
public class JmxSample implements JmxSampleMBean {
private final String title = "JMX Sample";
private int timeOut = 20;
public void getMessage() {
System.out.println("Hi," + getTitle());
}
public int add(int x, int y) {
int result = x + y;
System.out.println(x + "+" + y + "= " + result);
return result;
}
public String getTitle() {
System.out.println("the " + title + " has been called.");
return this.title;
}
public int getTimeOut() {
return this.timeOut;
}
public synchronized void setTimeOut(int value) {
this.timeOut = value;
System.out.println("time out has been changed to " + this.timeOut + " seconds.");
}
}
MBean Factory
The main method consists a MBean server and a MBean registration and nothing else:
package com.datispars.monitoring;
import java.lang.management.*;
import javax.management.*;
public class Main {
public static void main(String[] args) throws Exception {
// instantiating MBean server
MBeanServer mbServer = ManagementFactory.getPlatformMBeanServer();
// declaring name and type of MBean
ObjectName mBeanName = new ObjectName("com.datispars.monitoring:type=JmxSample");
// instantiating MBean
JmxSample jmxSample = new JmxSample();
// regisring onject as MBean
mbServer.registerMBean(jmxSample, mBeanName);
// waiting for jmx requests.
System.out.println("Listening...");
Thread.sleep(Integer.MAX_VALUE);
}
}
Big Brother Is Watching You
Now it is the time to watch what is happening within the application. Run the application. Open a terminal and just run “jconsole“. Click on the local application name. The jconsole looks fantastic!. Alive diagrams that draw by the information which are provided by JVM let you know how is the situation. Go to the MBean tab and find the MBean declarations of your application. Call “add” with integer numbers. Have a look on the console output of the application. It is great! That is why I love Java. You can find always engineering solutions for the problem.
In next part we will set up an Apache Tomcat monitoring with MBean objects.
Posted in Uncategorized
Published on July 10, 2010 by Amir Sedighi
Introduction
Here is an approach to speed up object/service invoking. This solutions may apply to a web-based application or any other service that works using a series of interactions and transactions. Here I don’t refer to any specific technology or platform, because it is a pretty general technique. It is all about sending a bigger message that carries more parameters for multiple methods instead of sending multiple messages to the multiple methods. In other word this article is about favoring fatty messaging instead of chatty messaging.
Converting A “Chatty” Messaging To A “Fatty” One
Monitoring an application in runtime appears that how much a big UI transaction may consists a lot of inner interactions between client application and server side services. This may happens even between servers such as application server and database server. In many cases you need to perform a macro interaction that needs many input parameters to get finish. These input parameters may should provides by user or any other external system. Below sample shows a chatty approach that gets some input parameters at start point and some others during the process.
Assume we have below three methods in class B:
public int sum ( int a , int b ) {
return a + b;
}
public int multi ( int a , int b ) {
return a * b;
}
public int power ( int a , int b ) {
int result = 1;
for ( int a = 1 ; a < b ; a ++ ){
result = result * a ;
}
return result;
}
Now if class A use to call these three methods sequentially then we will have such a sequence as below:
A (Caller) B (Callee)
----------------------------------------------------------------------
B bObject = new B();
firstVal = bObject.sum(1 , 2)
B returns 3
secondVal = bObject.multiply( firstVal , 2 )
B returns 3 * 2 = 6
thirdVal = bObject.power( secondVal , 2 )
B returns 6 * 6 = 36
Assume B hosted in a remote machine. Something like RMI or even web-service calling. As you can see we have three remote method invoking that all are synchronized. Regardless of process consuming in B machine, we have at least 3 remote call that each one has one return value passing. Instead of this chatty talking between A and B we can use an onetime fatty calling by implementing a wrapper in B as below:
public int whatANeeds ( int a , int b, int multiValue, int powerValue ) {
int firstVal = this.sum(a,b);
int secondVal = this.multi(firstVal, multiValue );
return this.power(secondVal, powerValue );
}
Now sequence of remote method calling will be as below:
A (Caller) B (Callee)
----------------------------------------------------------------------
B bObject = new B();
final = bObject.whatANeeds (1 , 2 , 2 , 2);
B returns 36
An Old Fashion High Performance Approach
We can extend this kind of method invoking in another special that B is a RDBMS. Assume B is a RDBMS and A is the application server that calls some queries or runs some data manipulating SQL commands remotely. In this case stored-procedures could be useful. Everybody knows the most significant bottleneck is the low capacity of I/O and data transfer systems. So calling server-side methods in fatty messaging is always pretty effective in improving performance. Implementing stored-procedures in RDBMS is so old fashion for new age of developers. But it is really the fastest way that helps a business application to run faster. As a result we almost can convert multiple chatty methods invoking to a fatty one that runs faster. This could be useful everywhere in internal or external method/service invoking.
Posted in Software Engineering
Published on July 7, 2010 by Amir Sedighi
Introduction
Multi-threading is a basic technique in programming languages. Many applications and software services work using threads. Actually in OS level whatever runs are threads. Every programming language has its syntax and commands to use threading.
An interactive reach user interface should use multithreading. However JavaScript didn’t support multi-threading before HTML5. Now developers are able to use multi-threading features that support by new version of JavaScript within HTML5.
Web Worker
HTML 5 solution for implementing multi-threading is Web Worker. Web Workers let you run multiple processes in the background concurently. All workers run at the same time with together depends on the client computer speed. Using this threads you can make a network request, calculation or persisting data using local storage or fetchnig data from a database server.
Check if your browser supports Web Workers or not. Below function is undefined if it doesn’t.
function supports_web_workers() {
return !!window.Worker;
}
A Sample
Here you can find some samples of HTML 5. But I like to review another example together. Check this out.
I just want to highlight below parts. You can find the complete code here.
Here we have two separated thread. Ticker and Searcher:
// TICKER
var symbol = 'GOOG'; // default symbol to watch
var ticker = new Worker('ticker.js');
// SEARCHER
var searcher = new Worker('searcher.js');
function search(query) {
searcher.postMessage(query);
}
These thread work together at the same time. For JavaScript developers implementing of this kind of processes was just as a so far dream even in few years ago.
Posted in JavaScript
Published on July 5, 2010 by Amir Sedighi
Introduction
Ajax brings some kind of flexibility for web developers. Using Ajax developers are able to develop lazy routines much easier. Ajax libraries and API let the front-end events to fire their own server-side handlers on demand. This is a great facility because whatever you make a page lighter then you have a faster loading. In the opposite side your application will be lazy in sending responses to the user actions. Here is a trade-of and HTML5 has a proper solution for decreasing response time.
Local Storage
LocalStorage is part of HTML5 standard specification that becomes very useful for caching server-side information into the client machine. HTML5 storage stays on your computer and works even when you reopen the browser and go to the site that you stored its information before.
Actually it is a memory space provides by the browser that websites can use to persistent data that doesn’t need to be sent to the server at the moment. It also can be used for pre-buffering.
LocalStorages remembers all fields that you fetched from the server side database. LocalStorage also increase the amount of downloads of a partial network. Lack of some kind of wide caching system within a business application makes interactive applications slow.
There are plenty of requirements that LocalStorage will get them the proper answer such as buffering and off-line facility.
LocalStorage is a key/value storage. Moreover web browser and API vendors will have their specific implementations that provides such as api for accessing stored data by a semi SQL.
To finding out if your browse support HTML 5 run below just function:
function isMyBrowserSupportLocalStorage() {
return !(window['localStorage'] && ('localStorage' in window) == null);
}
Below javascript lines shows that how localStorage helps the developer for maintaining variables:
globalStorage[''].globalVariable= 'This means globalVariable to be used by anyone';
globalStorage['org'].foo1 = 'Accessible for *.org websites';
globalStorage['blog.datispars.com'].companyVar= 'This is a value that can be accessed by the company website';
Same-Origin Restriction
A LocalStorage is not pretty secure as server-side remote databases. Every application or user that who has access to the physical storage probably will access to your LoclalStorage. Moreover any web site can read and modify its own values within a database. Websites can not access other websites databases. This has been called a same-origin restriction.
Conclusion
JavaScript developers used to cache the application informations into memory using structure that they built by coding into the memory such as array, lists and formatted strings. Some 3rd party tools and API have been came to make client side storing easier and more effective such as SQLLite. Now in a big improvement HTML5 is the first version HTML standard that supports local storages within contract. After this developers will be able to provide faster and more reach applications.
Posted in JavaScript
Published on July 5, 2010 by Saeid Zebardast
To see some part of a text file, use the following methods:
1- To get top 20 lines of a file header, use:
$ head -n20 FILE
2- To get 30 line of bottom of a file, use:
$ tail -n30 FILE
3- If you want to print some lines that they are not in the first part or in the last part of a file, you can use:
$ more +num10 FILE | head -n20
The previous command print from line 10 to 30 (20 lines) of FILE.
Tags: Command Line, Linux
Posted in Linux
Published on July 3, 2010 by Amir Sedighi
Previous Part.
Sanctions
Economic sanctions has prevented large scale companies and products to be present in Iran’s software market. But smaller companies pass it like mice that pass a wall by its cavities. In other hand Software is not such an industry that works using huge and complex manufactures. Software development just needs human and knowledge and not more. Moreover most of scientific fields have their own opensource products that consists a lot of implemented knowledge within procedures and behaviors. Indeed, economic sanctions just specialized software industry of Iranian engineers rather restrict them at all. Internet basically designed to makes available knowledge and break censorship. It seems Software is not a kind of restrict-able knowledges. Iranian software development teams favor Microsoft .Net and Php for general purposes applications.
Operating System
MS-Windows has captured Iranian operating system market for many years. Microsoft Windows keeps its share market but something starts to change. These are some years that Linux flavors are getting popular between Iranian software developers. So here are many local products on the shelf that uses Linux flavors as platform. Even Iranian universities developed an Iranian version of Linux. New edition of rich client Linux such as Ubuntu are getting their community between Iranian software professionals. Moreover, Linux already has captured most of networking servers market share. All ISP’s prefer to use Linux or Free BSD server rather than Windows. Windows has home using market yet, but I think it is changing. Unauthorized Apple resellers sell latest Apple’s products more than ever. iPhone and MacBook Pro are the most popular electronic products between Iranian youth.
In Minicomputer and Mainframe industries Sun and IBM products are more popular for Iranians. IBM is trust-able and Iranian companies accept to pay twice or more times of price of a IBM Z series mainframe for high transactional businesses. There are some difficulties for spare parts and technical references. But noting is impossible here.
Software Development
We reviewed in past part that Copyright doesn’t support for software products in Iran. So Iranian students almost never pay for any software during getting familiar with software platforms. Cracked software are available always. You don’t have to pay for them. Even you like to pay, There are no authorized reseller. Even if you find a reseller he isn’t able to sell you because of economic sanctions. So you have to download and use softwares without any responsibility. In this case Microsoft, JBoss, IBM, Oracle products are more favorite. For example Oracle databases are using widely by large scale organizations. In many cases they prefer to buy from an authorized branch. The only Iranian branch of Oracle is not support by Oracle any more. So the Iranian Oracle administrator, will buy the enterprise edition by paying something less than 5$ and deploy it after some challenges. MS-SQLServer is the most favorite database in this market I think. Also MySQL gets famous by using in web-based projects. I also worked with DB2 in some cases. So In database business all databases are available. PHP, C#.Net and Java are the most popular technologies. .Net has thousands programmers. PHP has its market share and is getting larger day by day. J2SE and J2EE almost are available. Spring, JBoss and other frameworks are using widely. Indeed in enterprise area Java technology is the major one that follows COBOL legacy projects. In fact, J2EE using a vast of opensource technologies is the most favorite platform for Iranian companies who are working in enterprise level of the business such as Banking. Some companies has been accomplished their enterprise projects using Delphi or .Net, However now Java in enterprise level has no competitor. ERP solutions is a main competition advantage for large scale companies. But actually economic sanction has limited this market for western ERP companies. So Indian or Singaporean companies try this area more than European. Also some Iranian companies have tried to localize open source ERP solutions. Lack of technical reference and legal supports has effected ERP and specific industries more than general areas.
Integration
Following limitations and sanctions, software market has no a complete harmony between solutions and platforms. So integration is a rich market in Iran. I as a senior developer or consultant have worked in many integration projects. In fact integration is an inseparable part of large scale projects. In Iran a large scale software project implementation is just like somehow to completing a puzzle that you don’t have all pieces. The map is out of date. Moreover you have to use some pieces of other different puzzles to cover some areas of your puzzle with them. Some how there are missing pieces that you have to develop them. Finely you have to integrate them all. This is why I told that Iranian software market is something different.
Iranian Developer Teams, Working style
Following what I told, here you can find talented and solid developers who they know how to find the way. They almost have learned how to develop using minimum resources. They know how to integrate tools and products. They know how to dive into a unfamiliar technology. In this way they have a lot of, difficulties that even aren’t easy to imagine for other countries. Sun JDK’s, Source Forge, Oracle, Google Codes and many other libraries are closed to them. In other hand governmental restriction and filtering prevented Iranian students to search freely. At this time that I am writing this, WordPress and some other giant blogs are bocked to Iranians by some local rules and restrictions. Low speed internet connection is another problem. But, with all restrictions and problems the Iranian Software market keeps growing.
Posted in Software Engineering
Published on July 3, 2010 by Saeid Zebardast
Sometimes you need to get reports or something like, that runs a complex query which includes `JOIN`, `GROUP BY`, `HAVING` and etc. The time of running is depends on amount of records and complexity. To run queries faster we can use temporary tables.
How Temporary table helps us
- It is faster to get data from temporary table instead of getting data form DB tables with several filter conditions.
- Temporary tables are connection specific. When the current client session terminates all the temporary tables are automatically deleted.
- Temporary table with same name can be created with in different connections.
How to use Temporary table in MySQL?
Creating temporary table is the same as creating regular table. I created the `user_usage_report` temporary table using below command:
CREATE TEMPORARY TABLE user_usage_report
SELECT u.id, o_u.size AS out_size, i_u.size AS in_size
FROM user u
INNER JOIN out_usage o_u ON o_u.user_id=u.id
INNER JOIN in_usage i_n ON i_n.user_id=u.id
WHERE
o_u.start_date BETWEEN '2009-01-01' AND NOW()
AND
i_u.start_date BETWEEN '2009-01-01' AND NOW()
GROUP BY u.id
Next I run below select query through the same connection:
SELECT id, in_size, out_size FROM user_usage_report ORDER BY in_size, out_size
After using the temporary table, I realized that it acquires results several times faster than pure select. Just try it!
Tags: MySQL, Optimization
Posted in MySQL
Published on July 1, 2010 by Amir Sedighi
Introduction
Iranian software companies favor some technologies and platforms, however they almost didn’t enter to other zones that are popular in other parts of the world. Here you will find a quick overview about favorite technologies that are in use by Iranian software companies and individual experts.
Iranian Software Market, Separated Just Like an Iceland
Following special political situation Iran’s software market has a special characteristic that you cant compare it with other countries in some ways. Following economic sanction and reducing international trades in high-tech area, Iranian software industry has passed its specific growing path among the years. Some technologies are getting more popular and some others remain unknown even for the best companies.
Also regarding to the global motivations of immigration, most of young talented Iranian engineers leave their motherland toward overseas. Some of them come back to Iran after a while to start new businesses. Some others try to outsource software projects to Iranian teams and individuals.
Copyright
Most of Iranian students doesn’t have any respect for Copyright rules likewise the government. So software duplicators have a large black legal market! Some how software brokers catch more than the software producer. I believe this is a side effect of restrictions. Anyway lack of copyright rule let them download anything and use without a cost and without any authorized support.
Lack of legal Services
Lack of legal branches and authorized software resellers, provides a huge market for companies and individual persons who can support prevented technologies that are under economic sanction policies. For example you may find an individual engineer who used to work with IBM mainframes in past years and now he supports some large mainframes alone.
In this area there are many hardware companies that they are importing hardware and high-tech technologies much expensice than their international costs without providing any support to their customers. They sell complex machines just like a chewing gum. “No return, no money back, no support, just take it and go.” In this complex and difficult market some customers prefer to have a low quality Huawei router that is supported by chines engineers rather than an unsupported brilliant Cisco device.
Every Threat is an Opportunity
Software market is not an exception too. In software business lack of legal branches makes a large market. However software industry grows locally to capture market of giants companies such as People Soft, SAP and IBM. Lack of authorized branches gives a great opportunity to some Iranian companies to gaining a lot of money by producing localized business applications. Some local companies now reached such as popularity that knowledge of using their applications is a pretty valuable skill. They runs their rules in general business application development. Iranian people are not familiar with English language as many other countries so they prefer to use localized business applications. As I mentioned Iran software market uses some special platforms and technologies instead of some other. In next part I introduce such technologies that use by Iranian companies widely.
Next Part.
Posted in Software Engineering
Published on June 28, 2010 by Amir Sedighi
Introduction
This article have a quick overview of Continuous Integration, concepts and its current usage.
“Continuous Integration is a software development practice where members of a team integrate their work frequently, usually each person integrates at least daily – leading to multiple integrations per day. Each integration is verified by an automated build (including test) to detect integration errors as quickly as possible. Many teams find that this approach leads to significantly reduced integration problems and allows a team to develop cohesive software more rapidly. “ – Martin Fowler
Major Parts
When we implement a modification such as change, bug-fix or improvement thorough a software code, almost causes new bugs and failures that it could be better to find and fix them as soon as possible. The proper solution that has recommended by most of professionals is using of a continuous integration system within the development process.
Repository
The first and main tools of team development is the repository. All artifacts required should be placed in the repository. Repository keeps and maintain code versions and help us to track the development steps one by one. Repository also enables the team members to share their results together.
Build System
One of continuous integration disciplines is that, a single command should have the capability of building whole of the system. This means much easier and faster product generation.
There are many tools on the shelf that make us enable to build the application such as Ant and Maven.
Testing
Obviously we prefer to find bugs as soon as possible. Using automated testing the build system will be enable to run tests, find bugs and notify us automatically. In a team development everybody may commit partial cods within a day. Early building and testing is a key technique for finding bugs and incompatibilities. Build systems are able to run tests and find bugs immediately when the programmers commit the code or at least in a short time. The theory is that every commit should be qualified.
Advantages
Encourage using of source controls and unit testing that are the best practices of software development.
Early warning of code failures and bugs
Early running of unit tests.
Fast and easy building system.
Easy support for multiple branches of code.
It should be easy to find out who made the relevant changes to the new bug.
Posted in Software Engineering
No Comments