Java Monday


hình ảnh
bikini
giá rẻ
smartphone
ngon
Goldmark City
Việt Nam
căn hộ
Hà Nội
chất lượng
dự án
sao
Hồ Ngọc Hà
Tin tức
Quảng cáo

điện thoại
beach
Phạm Băng Băng

Appengine Java Socket IO optimize

We are implement memcached (memcached server run in GCE) throughout Socket API
But many problem occur  .

1. We need to self implements memcached client because other java memcached client such as spymemcache , xmemcache .. using thread and other class that restriction by Google Appengine

Here is my implements http://pastebin.com/PfR9n69d
so we implement Jcache  (jsr107cache) to compatible to google api

2. We hit first error is :

exception invalid stream header: ACED0000. 

This error occur  when we try to read object from memcached server .
It mix text and binary data , header and footer is txt while body is binary .

one appengine instance handle many request at same time , so it we read and write to memcache at same time , so data is dirty , so we need to synchronized methods such as get , put and delete

3. But now at same time one instance can call only  get , put and delete . it locked by JVM
Website is too slow .
So at this time we need to create mutils Socket instance for one JVM .

we chose implement is by createCache
public Cache createCache(Map map) throws CacheException {
log.warning("cache instance count "+instacnes.size());
try {
if (instacnes.size()<20)
{
Memcached instacne = new Memcached(map);
instacnes.add(instacne);
return instacne;
}
else
{

int robin = counter++%20;
System.out.println(robin);
return instacnes.get(robin);
}

} catch (Exception e) {
log.warning("error while create cache instance "+e.getMessage());
throw new CacheException(e.getMessage());
}

}


4. Now , website is running fine . but when we turn on appstats .

as you see . a ton of remote_socket .Send and remote_socket.Receive

why ?


here is big problem because 100+ remote_socket .Send and remote_socket.Receive  will cost 1 to 2s

we use DataOutputStream  writeBytes and DataInputStream readLine .
Because they read byte by byte . we need to implement batch read and write .

Change to outputstream .write (byte[])
out.write("get <key>".getBytes());

change to byte buff [] = new byte[1048576];
int byteReaded  = recv.read(buff);

we use big buffer 1MB to store








How To Write Your Own Java / Scala Debugger

About Iris Shoor

With this post we’ll explore how Java / Scala debuggers are written and work. Native debuggers such WinDbg for Windows or gdb for Linux/Unix get their power from hooks provided to them directly by the OS to monitor and manipulate the state of an external process. The JVM, acting as an abstraction layer on top of the OS, provides its own independent architecture for debugging bytecode.blog_MyDebugger2

This framework and its APIs are completely open, documented and extensible, which means you can write you own debugger fairly easily. The framework’s current design is built out of two main parts – the JDWP protocol and the JVMTI API layer. Each has its own set of benefits and use-cases for which it works best.

The JDWP protocol

The Java Debugger Wire Protocol is used to pass requests and receive events (such as changes in thread states or exceptions) between the debugger and debuggee process using binary messages, usually over the network. The concept behind this architecture is to create as much separation as possible between the two. This is meant to reduce the Heisenberg effect (Werner the physicist that is, not your friendly Meth cooking


Source : http://www.javacodegeeks.com/2013/09/how-to-write-your-own-java-scala-debugger.html

My Favorite Books for Advanced Java Developers

About Dustin Marx

The idea for my last blog post (my ten favorite online resources for advanced Java developers), was inspired by the Xiaoran Wang post Top 10 Websites for Advanced Level Java Developers. Wang also wrote a post called Top 10 Books For Advanced Level Java Developers. As with the post on best websites for advanced Java developers, it is easy to see why Wang listed the ten books he did list. In this post, I look at my top ten list which includes many of the same books as on his list, but my list has a few that are different.

There are numerous good (and some not so good) books aimed at beginning Java developers. However, it seems far more difficult to find good Java books for intermediate and advanced developers. There are plenty of books that provide in-depth coverage on very narrow topics and so are suitable for advanced Java developers, but there seems to be few “more general” Java books aimed at advanced developers.

5. Java Generics and Collections

booksI think many Java developers would say that using Java collections is easy and that using generics can range from easy to difficult depending on what you’re doing. However, there is much in Java Generics and Collections ( O’Reilly, 2006) to appeal to the advanced developer in its coverage of both generics and collections. The authors of this book point out useful practices related to generics as well as outlining which collections to use in different situations. Even some experienced Java developers may not always consider which collections to use in a particular situation as carefully as they should and this book provides insight into advantages and strengths of each major Java standard collection as well as the drawbacks of each. The book delves deeply into the quagmire of generics and outlines important considerations such as the get and put principle.

4. Java Performance

0137142528Charlie Hunt’s and Binu John’s Java Performance (Pearson Education, 2011) provides in-depth coverage regarding tuning of Java applications. The book outlines the many facets of performance tuning as it summarizes the command-line options that are available and how they can be used to measure and tweak settings so that applications will perform better. This is a complex topic that is covered thoroughly and with focus on recent versions of Java.

3. The Well-Grounded Java Developer

evans_cover150The Well-Grounded Java Developer ( Manning, 2012) is a book that is definitely targeted at intermediate and advanced Java developers. As I discussed in my review of The Well-Grounded Java Developer, it helps experienced Java developers come up to speed on some of the latest Java and JVM trends (Java 7, dependency injection, Scala, Groovy, Clojure) while also covering some topics in depth that rarely receive that type of treatment (class loading, performance tuning, concurrency). There are books that specialize in each of these topics, but this is one book that can quickly provide a foundation in all of these advanced topics (and more) in a single book.

 
 

2. Java Concurrency in Practice

books1Like generics, concurrency is another skill that even many advanced Java developers can afford to enhance. Java Concurrency in Practice ( Pearson Education, 2006, by Brian Goetz and a host of other Java concurrency luminaries) is the de facto standard among Java books for coverage of writing concurrent applications in Java.

 
 
 
 
 

1. Effective Java

Both editions (First Edition and Second Edition) of Effective Java (Joshua Bloch, Pearson Education, Second Edition, 2008) have been outstanding. Christian Beutenmüller made a good point on the DZone syndicated version of Ryan Wang’s Top 10 Books for Advanced-level Java Developers: “I would remove Effective Java (since this is one of the first books I recommend to beginners).” Like Beutenmüller, I too recommend Effective Java to new Java developers, but I find myself referring even intermediate and advanced Java developers to Effective Java and refer to it regularly. 0321356683There are portions of Effective Java that are easily digestible even by those relatively new to Java and then there are portions of that book that I’ve realized that I don’t really appreciate until I have gained knowledge and experience. In many cases, I need realistic experience doing something the wrong way to understand some of the benefits and nuances of the practices outlined in this book. In short, Effective Java is one of the few books I can think of that is particularly appropriate to beginning Java developers, particularly appropriate to intermediate Java developers, and particularly appropriate to advanced Java developers.

Honorable Mention

There are other books that could have made this list and most of us probably have different takes on what are advanced Java books. For me, an “advanced Java developer” is a Java developer with significant depth of knowledge, significant breadth of knowledge, awareness of new and upcoming features of Java, and awareness of tools and products in the Java community that aid the entire Java development lifecycle. Effective Unit Testing and Java Power Tools are two books that are not on advanced subjects, but are books that I think contain information that can help a Java developer move from being a beginner to an intermediate or advanced Java developer. In particular, Effective Unit Testing can help Java developers write better and more efficient unit tests and Java Power Tools helps Java developers increase their breadth of knowledge key open source tools that can be used in all phases of Java development. Beginning Java developers tend not to have the unit testing experience that is contained in Effective Unit Testing and generally lack knowledge of the products available to the Java developer as outlined in Java Power Tools.

Conclusion

It is my belief that it is difficult to write and publish an advanced Java book. Writing an advanced Java books requires the author(s) to have deep understanding of the topic being written about and it is likely that publishers generally sell far more introductory books than advanced books. The barrier to entry seems much higher for writing and publishing advanced Java books as compared to writing and publishing entry-level Java books. Online resources in many ways seem better suited for satisfying advanced Java developers, but the five books I listed in this post buck that trend and provide a level of detailed and thorough information unmatched in the online resources in terms of accessibility and cohesiveness. The books in this list are useful to advanced Java developers, but are probably most useful in helping Java developers to become advanced Java developers.

Related Whitepaper:

Bulletproof Java Code: A Practical Strategy for Developing Functional, Reliable, and Secure Java Code

Use Java? If you do, you know that Java software can be used to drive application logic of Web services or Web applications. Perhaps you use it for desktop applications? Or, embedded devices? Whatever your use of Java code, functional errors are the enemy!

To combat this enemy, your team might already perform functional testing. Even so, you're taking significant risks if you have not yet implemented a comprehensive team-wide quality management strategy. Such a strategy alleviates reliability, security, and performance problems to ensure that your code is free of functionality errors.Read this article to learn about this simple four-step strategy that is proven to make Java code more reliable, more secure, and easier to maintain.

Get it Now!  


Source : http://www.javacodegeeks.com/2013/09/my-favorite-books-for-advanced-java-developers.html

How to kill Java with a Regular Expression

We recently stumbled upon a phenomen we absolutely weren’t aware of: You can kill any Java IDE and also any Java process with a simple regular expression…

Back in university, I was taught that regular expressions, which are called regular grammers or type 3 grammers  always end up in an finite state automaton and can therefore be processed in linear time (input length doubles, processing time doubles). However, that’s only true for “sane” expressions. A regular expression can also result in an non-deterministic finite state automaton and things can get messed up quite bad.

Consider the expression: (0*)*A  This will any number of zeros, followed by an upper case A. Now if you use Matcher.find() for this expression, everything is fine as long as there is a match in the input. However, if you call this, with ” 00000000000000000000″ as input, your program will hang (and so will the regex console in Eclipse or IntelliJ and every (Java-based) online regex service).

What at first glance looks like an infinite loop, truns out to be catastrophic backtracking. What this basically means is, that the matcher detects, that no A was found at the end of the input. Now the outer quantifier goes on step back – the inner one forward and again – no result. Therefore the matcher goes back step by step retrying all combinations to find a match. It will eventually return (without a match) but the complexity (and therefore the runtime) of this is expotential (adding one character to the input doubles the runtime). A detailed description can be found here: catastrophic backtracking

Here are some runtimes I measured (which almost exactly double for each character added):

0000000000: 0.1ms00000000000: 0.2ms000000000000: 0.7ms0000000000000: 1.3ms00000000000000: 1.7ms000000000000000: 3.5ms0000000000000000: 7.2ms00000000000000000: 13.9ms000000000000000000: 27.5ms0000000000000000000: 55.5ms00000000000000000000: 113.0ms000000000000000000000: 226.4ms0000000000000000000000: 439.1ms00000000000000000000000: 886.0ms

As a little side-note: For micro benchmarks like this, you always need to “warm” up the JVM as the HotSpot JIT will jump in at some point and optimize the code. Therefore the first run looks like this:

0000000000: 6.8ms00000000000: 11.8ms000000000000: 25.5ms0000000000000: 39.5ms00000000000000: 6.3ms   <- JIT jumped in and started to translate000000000000000: 5.4ms     to native code.0000000000000000: 7.1ms00000000000000000: 14.2ms000000000000000000: 26.8ms0000000000000000000: 54.4ms00000000000000000000: 109.6ms000000000000000000000: 222.1ms0000000000000000000000: 439.2ms00000000000000000000000: 885.6ms

So what’s the take-away here? If you’re running a server application or anything critical used by many users, don’t let them enter regular expressions unless you really trust them. There are regex implementations out there, which detect this problem and abort, but Java (up to JDK 8) doesn’t.

Note: You can test this with your local IDE or a small Java program to your hearts content – but please don’t start to knock out all the regex tester websites out there. Those guys provide a nice tool free of charge, so it would be quite unfair..

Here is the tiny benchmark I used:

public class Test {    public static void main(String[] args) {        for (int runs = 0; runs < 2; runs++) {            Pattern pattern = Pattern.compile("(0*)*A");            // Run from 5 to 25 characters            for (int length = 5; length < 25; length++) {                // Build input of specified length                String input = "";                for (int i = 0; i < length; i++) { input += "0"; }                               // Measure the average duration of two calls...                 long start = System.nanoTime();                for (int i = 0; i < 2; i++) {                    pattern.matcher(input).find();                }                System.out.println(input + ": "                        + ((System.nanoTime() - start) / 2000000d)                        + "ms");            }        }    }} 
Related Whitepaper:

Bulletproof Java Code: A Practical Strategy for Developing Functional, Reliable, and Secure Java Code

Use Java? If you do, you know that Java software can be used to drive application logic of Web services or Web applications. Perhaps you use it for desktop applications? Or, embedded devices? Whatever your use of Java code, functional errors are the enemy!

To combat this enemy, your team might already perform functional testing. Even so, you're taking significant risks if you have not yet implemented a comprehensive team-wide quality management strategy. Such a strategy alleviates reliability, security, and performance problems to ensure that your code is free of functionality errors.Read this article to learn about this simple four-step strategy that is proven to make Java code more reliable, more secure, and easier to maintain.

Get it Now!  

Leave a Reply


+ 1 = seven




Source : http://www.javacodegeeks.com/2013/09/how-to-kill-java-with-a-regular-expression.html

How to Speed Up Apache Xalan’s XPath Processor by Factor 10x

About Lukas Eder

There has been a bit of an awkward bug in Apache Xalan for a while now, and that bug is XALANJ-2540. The effect of this bug is that an internal SPI configuration file is loaded by Xalan thousands of times per XPath expression evaluation, which can be measured easily as such:
 
 
 
 
 
 
 
this:

Element e = (Element)  document.getElementsByTagName("SomeElementName")          .item(0);String result = ((Element) e).getTextContent();

Seems to be an incredible 100x faster than this:

// Accounts for 30%, can be cachedXPathFactory factory = XPathFactory.newInstance();// NegligibleXPath xpath = factory.newXPath();// NegligibleXPathExpression expression =  xpath.compile("//SomeElementName");// Accounts for 70%String result = (String) expression  .evaluate(document, XPathConstants.STRING);

It can be seen that every one of the 10k test XPath evaluations led to the classloader trying to lookup the DTMManager instance in some sort of default configuration. This configuration is not loaded into memory but accessed every time. Furthermore, this access seems to be protected by a lock on the ObjectFactory.class itself. When the access fails (by default), then the configuration is loaded from the xalan.jar file’s configuration file:

META-INF/service/org.apache.xml.dtm.DTMManager

Every time!:

A profiling session on Xalan

A profiling session on Xalan

Fortunately, this behaviour can be overridden by specifying a JVM parameter like this:

-Dorg.apache.xml.dtm.DTMManager=  org.apache.xml.dtm.ref.DTMManagerDefault

or

-Dcom.sun.org.apache.xml.internal.dtm.DTMManager=  com.sun.org.apache.xml.internal.dtm.ref.DTMManagerDefault

The above works, as this will allow to bypass the expensive work in lookUpFactoryClassName() if the factory class name is the default anyway:

// Code from c.s.o.a.xml.internal.dtm.ObjectFactorystatic String lookUpFactoryClassName(       String factoryId,       String propertiesFilename,       String fallbackClassName) {  SecuritySupport ss = SecuritySupport    .getInstance();  try {    String systemProp = ss      .getSystemProperty(factoryId);    if (systemProp != null) {       // Return early from the method      return systemProp;    }  } catch (SecurityException se) {  }  // [...] "Heavy" operations later

Resources

The above text is an extract from a Stack Overflow question and answer that I’ve contributed to the public a while ago. I’m posting it again, here on my blog, such that the community’s awareness for this rather heavy bug can be raised. Feel free to upvote on this ticket here, as every Sun/Oracle JDK on this planet is affected: https://issues.apache.org/jira/browse/XALANJ-2540

Contributing a fix to Apache would be even better, of course…
 

Related Whitepaper:

Bulletproof Java Code: A Practical Strategy for Developing Functional, Reliable, and Secure Java Code

Use Java? If you do, you know that Java software can be used to drive application logic of Web services or Web applications. Perhaps you use it for desktop applications? Or, embedded devices? Whatever your use of Java code, functional errors are the enemy!

To combat this enemy, your team might already perform functional testing. Even so, you're taking significant risks if you have not yet implemented a comprehensive team-wide quality management strategy. Such a strategy alleviates reliability, security, and performance problems to ensure that your code is free of functionality errors.Read this article to learn about this simple four-step strategy that is proven to make Java code more reliable, more secure, and easier to maintain.

Get it Now!  

Leave a Reply


eight − = 6




Source : http://www.javacodegeeks.com/2013/09/how-to-speed-up-apache-xalans-xpath-processor-by-factor-10x.html

Six Java features to stay away from

I have spent countless hours troubleshooting different applications. Via the experience I can draw a conclusion about several Java SE features/APIs which most of the developers should just stay away from. When I refer to most of the developers, I have the regular Java EE developers in mind, not to the library designers / infrastructure engineers.

Full disclosure: I do honestly think that in the long run, most of the teams are better off staying away from the following features. But as always, there are exceptions. If you have a strong team and are fully aware of what you are doing, go ahead. In most cases though, if you start including following tools to your arsenal, you will regret it in the long run:

  • Reflection
  • Bytecode manipulation
  • ThreadLocals
  • Classloaders
  • Weak/Soft references

Source : http://www.javacodegeeks.com/2013/09/six-java-features-to-stay-away-from.html

Simplicity vs. Robustness – Demonstrated On Lock File Handling

About Jakub Holy

Today we will discuss a conflict between the design values of keeping things simple, stupid (KISS) and robustness, between underdesign and overdesign.

We were writing a batch Java application and needed to ensure that at maximum one instance is running at a time on the server. A team member had the good idea of using lock files, which indeed worked and helped us a lot. However the original implementation wasn’t very robust, which has cost us valuable people time and expensive context switches due to troubleshooting the damn application rejecting to run and locating the lock file.

As Øyvind Bakksjø of Comoyo has recently explained, a software engineer is distinguished from a mere coder by thinking and caring not only the happy path through the code but also about the unhappy cases. Good engineers think about possible problems and try to handle them gracefuly so that code that depends on them and their users have easier time dealing with problematic situation. Robustness includes catching errors early, handling them in a good way, and providing useful and helpful error messages. On the other hand, simplicity [TBD: Hickey] is a crucial characteristic of systems. It is always too easy to spend too much time on making code bullet-proof instead of focusing the effort somewhere where it would be more valuable to the business.


Source : http://www.javacodegeeks.com/2013/09/simplicity-vs-robustness-demonstrated-on-lock-file-handling.html

Java Reflection Tutorial

About Ryan Wang

In this tutorial, I mainly write some examples to introduce what Java reflection can do. Hopefully, it can give you an overview of this concept. Please leave your comment for suggestions.

What is Reflection?

In brief, reflection is the ability of a program to examine and modify the structure and behavior of an object at runtime.

This concept is sometimes mixed with introspection. Introspection (Type introspection) is the ability of a program to examine the type or properties of an object at runtime. Therefore, it is a subset of reflection. Some languages support introspection, but do not support reflection, e.g., C++.

Why do we need reflection?

Reflection enables us to do the following:


Source : http://www.javacodegeeks.com/2013/09/java-reflection-tutorial.html

Simplicity vs. Robustness – Demonstrated On Lock File Handling

About Jakub Holy

Today we will discuss a conflict between the design values of keeping things simple, stupid (KISS) and robustness, between underdesign and overdesign.

We were writing a batch Java application and needed to ensure that at maximum one instance is running at a time on the server. A team member had the good idea of using lock files, which indeed worked and helped us a lot. However the original implementation wasn’t very robust, which has cost us valuable people time and expensive context switches due to troubleshooting the damn application rejecting to run and locating the lock file.

As Øyvind Bakksjø of Comoyo has recently explained, a software engineer is distinguished from a mere coder by thinking and caring not only the happy path through the code but also about the unhappy cases. Good engineers think about possible problems and try to handle them gracefuly so that code that depends on them and their users have easier time dealing with problematic situation. Robustness includes catching errors early, handling them in a good way, and providing useful and helpful error messages. On the other hand, simplicity [TBD: Hickey] is a crucial characteristic of systems. It is always too easy to spend too much time on making code bullet-proof instead of focusing the effort somewhere where it would be more valuable to the business.


Source : http://www.javacodegeeks.com/2013/09/simplicity-vs-robustness-demonstrated-on-lock-file-handling.html

Back to Top