Friday, February 28, 2014

Notes on Design Pattern

Too Many Parameters in Java Methods, Part 3: Builder Pattern
http://java.dzone.com/articles/too-many-parameters-java-1
Achieving Immutability with Builder Design Pattern
http://java.dzone.com/articles/immutability-with-builder-design-pattern

Thursday, February 27, 2014

Notes on Java Threads and Concurrency

Java Concurrency: Atomic Variables
http://www.baptiste-wicht.com/2010/09/java-concurrency-atomic-variables/
http://ifeve.com/atomic-operation/
AtomicStampedReference
使用循环CAS实现原子操作
在Java并发包中有一些并发框架也使用了自旋CAS的方式来实现原子操作,比如LinkedTransferQueue类的Xfer方法。CAS虽然很高效的解决原子操作,但是CAS仍然存在三大问题。ABA问题,循环时间长开销大和只能保证一个共享变量的原子操作。
DK提供了AtomicReference类来保证引用对象之间的原子性,你可以把多个变量放在一个对象里来进行CAS操作。
public class Stack {
    private final AtomicReference<Element> head = new AtomicReference<Element>(null);

    public void push(String value){
        Element newElement = new Element(value);

        while(true){
            Element oldHead = head.get();
            newElement.next = oldHead;

            //Trying to set the new element as the head
            if(head.compareAndSet(oldHead, newElement)){
                return;
            }
        }
    }

}

http://sns.ruanko.com/space.php?uid=60391&do=blog&id=186531
workerCount:当前活动的线程数;
private final AtomicInteger ctl = new AtomicInteger(ctlOf(RUNNING, 0));
这个是用一个int来表示workerCount和runState的,其中runState占int的高3位,其它29位为workerCount的值。
用AtomicInteger是因为其在并发下使用compareAndSet效率非常高;
当改变当前活动的线程数时只对低29位操作,如每次加一减一,workerCount的值变了,但不会影响高3位的runState的值。
当改变当前状态的时候,只对高3位操作,不会改变低29位的计数值。
http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/7u40-b43/java/util/concurrent/ThreadPoolExecutor.java#ThreadPoolExecutor
因为CAPACITY值为:00011111111111111111111111111111,所以&操作将参数的高3位置0了
 * 保留参数的低29位,也就是workerCount的值
private static int workerCountOf(int c)  { return c & CAPACITY; }

private static int runStateOf(int c)     { return c & ~CAPACITY; }
~为按位取反操作,则~CAPACITY值为:11100000000000000000000000000000
 * 再同参数做&操作,就将低29位置0了,而高3位还是保持原先的值,也就是runState的值

 * 将runState和workerCount存到同一个int中
private static int ctlOf(int rs, int wc) { return rs | wc; }

不知道大家有没有注意到,当当前活动的线程数量 >= corePoolSize 的时候,都是优先添加到队列中,直到队列满了才会去创建新的线程

http://blog.csdn.net/novelly/article/details/16344105
http://www.ticmy.com/?p=243

1. 添加一个task的过程
当要添加一个新的task,如果当前线程数小于 corePoolSize,直接添加一个线程,即使当前有空闲的线程。否则添加队列中。如果队列满了呢?则会判断当前线程数是否小于maximumPoolSize,如是则添加一个新的线程用来执行该task。如果超出最大线程数,那就只能reject了。
http://xiaoz5919.iteye.com/blog/1746217
http://tutorials.jenkov.com/java-util-concurrent/threadpoolexecutor.html
https://github.com/kimchy/kimchy.github.com/blob/master/_posts/2008-11-23-juc-executorservice-gotcha.textile

Java Fork/Join
ForkJoinPool:
http://www.javabeat.net/simple-introduction-to-fork-join-framework-in-java-7/
Fork/Join addresses the need for divide-and-conquer, or recursive task-processing in Java programs 
Fork/Join's logic is very simple: (1) separate (fork) each large task into smaller tasks; (2) process each task in a separate thread (separating those into even smaller tasks if necessary); (3) join the results.
It is an ExecutorService for running ForkJoinTasks and also managing and monitoring the tasks. It employs worker-stealing algorithm where in idle threads can pick subtasks created by other active tasks and execute them. In this way there is efficient execution of tasks spawned by other tasks.

http://ifeve.com/talk-concurrency-forkjoin/
Fork/Join框架提供了以下两个子类:
RecursiveAction:用于没有返回结果的任务。
RecursiveTask :用于有返回结果的任务。
ForkJoinPool :ForkJoinTask
需要通过ForkJoinPool来执行,任务分割出的子任务会添加到当前工作线程所维护的双端队列中,进入队列的头部。当一个工作线程的队列里暂时没有任务时,它会随机从其他工作线程的队列的尾部获取一个任务。
ForkJoinPool由ForkJoinTask数组和ForkJoinWorkerThread数组组成,ForkJoinTask数组负责存放程序提交给ForkJoinPool的任务,而ForkJoinWorkerThread数组负责执行这些任务。
public class ForkJoinPool extends AbstractExecutorService 
     * Array serving as submission queue. Initialized upon construction.
    private ForkJoinTask<?>[] submissionQueue;
    private final ForkJoinWorkerThreadFactory factory;
pushTask方法把当前任务存放在ForkJoinTask 数组queue里。然后再调用ForkJoinPool的signalWork()方法唤醒或创建一个工作线程来执行任务
ForkJoinTask的join方法实现原理。Join方法的主要作用是阻塞当前线程并等待获取结果
http://coopsoft.com/ar/CalamityArticle.html

http://www.ibm.com/developerworks/library/j-jtp11137/
Merge sort is an example of a divide-and-conquer algorithm, where a problem is recursively broken down into subproblems, and the solutions to the subproblems are combined to arrive at the final result. Divide-and-conquer algorithms are often useful in sequential environments but can become even more effective in parallel environments because the subproblems can often be solved concurrently.
Result solve(Problem problem) { 
    if (problem.size < SEQUENTIAL_THRESHOLD)
        return solveSequentially(problem);
    else {
        Result left, right;
        INVOKE-IN-PARALLEL { 
            left = solve(extractLeftHalf(problem));
            right = solve(extractRightHalf(problem));
        }
        return combine(left, right);
    }
}
This kind of parallel decomposition is often called fork-join because executing a task forks (starts) multiple subtasks and then joins (waits for completion) with them.
Note that the subproblem() method does not copy the elements; it merely copies the array reference and offsets into an existing data structure. 

A ForkJoinExecutor is like an Executor in that it is designed for running tasks, except that it specifically designed for computationally intensive tasks that do not ever block except to wait for another task being processed by the same ForkJoinExecutor.

Conventional thread pools are designed for tasks that are independent of each other and are also designed with potentially blocking, coarse-grained tasks in mind — fork-join solutions produce neither. Fine-grained tasks in conventional thread pools can also generate excessive contention for the task queue shared among all workers.

Work stealing
Fork-join is a technique that makes it easy to express divide-and-conquer parallel algorithms.
Fork-join embodies the technique of divide-and-conquer; take a problem and recursively break it down into subproblems until the subproblems are small enough that they can be more effectively solved sequentially. The recursive step involves dividing a problem into two or more subproblems, queueing the subproblems for solution (the fork step), waiting for the results of the subproblems (the join step), and merging the results. 

http://www.java-allandsundry.com/2012/08/mergesort-using-forkjoin-framework.html
http://www.oracle.com/technetwork/articles/java/fork-join-422606.html

The issue is that of parallelism: When a Callable waits for the result of another Callable, it is put in a waiting state, thus wasting an opportunity to handle another Callable queued for execution.

Wednesday, February 26, 2014

Notes on Solr Query and Relevancy Tuning

How do I give a negative (or very low) boost to documents that match a query?
http://wiki.apache.org/solr/SolrRelevancyFAQ
Correct way:
q =  foo^100 bar^100 (: -xxx)^999  

==> The following query doesn't work as it will boost that match a little bit.
q = foo^100 bar^100 xxx^0.00001    # NOT WHAT YOU WANT

Scoring
Term frequency—tf: The more times a term is found in a document's field, the higher the score it gets. This concept is most intuitive
Term frequency—tf: The more times a term is found in a document's field, the higher the score it gets. This concept is most intuitive.
Co-ordination factor—coord: The greater the number of query clauses that 
match a document, the greater the score will be. Any mandatory clauses 
must match and the prohibited ones must not match, leaving the relevance of 
this piece of the score to situations where there are optional clauses.
Field length—fieldNorm: The shorter the matching field is, measured in 
number of indexed terms, the greater the matching document"s score will 
be. Norms for a field can be marked as omitted in the 
schema with the omitNorms attribute, effectively neutralizing this component 
of the score. 

Alternative Scoring Models
The scoring factors described above relate to Lucene"s default scoring model. It"s 
known as the Vector Space Model, also referred to as simply TF-IDF due to its  
most prominent components. 

In Lucene the relevance model is implemented by a Similarity subclass, and  
Solr provides a SimilarityFactory for each one. 

Query-time and index-time boosting
fuzzy query
a_name:Smashing~
For the fuzzy query case seen here,  
you could use dismax"s bq parameter and give it a non-fuzzy version of the user"s query. That will have the effect of boosting an exact match stronger.

Lucene"s DisjunctionMaxQuery
fieldA:rock^2 OR fieldB:rock^1.2 OR fieldC:rock^0.5

The difference between that boolean OR query and DisjunctionMaxQuery is only in the scoring. 
if the intention is to search for the same text across multiple fields, then it"s better to 
use the maximum sub-clause score rather than the sum. Dismax will take the max 
whereas boolean uses the sum. 

The dismax query parser has a tie parameter, which is between zero (the default) 
and one. By raising this value above zero, it serves as a tie-breaker to give an edge to 
a document that matched a term in multiple elds versus one.  At the highest value 
of 1, it scores very similar to that of a boolean query.

Boosting: Automatic phrase boosting
+(Billy Joel) "Billy Joel"

Configuring automatic phrase boosting
Automatic phrase boosting is not enabled by default. In order to use this feature, you 
must use the pf parameter, which is an abbreviation of "phrase fields". 
You should start with the same value and then make adjustments. 
Common reasons to vary pf from qf:
• To use different (typically lower) boost factors so that the impact of phrase 
boosting isn"t overpowering.

Start with the same value used as qf, but with boosts cut in half. Remove fields that are always one 
term, such as an identifier. Use common-grams or shingling, as described in Chapter 10, Scaling Solr, to 
increase performance.

Phrase slop conguration
"Billy Joel"~1
dismax adds two parameters to automatically set the slop: qs for any explicit phrase 
queries that the user entered and ps for the phrase boosting mentioned previously. 

Partial phrase boosting

Boosting: Boost queries
bq=r_type:Album^2 (*:* -r_type:Compilation)^2 r_official:Official^2

Boosting: Boost functions

boost=recip(map(rord(r_event_date_earliest),0,0,99000) ,1,95000,95000)

Monday, February 24, 2014

Notes on Linux Commands

du - estimate file space usage
-s, --summarize
-h, --human-readable
-a, --all write counts for all files, not just directories
du -d 0 -h /home/userA
du -ah --exclude="*.txt" /home/userA

df - report file system disk space usage
df -m
df -ah
Include Certain File System Type -t
Exclude Certain File System Type: -x
df -t ext3
df -x ext3

Thursday, February 20, 2014

Notes on Windows Batch Scripting

Find a jar file given the class name?
http://stackoverflow.com/questions/1500141/find-a-jar-file-given-the-class-name
for %i in (*.jar) do @"C:\Program Files\Java\jdk1.6.0_24\bin\jar" tvf %i | find "JspServlet.class"&& echo %i 


Get-ChildItem -recurse -Filter *.jar | Foreach { jar tvf $_.fullname | Select-String -pattern "JspServlet.class"}

Get parent folder
http://stackoverflow.com/questions/16623780/how-to-get-windows-batchs-parent-folder
for %%i in ("%~dp0..") do set "folder=%%~fi"

Get current folder name
http://superuser.com/questions/160702/get-current-folder-name-by-a-dos-command

for %%* in (.) do set CurrDirName=%%~n*


http://superuser.com/questions/160702/get-current-folder-name-by-a-dos-command
%0     - as the name how this batchfile was called
%~d0   - as the drive letter where this batchfile is located ('\\' in case of share)
%~p0   - as path (without the drive letter) where this batchfile is located
%~n0   - as filename (without suffix) of this batchfile
%~x0   - as filename's suffix (without filename) of this batchfile
%~a0   - as this batchfile's file attributes
%~t0   - as this batchfile's date+time
%~z0   - as this batchfile's filesize
%~dpnx - as this batchfile's fully qualified path+filename

Search classes in jar file
http://www.windows-commandline.com/search-classes-in-jar-file/
forfiles /S /M *.jar /C "cmd /c jar -tvf @file | findstr /C:"classname" && echo @path"
List the classes in a jar file
jar -tvf jarfile | findstr /C:".class"
Find all the jar files in a folder
dir /S /b *.jar

Echo date and time
echo %DATE% %TIME%
http://snipplr.com/view/21573/
set DATESTAMP=%DATE:~10,4%_%DATE:~4,2%_%DATE:~7,2%
set TIMESTAMP=%TIME:~0,2%_%TIME:~3,2%_%TIME:~6,2%

set DATEANDTIME=%DATESTAMP%_%TIMESTAMP%

*See what process is using a TCP port*
http://www.techrepublic.com/blog/the-enterprise-cloud/see-what-process-is-using-a-tcp-port-in-windows-server-2008/
netstat -a -n -o
netstat -ano | findstr 9999
tasklist  /svc /fi "PID eq 755784"
http://ss64.com/nt/tasklist.html
/fi FilterName [/fi FilterName2 [ ... ]]
/svc List information for each process without truncation.
TASKLIST /FI "imagename eq svchost.exe" /svc
TASKLIST /v /fi "STATUS eq running"

TASKLIST /v /fi "username eq SERVICE_ACCT05"

Wednesday, February 19, 2014

Notes on NetBeans


Netbeans Shortcuts
https://netbeans.org/project_downloads/usersguide/shortcuts-74.pdf
http://wiki.netbeans.org/Keyboard_Shortcuts
Coding in Java
Ctrl-O/Alt-Shift-O Go to type/file
Alt+F7, check 'Find All Subtypes'

Ctrl-E Delete current line
Ctrl+Alt+B
Inspect Members Ctrl+F12
Inspect Members At Caret Ctrl+Shift+F12
Inspect Hierarchy Alt+F12
Inspect Hierarchy At Caret Alt+Shift+F12

Alt-Insert Generate code
Ctrl-Shift-I Fix all class imports
Alt-Shift-I Fix selected class's import
Alt-Shift-F Format selection
Alt-Shift Left/Right/Up/Down   Shift lines left/right/up/down
Ctrl-Shift-R Rectangular Selection (Toggle)
Ctrl-Shift-Up/D Copy lines up/down 
Ctrl/Alt-F12 Inspect members/hierarchy
Ctrl-/ Add/remove comment lines 
Ctrl-E Delete current line
ALT+F7 Find Usages
CTRL+F4 Close Editor Window and CTRL+SHIFT+F4 Close All Editor Windows
CTRL+F12 Navigate to Member
SHIFT+ESC Toggle Editor Maximize / Minimize

Remote debug in Netbeans
http://wiki.netbeans.org/FaqDebuggingRemote
From the main menu, select Debug > Attach Debugger
Make sure that JPDA Debugger is selected in the combo-box.
Select the appropriate connection type (with respect to the value of the " transport " sub-option).
Enter values of other options:
If you use " dt_socket " transport:
Enter the hostname of computer where the debugged application is running (can be "localhost" if it is on the same computer) into the Host field.
Enter the selected port number into the Port field.
If you use " dt_shmem " tranport (only for MS Windows):
Enter the shared memory name (e.g. "myapp").

Netbeans: Run Junit test and Run focused test method
https://blogs.oracle.com/netbeansphp/entry/run_debug_focused_test_method
http://oopbook.com/junit-testing/junit-testing-in-netbeans/



Monday, February 17, 2014

Google Map Tips

Google Maps Search Tips
http://www.geomidpoint.com/meet/searching.html
Basic search
The Google Maps search query is made up of two parts. The first part is the category of the point of interest or the business name you are searching for. The second part is the geographic location you want to search in. You separate these two parts by either one of two keywords, 'in' or 'near', both keywords work with the same effect.

Advanced search
The vertical bar symbol '|' allows you to group two or more categories together in a single search.
Use the minus sign '-' to exclude a category or particular business from the search results.
restaurants -pizza in Boston, MA


1. Create a Multi-Stop Route
2. Check Out Upcoming Events
Google Map Search
http://gizmodo.com/10-tricks-to-make-yourself-a-google-maps-master-1228938727
Search local
Once you've got an area or address on screen, you can run further queries from the search box based on the section of the map that's currently visible. Zoom in to see fewer results; zoom out to see more.

See everything
Want to see everything in a particular area that Google Maps knows about? Enter an asterisk ("*") into the search box and hit Enter (or click the blue search button).

Find postal addresses
Clear the search box, click on any street and the address and ZIP code appear. 

https://support.google.com/gmm/answer/3263242?p=maps_tips_tricks&rd=1
View maps offline (Android)
Zoom in to an area on the map.
Search for "ok maps."
After a few moments, you'll see a message confirming that the on-screen map area has been cached or downloaded.

Save a place and find it later
https://support.google.com/gmm/answer/3273404
To save a place, just pull up the info sheet of the location and tap Save. To access your saved places, tap the person icon and scroll down. If you have any saved offers, you can also access them on here on your Maps Activity page.

Save your home and work addresses
https://support.google.com/gmm/answer/3273404
Save your home and work addresses to get directions faster when you’re on the go. Just tap the person icon in the upper right corner and enter your home and work addresses. You can later edit your home or work address in the Settings tab.

Zoom in and out in more ways than one
https://support.google.com/gmm/answer/3273126
In addition to pinching the screen to zoom, you can also double-tap on your map, hold, and then scroll down to zoom in, or scroll up to zoom out.

Drop a pin to see Street View and share the location
https://support.google.com/gmm/answer/3273356

Coding Interview Tips

How to do better without actually practicing
https://www.interviewcake.com/tips-and-tricks
Communication
Ownership/leadership

Communicate
Make it feel like you're on a team.
Use "we" instead of "I,"
Think out loud.
If you're stuck, just say what you're thinking. Say what might work. Say what you thought could work and why it doesn't work.

Say you don't know. 
Slow the eff down.

Get unstuck
the interviewer usually cares more about your ability to cleverly poke the problem from a few different angles

Draw pictures. Don't waste time trying to think in your head—think on the board. Draw a couple different test inputs. Draw how you would get the desired output by hand. Then think about translating your approach into code.

Solve a simpler version of the problem.
Write a naïve, inefficient solution and optimize it later. 
Think out loud more
Wait for a hint. Don't stare at your interviewer expectantly, but do take a brief second to "think"—your interviewer might have already decided to give you a hint and is just waiting to avoid interrupting.

Think about the bounds on space and runtime.

Get your thoughts down.
Call a helper function and keep moving.
Don't worry about syntax. 
Leave yourself plenty of room.
Start at the top of the board and leave a blank line between each line.

Save off-by-one checking for the end. Don't worry about whether your for loop should have "<" or "<=." Write a checkmark to remind yourself to check it at the end. Just get the general algorithm down.

Use descriptive variable names. 
Walk through your solution by hand, out loud, with an example input. 
Look for off-by-one errors. Should your for loop use a "<=" instead of a "<"?
Test edge cases. Bonus: mention unit tests!
Don't be boring.
If you're unsure, say something like, "Then I'd usually check the code against some edge cases—should we do that next?"

Practice
Actually write code with pen and paper.

Algorithm Websites

Thursday, February 13, 2014

Notes on VI

insensitive search
use the \c escape sequence:
/\ccopyright
add set ignorecase for case-insensitive searching in my vimrc, and I can use \C to do a case-sensitive search 
I find the smartcase very useful. If you search for something containing uppercase characters, it will do a case sensitive search; if you search for something purely lowercase, it will do a case insensitive search. You can use \c and \C to override this:
:set smartcase
:set ic
:set noic

Page up (back) is Control-B; page down (forward) is Control-F. Half-pages are Control-U (up) and Control-D (down).

Vi Cheat Sheet
http://www.keyxl.com/aaab462/105/VIM-Text-Editor-keyboard-shortcuts.htm

Notes on cURL

9 uses for cURL worth knowing
http://httpkit.com/resources/HTTP-from-the-Command-Line/
Set the Request Method -X POST
The curl default HTTP method, GET, can be set to any method you would like using the -X option. The usual suspects POST, PUT, DELETE, and even custom methods, can be specified.
curl -X PUT \
     -H 'Content-Type: application/json' \
     -d '{"firstName":"Kris", "lastName":"Jordan"}'
     echo.httpkit.com

Set Request Headers -h
curl -H "Authorization: OAuth 2c4419d1aabeec" http://echo.httpkit.com
curl -H "Accept: application/json" -H "Authorization: OAuth 2c3455d1aeffc" http://echo.httpkit.com
Send a Request Body -d
curl -X PUT -H 'Content-Type: application/json' -d '{"firstName":"Kris", "lastName":"Jordan"}' echo.httpkit.com
Use a File as a Request Body @example.json
Escaping JSON/XML at the command line can be a pain and sometimes the body payloads are large files. Luckily, cURL’s @readfile macro makes it easy to read in the contents of a file.
curl -X PUT -H 'Content-Type: application/json' -d @example.json echo.httpkit.com
POST HTML Form Data -d
-d "firstName=Kris" -d "lastName=Jordan" \
POST HTML Multipart / File Forms
As you know from writing HTML file upload form, these use a multipart/form-data Content-Type, with the enctype attribute in HTML. In cURL we can pair the -F option and the @readFile macro covered above.
-F "firstName=Kris" -F "publicKey=@idrsa.pub;type=text/plain"

Send Post to Solr Query Handler
curl -X POST -F "q=query" -F "start=0" -F "rows=10" http://localhost:11111/solr/select

View Response Headers
curl -i echo.httpkit.com 

http://www.thegeekstuff.com/2012/04/curl-examples/
Save the cURL Output to a file -O
curl -O http://www.gnu.org/software/gettext/manual/gettext.html
curl -O http://www.gnu.org/software/gettext/manual/html_node/index.html -O http://www.gnu.org/software/gettext/manual/gettext.html

Follow HTTP Location Headers with -L option
curl -L http://www.google.com

Continue/Resume a Previous Download -C
curl -C - -O http://www.gnu.org/software/gettext/manual/gettext.html

Limit the Rate of Data Transfer --limit-rate 1000B
curl --limit-rate 1000B -O http://www.gnu.org/software/gettext/manual/gettext.html
Download a file only if it is modified before/after the given time
curl -z 21-Dec-11 http://www.example.com/yy.html

Pass HTTP Authentication in cURL -u username:password
curl -u username:password URL

More Information using Verbose and Trace Option
curl -v http://google.co.in

Get Definition of a Word using DICT Protocol
curl dict://dict.org/d:bash


Download Files from FTP server
curl -u ftpuser:ftppass -O ftp://ftp_server/public_html/xss.php
List/Download using Ranges
curl   ftp://ftp.uk.debian.org/debian/pool/main/[a-z]/

Upload Files to FTP Server
curl -u ftpuser:ftppass -T myfile.txt ftp://ftp.testserver.com
curl -u ftpuser:ftppass -T "{file1,file2}" ftp://ftp.testserver.com
Optionally we can use “.” to get the input from STDIN and transfer to the remote.
$ curl -u ftpuser:ftppass -T - ftp://ftp.testserver.com/myfile_1.txt

Sunday, February 9, 2014

Learning JMX

http://docs.oracle.com/javase/7/docs/technotes/guides/management/agent.html
-Dcom.sun.management.jmxremote.port=55555
-Dcom.sun.management.jmxremote.authenticate=false

-Dcom.sun.management.jmxremote.ssl=false

Java MXBean custom types
http://stackoverflow.com/questions/14939335/java-mxbean-custom-types

Each resource that is to be managed must provide a management interface, which consists of the attributes and operations it exposes so that it can be monitored and controlled by a management application.

Standard MBeans
The management interface of the resource must have the same name as the resource's Java class, followed by "MBean"; it must be defined as a Java interface; and it must be implemented by the resource to be managed using the implements keyword.
The implementing class must contain at least one public constructor.
Getters and setters for attributes on the management interface must follow strict naming conventions.
The management interface is contained in its own .java file and must have the same name as its corresponding interface. Thus, every standard MBean requires at least two source code files: one for the interface and one for the class that implements the interface.

While an MBean may inherit the public (and protected) attributes and operations of its parent class, it will not necessarily inherit its management interface.
When this pattern is used, the introspection performed by the MBean server proceeds up the MBean interface inheritance graph, not the implementing inheritance graph.
Simple inheritance with MBean inheritance

Common Mistakes Not Caught by Introspection
MBean interface not given public visibility

there are three levels to the JMX architecture: instrumentation, agent, and distributed services. 
The ObjectName class

The ObjectName class is provided by the RI and is crucial to the MBean registration process. Every MBean must be represented by an ObjectName in the MBean server and no two MBeans may be represented by the same ObjectName. Each ObjectName contains a string made up of two components: the domain name and the key property list.
domain-name:key1=value1[,key2=value2,...,keyN=valueN]
DefaultDomain:name=Queue
Every compliant JMX implementation must provide a default domain name. For the JMX 1.0 RI, that name is DefaultDomain, but you can't depend on this to be the case all of the time.

There is only one restriction on domain names: you cannot use JMImplementation as the domain name for your MBeans. This domain name is reserved for the implementation (hence the name) and contains a single metadata MBean that provides information about the implementation, such as its name, version, and vendor.
String myObjName = ":Name=Worker,Role=Supplier";
ObjectName = new ObjectName(myObjName);
ObjectName = new ObjectName("UserDomain", "Name", "Controller");

The first step in using the MBean server is to obtain a reference to it. 
MBeanServer server = MBeanServerFactory.createMBeanServer(  );
MBeanServer server = MBeanServerFactory.createMBeanServer(  );
  ObjectName objName = new ObjectName("UserDomain:Name=Controller");
  Controller controller = new Controller(  );
  server.registerMBean(controller, objName);

server.createMBean("sample.standard.Controller", objName);
Dynamic MBeans
Standard MBeans are well suited for management interfaces that are relatively static. However, if a management interface must be defined for an existing resource, is likely to evolve over time, or for some other reason needs to be exposed at runtime, 

When the MBean server is asked to register a dynamic MBean, however, no introspection is performed.
Instead of using a Java interface with the name "MBean" on it, dynamic MBeans use metadata classes to expose their management interfaces. They make that metadata available through an interface called DynamicMBean, which must be implemented by all dynamic MBeans. 

Instead of using a Java interface with the name "MBean" on it, dynamic MBeans use metadata classes to expose their management interfaces. They make that metadata available through an interface called DynamicMBean, which must be implemented by all dynamic MBeans. 
  

















Open MBeans
By using open MBeans, we can instrument application resources of any type and make them available to any agent or management application that does not have access to the bytecode for either the resource, attribute, or operation parameter. The agent or management application can even be a non-Java program!

The open MBean types are at the heart of what makes open MBeans "open." The JMX RI defines a base class, OpenType, from which all open MBean types are derived. This ensures consistency among all of the subsequent types.

OpenType

This class is the base class for all open MBean types
Type name
The name that has been assigned to the data type represented by this OpenType instance. Must be unique across all other open MBean types.
Class name
The fully qualified name of the Java class that the OpenType instance represents (e.g., java.lang.Integer is the class name for integer data types).

SimpleType
This class is a subclass of OpenType (as are all valid open MBean types) that is used to represent the following open MBean types:
OpenType openType = SimpleType.LONG;

Other basic types
ArrayType
CompositeType
TabularType
public CompositeData createCompositeDataObject(  ) {
  try {
  CompositeType buildingType = new CompositeType(
    "BuildingCompositeType",
    "CompositeType that represents a Building.",
    itemNames,
    itemDescriptions,
    itemTypes
  );
  
    String[] itemNames = {
      "Name",
      "NumberOfFloors",
      "Height",
      "UndergroundParking",
      "NumberOfElevators",
      "OfficeSpace"
    };
    Object[] itemValues = {
      "Building A",
      new Short(3),
      new Integer(45),
      new Boolean(false),
      new Short(1),
      new Long(10000)
    };
    CompositeData buildingData = new CompositeDataSupport(
      buildingType, // See Example 5-1
      itemNames,
      itemValues
    );
    return buildingData;
  } catch (Exception e) {
    // . . .
  }
}

Saturday, February 8, 2014

Learning ConcurrentHashMap

http://javarevisited.blogspot.com/2013/02/concurrenthashmap-in-java-example-tutorial-working.html
ConcurrentHashMap putifAbsent
concurrency level allows ConcurrentHashMap to partition Map ConcurrentHashMap allows multiple readers to read concurrently without any blocking. This is achieved by partitioning Map into different parts based on concurrency level and locking only a portion of Map during updates. Default concurrency level is 16, and accordingly Map is divided into 16 part and each part is governed with different lock. This means, 16 thread can operate on Map simultaneously, until they are operating on different part of Map. This makes ConcurrentHashMap high performance despite keeping thread-safety intact.  Though, it comes with caveat. Since update operations like put(), remove(), putAll() or clear() is not synchronized, concurrent retrieval may not reflect most recent change on Map.

In case of putAll() or clear(), which operates on whole Map, concurrent read may reflect insertion and removal of only some entries. Another important point to remember is iteration over CHM, Iterator returned by keySet of ConcurrentHashMap are weekly consistent and they only reflect state of ConcurrentHashMap and certain point and may not reflect any recent change. Iterator of ConcurrentHashMap's keySet area also fail-safe and doesn’t throw ConcurrentModificationExceptoin..

Default concurrency level is 16 and can be changed, by providing a number which make sense and work for you while creating ConcurrentHashMap. Since concurrency level is used for internal sizing and indicate number of concurrent update without contention, so, if you just have few writers or thread to update Map keeping it low is much better. ConcurrentHashMap also uses ReentrantLock to internally lock its segments.

ConcurrentHashMap is best suited when you have multiple readers and few writers.
1. ConcurrentHashMap allows concurrent read and thread-safe update operation.
2. During update operation, ConcurrentHashMap only lock a portion of Map instead of whole Map
3. Concurrent update is achieved by internally dividing Map into small portion which is defined by concurrency level.
4. Choose concurrency level carefully as a significant higher number can be waste of time and space and lower number may introduce thread contention in case writers over number concurrency level.
5. All operations of ConcurrentHashMap are thread-safe.
6. Since ConcurrentHashMap implementation doesn't lock whole Map, there is chance of read overlapping with update operations like put() and remove(). In that case result returned by get() method will reflect most recently completed operation from there start.
7. Iterator returned by ConcurrentHashMap is weekly consistent, fail safe and never throw ConcurrentModificationException. In Java.
8. ConcurrentHashMap doesn't allow null as key or value.
9. You can use ConcurrentHashMap in place of Hashtable but with caution as CHM doesn't lock whole Map.
10. During putAll() and clear() operations, concurrent read may only reflect insertion or deletion of some entries.

http://blog.stevenlevithan.com/archives/greedy-lazy-performance
internally ConcurrentHashMap allows concurrent threads to read values without locking at all, except in a minority of cases, and for concurrent writers to add and update values while only acquiring locks over localised segments of the internal data structure. Read operations are therefore mostly synchronization free and have greatly improved performance, and if there are many concurrent writers then lock contention will be reduced improving performance even further.
The designers of ConcurrentHashMap originally conceived the class for occasional use in in high concurrency areas at the center of systems and the default construction options reflect this!!
the number of shards should equal the number of concurrent writer threads that you normally see. And, the default value for concurrencyLevel is 16!

Best practices for using ConcurrentHashMap
http://howtodoinjava.com/2013/05/27/best-practices-for-using-concurrenthashmap/
ConcurrentHashMap offers internally maintained concurrency. It means you do not need to have synchronized blocks when accessing ConcurrentHashMap in multithreaded application.

Fully parametrized constructor of ConcurrentHashMap takes 3 parameters, initialCapacity, loadFactor and concurrencyLevel.
1) initialCapacity
2) loadFactor
3) concurrencyLevel
concurrencyLevel denotes the number of shards. It is used to divide the ConcurrentHashMap internally into this number of partitions and equal number of threads are created to maintain thread safety maintained at shard level.
The default value of “concurrencyLevel” is 16. In most cases in normal application, a single shard is able to handle multiple threads with reasonable count of key-value pairs. And performance will be also optimal. 

Monday, February 3, 2014

Powershell Equivalents like AWK and Grep

Get-Content largefile.txt -totalcount 1

http://windows-powershell-scripts.blogspot.com/2009/06/awk-equivalent-in-windows-powershell.html
ps: Get-Content .\test.csv | %{ $_.Split(',')[1]; }
awk:cat test.csv | awk -F, '{print $2}'

cat test.csv | awk -F, '{total+=$3} END {print "Total: "total}'
Get-Content .\test.csv | %{ $_.Split(',')[1]; }

cat test.csv | awk -F, '{print "No of fields in record "$1" = "NF }
Get-Content .\test.csv | %{ $a=$_.Split(','); Write-Host "No of fields in record"$a[0]"="$a.length; }

cat test.csv | awk -F, '{if ($NF ~ "[a-c]") print}'
Get-Content .\test.csv | %{ if ($_.Split(',')[-1] -match "[a-c]") { $_; } }

cat test.csv | awk -F, '{total+=$3} END {print "Total: "total}'

Linux grep equivalent: select-string
select-string -pattern “somestring” foo.txt | foreach { $_.ToString().split(” “)[2] }


ForEach-Object
http://technet.microsoft.com/en-us/library/hh849731.aspx
Parameter Set: ScriptBlockSet
ForEach-Object [-Process] <ScriptBlock[]> [-Begin <ScriptBlock> ] [-End <ScriptBlock> ] [-InputObject <PSObject> ] [-RemainingScripts <ScriptBlock[]> ] [-Confirm] [-WhatIf] [ <CommonParameters>]

Parameter Set: PropertyAndMethodSet
ForEach-Object [-MemberName] <String> [-ArgumentList <Object[]> ] [-InputObject <PSObject> ] [-Confirm] [-WhatIf] [ <CommonParameters>]

Script block. You can use a script block to specify the operation. Within the script block, use the $_ variable to represent the current object. The script block is the value of the Process parameter. The script block can contain any Windows PowerShell script.
For example, the following command gets the value of the ProcessName property of each process on the computer.
Get-Process | ForEach-Object {$_.ProcessName}
30000, 56798, 12432 | ForEach-Object -Process {$_/1024}
Get-ChildItem $pshome | ForEach-Object -Process {if (!$_.PSIsContainer) {$_.Name; $_.Length / 1024; "" }}

PS C:\> $Events = Get-EventLog -LogName System -Newest 1000
PS C:\>$events | ForEach-Object -Begin {Get-Date} -Process {Out-File -Filepath Events.txt -Append -InputObject $_.Message} -End {Get-Date}

Because Windows PowerShell treats null as an explicit placeholder, the ForEach-Object cmdlet generates a value for $null, just as it does for other objects that you pipe to it.
1, 2, $null, 4 | ForEach-Object {"Hello"}

Get-Module -List | ForEach-Object -MemberName Path
"Microsoft.PowerShell.Core", "Microsoft.PowerShell.Host" | ForEach-Object {$_.Split(".")}
PS C:\>"Microsoft.PowerShell.Core", "Microsoft.PowerShell.Host" | ForEach-Object -MemberName Split -ArgumentList "."
PS C:\>"Microsoft.PowerShell.Core", "Microsoft.PowerShell.Host" | Foreach Split "."

http://ss64.com/ps/foreach-object.html
get-childitem C:\ | foreach-object -process { $_.length / 1024 }

ForEach (loop statement)
$trees = @("Alder","Ash","Birch","Cedar","Chestnut","Elm")

 foreach ($tree in $trees) {
   "$tree = " + $tree.length
 }
 foreach ($num in 1,2,3,4,5) {
  if ($num -eq 2) { continue } ; $num
 }

Loop through a collection of .txt files:

  foreach ($file in get-ChildItem *.txt) {
    Echo $file.name
}


cat test.txt | foreach-object {$null = $_ -match '<FancyPants>(?<x>.*)<.FancyPants>'; $matches.x}

http://www.hanselman.com/blog/UnixFightSedGrepAwkCutAndPullingGroupsOutOfAPowerShellRegularExpressionCapture.aspx
cat test.txt | foreach-object {$null = $_ -match '<FancyPants>(?<x>.*)<.FancyPants>'; $matches.x} | sort | get-unique
cat test.txt | % {$null = $_ -match '<FancyPants>(?<x>.*)<.FancyPants>';$matches.x} | sort | gu


http://technet.microsoft.com/en-us/library/hh849731.aspx
Get-Process | ForEach-Object {$_.ProcessName}
Split is just one of many useful methods of strings. To see all of the properties and methods of strings, pipe a string to the Get-Member cmdlet.


Get-Content .\1c* -TotalCount 10 | %{ if($_.StartsWith("INFO")) {a=$_.Split()[3]; $a.Substring(0,$a.Length-1); }
Get-Content .\1c* -TotalCount 10 | %{ if($_.StartsWith("INFO")) {$_.Split()[3];} else {$_} }
Get-Content .\1c* -TotalCount 10 | %{ if $_.Split()[3]; }

http://ss64.com/ps/if.html
if (condition) {commands_to_execute}
[ elseif (condition2) {commands_to_execute} ]
else {commands_to_execute}  

Part-4: Text Manipulation in PowerShell using .Replace(), and .Remove() methods.
http://newdelhipowershellusergroup.blogspot.com/2012/02/part-4-text-manipulation-in-powershell.html
$text.Replace("."," ")
The syntax for remove method is to define to keep the number of characters and remove everything else.
$text.Remove(4)
$text.Remove(4,7)


$string = $string.Substring(0,$string.Length-1)