Feed aggregator

Android Asynctask Internal vis-a-vis Half Sync Half Async Pattern

Javalobby Syndicated Feed - Wed, 18-Mar-15 21:50
The way Asynctask has been implemented in Android, is an apt example of Half Sync - Half Async pattern described in Pattern Oriented Software Architecture or POSA2. First of all, let us try to understand what we mean by Half Sync-Half Async design pattern. Half Sync- Half Async pattern is a specific way how we want to structure our threads in a multithreaded application. As the name suggests, in...
Categories: Java

One Year After Java 8's Release, IDEs and Compilers are Not Fully Ready Yet

Javalobby Syndicated Feed - Wed, 18-Mar-15 09:35
One year ago, on March 18, 2014, Java SE 8 was released, and with it, the bliss of functional programming through lambda expressio Preview Text:  One year ago, on March 18, 2014, Java SE 8 was released, and with it, the bliss of functional programming through lambda expressions and the streams API. Legacy ...
Categories: Java

DZone's Data Migration Checklist

Javalobby Syndicated Feed - Wed, 18-Mar-15 01:00
Last week, DZone released its Guide to Database and Persistence Management, where you'll find the data, ideas, and solutions you need to optimize your data storage and management. Here's the Checklist from the guide, which will help you plan your move from one database system to another. Preview Text:  Data migration is the process of...
Categories: Java

Walking Recursive Data Structures Using Java 8 Streams

Javalobby Syndicated Feed - Tue, 17-Mar-15 23:30
The Streams API is a real gem in Java 8, and I keep finding more or less unexpected uses for them. I recently wrote about using them as ForkJoinPool facade. Here’s another interesting example: Walking recursive data structures.Without much ado, have a look at the code: Preview Text:  The Streams API is a real gem in Java 8, and I keep...
Categories: Java

The Best of DZone: Mar. 11 - Mar. 18

Javalobby Syndicated Feed - Tue, 17-Mar-15 23:30
THIS WEEK'S TOP LINKS Check out the most popular links shared on DZone from the past week. Be sure to share the best developer links from across the web at DZone.com. Preview Text:  If you missed anything on DZone this week, now's your chance to catch up! This week's best include 6 cures for Big Data...
Categories: Java

What are Angular HTTP interceptors anyway?

codecentric Blog - Tue, 17-Mar-15 23:00

Sometimes it takes more time or words to describe a concept in natural language, than to express it in code. Angular’s HTTP interceptors are such an example. The implementation is about half the size (about 30 lines of code at the time of writing) of the documentation (80 lines). Understanding the code behind frameworks’ features is often desirable as it improves understanding of language concepts (Promises in the case) as well as use cases for frameworks’ features.

In this article I am presenting the code that is driving Angular’s HTTP interceptor feature. You will learn how easy it is to look at Angular’s codebase, how versatile Promises are and we will look back at a common cross-cutting concern which is handled with HTTP interceptors.

Interceptors, a short reminder

Angular’s HTTP interceptors can be used to pre- and postprocess HTTP requests. Preprocessing happens before requests are executed. This can be used to change request configurations. Postprocessing happens once responses have been received. Responses can be transformed via postprocessing. Global error handling, authentication, loading animations and many more cross-cutting concerns can be implemented with HTTP interceptors. The following listing shows how a request (preprocessing) and response (postprocessing) interceptor can be used.

const module = angular.module('interceptorTest', []);
 
module.config($httpProvider => {
  $httpProvider.interceptors.push(
    createInterceptor.bind(null, 'A'),
    createInterceptor.bind(null, 'B')
  );
});
 
module.run($http => {
  $http.get('https://api.github.com')
  .then(response =>; console.log('Response handler'));
});
 
angular.bootstrap(document.documentElement, [module.name]);
 
function createInterceptor(id) {
  return {
    request(config) {
      console.log(`Interceptor ${id}: Request`);
      return config;
    },
 
    response(response) {
      console.log(`Interceptor ${id}: Response`);
      return response;
    }
  };
}
 
// Generates the following output:
// Interceptor A: Request
// Interceptor B: Request
// Interceptor B: Response
// Interceptor A: Response
// Response handler

Interceptors are registered with the $httpProvider by adding them to the $httpProvider.interceptors array. Interceptors execute in the order in which they appear in the aforementioned array, during the request phase. For the response phase, they are executed in reverse order. The following image illustrates the process and should help understand the reverse order part.

Request interception process

Looking at the source

Angular makes it easy to inspect the framework’s sources. The framework’s documentation is generated from the sources and every API documentation page contains a reference to the sources. A click on the View Source button in the top right corner sends you right to the source code on GitHub.

The View Source button is located in the top right corner

As mentioned in the introduction, the interceptor source code is fairly short. This is due to the fact that the interceptors are based on Promises. Promises are responsible for the complicated mechanism of config (preprocess) and response (postprocess) transformation orchestration. An example for such a transformation is a rejection handler which falls back to cached values when requests fail. For reference, this is the source code at the time of writing.

// Source: https://github.com/angular/angular.js/blob/3fd48742b0fecbc470c44b465ba90786bda87451/src/ng/http.js#L794-L812
var chain = [serverRequest, undefined];
var promise = $q.when(config);
 
// apply interceptors
forEach(reversedInterceptors, function(interceptor) {
  if (interceptor.request || interceptor.requestError) {
    chain.unshift(interceptor.request, interceptor.requestError);
  }
  if (interceptor.response || interceptor.responseError) {
    chain.push(interceptor.response, interceptor.responseError);
  }
});
 
while (chain.length) {
  var thenFn = chain.shift();
  var rejectFn = chain.shift();
 
  promise = promise.then(thenFn, rejectFn);
}

As you can see, request interceptors are chained together in the form of Promise transformations via then(onFulfilled, onRejected). This has interesting implications.

  1. Response error interceptors can be used to resolve errors before the caller gets a chance to inspect the request’s result. This means that fallbacks can be implemented in a transparent way.
  2. Because there are request and response interceptors that are capable of transforming config and response objects, the $http request config can be extended with domain specific attributes that are translated to standard HTTP headers.
  3. Generic error handling is hard to get right. While every error could be caught, logged and the user informed, this is often not sufficient from a usability point of view. Some HTTP errors are expected and handled by userland code in Angular services, while others are unexpected and not handled. Angular’s interceptors concept does not provide native means to differentiate between expected and unexpected errors.

Conclusion

Angular’s HTTP interceptors are a fine example for the benefits of reading source code. It also shows the strength of Promises and how versatile they are. At the same time a closer look reveals that we need to put more energy into global error handling. A solution to take care of unhandled rejected Promises would need to exist to implement global error handling properly. The Q Promise library has something like this on the global Q object (API). Angular’s version of Q unfortunately does not contain this feature.

The post What are Angular HTTP interceptors anyway? appeared first on codecentric Blog.

Categories: Agile, Java, TDD & BDD

Turning on GC Logging at Runtime

Javalobby Syndicated Feed - Tue, 17-Mar-15 23:00
There is always the next JVM behaving badly. And you know by heart that if you just could have had those few startup options exposing some more information about what is going on, you might have stood a chance of actually fixing the goddamn thing. But nope, exactly the flag you need (be it -XX:+HeapDumpOnOutOfMemoryError or -XX:+PrintGCDetails) is always missing. Preview Text:  ...
Categories: Java

The Insightful, Funny Talk About How DevOps Can Fix Government IT

Javalobby Syndicated Feed - Tue, 17-Mar-15 23:00
This presentation is about how DevOps, and a lovely tool from Netflix OSS called Chaos Monkey, helped vastly improve US government agencies handling immigration and citizenship. Both insightful and funny, this talk comes highly recommended from Netflix architectural guru, Adrian Cockroft. Preview Text:  This presentation is about how DevOps, and...
Categories: Java

SpringOne2GX 2014 Replay: RaveJS - Spring Boot concepts for JavaScript applications

Javalobby Syndicated Feed - Tue, 17-Mar-15 23:00
Recorded at SpringOne2GX 2014.Speaker: John HannWeb / JavaScript TrackSlides: http://www.slideshare.net/SpringCentral/rave-js-springone-2gx-2014-41117770 Preview Text:  Modern JavaScript frameworks have become quite sophisticated. Unfortunately, they have also become quite complicated Legacy Sponsored:  ...
Categories: Java

Cultural Judo: How to Change Culture Without Changing Culture

Javalobby Syndicated Feed - Tue, 17-Mar-15 23:00
There are a bunch of us out there that are super passionate about this notion of agile as a cultural framework… or a value system… or a mindset. If you’ve been following this blog for any length of time, I suspect you’ll know where I stand on this. I think that culture, values, and mindset are important… but they aren’t why we adopt agile. We adopt agile to build better software,...
Categories: Java

Integrating jOOQ with Grails Featuring the UWS-jOOQ Plugin

Javalobby Syndicated Feed - Tue, 17-Mar-15 23:00
IntroductionGrails is a web framework aimed to boost development productivity. One of the main features is domain centric database schema generation. Applications built with Grails are able to update existing schema just before they start. To do this, Grails is using built-in domain mappers or migrations in more advanced cases. Preview Text:  ...
Categories: Java

How the Internet of Things Will Affect Database Management

Javalobby Syndicated Feed - Tue, 17-Mar-15 23:00
Preview Text:  There's no denying the rise of the Internet of Things will challenge existing database systems to adapt to accommodate huge volumes of unstructured data from diverse sources. Some analysts question whether RDBMSs have the scalability, flexibility, and connectivity required to collect, store, and categorize the disparate data...
Categories: Java

MongoDB World Conference - Scale the Universe!

Javalobby Syndicated Feed - Tue, 17-Mar-15 23:00
Want to join a couple thousand developers, administrators, and others to talk about MongoDB? Of course you do! Which is exactly why you should check out the MongoDB World Conference, "the largest global MongoDB conference to date!" The conference will be in New York, NY, June 1-2. Here's some more info from the MongoDB World webpage: Data is only worth what you make of it. MongoDB elevates the...
Categories: Java

How to Rank DevOps Opportunities in SaaS Startups

Javalobby Syndicated Feed - Tue, 17-Mar-15 23:00
Programmers now rely on a growing number of developer-oriented services to accelerate the development of software-as-a-service applications.  That’s because modern web and mobile applications are now so complex that code reuse is standard practice to keep organizations agile. Why reinvent the wheel when you can save 100 man years with a simple reuse of a few lines of Javascript or by...
Categories: Java

How the Oculus Rift Works to Create Virtual Reality

Javalobby Syndicated Feed - Tue, 17-Mar-15 22:30
This article is an excerpt from Oculus Rift in Action by Bradley Austin Davis, Karen Bryla and Alex Benton.Virtual reality is about constructing an experience that simulates a user’s physical presence in another environment. The Rift accomplishes this by acting both as a specialized input device and a specialized output device. Preview Text:  ...
Categories: Java

Delta Architectures: Unifying the Lambda Architecture and Leveraging Storm from Hadoop/REST

Javalobby Syndicated Feed - Tue, 17-Mar-15 22:30
Recently, I've been asked by a bunch of people to go into more detail on the Druid/Storm integration that I wrote for our book: Storm Blueprints for Distributed Real-time Computation.  Druid is great. Storm is great. Preview Text:  Recently, I've been asked by a bunch of people to go into more detail on the Druid/Storm integration...
Categories: Java

Linux, Debts and Out Of Memory Killer

Javalobby Syndicated Feed - Tue, 17-Mar-15 22:30
Imagine that you go to the bank, and ask for a $100,000 mortgage. The nice guy in the bank agrees to lend you the money,  and since you need to pay that in 5 installments, you take $15,000 to the contractor, and leave the rest in the bank until it is needed. The bank is doing brisk business, and promises a lot of customers that they can get their mortgage in the bank. Since most of the...
Categories: Java

Modeling Earthquake Dynamics

Javalobby Syndicated Feed - Tue, 17-Mar-15 22:30
Since 2012--with Marilou Durand, a student at UQAM--I have been working on the seismic gap hypothesis (see e.g. McCann et al. (1978) or Kagan & Jackson (1991)), or to be more specific, on the dynamics between an earthquake's magnitude (or seismic moment) and inter-occurence durations. Preview Text:  Since 2012 I have been working on...
Categories: Java

R: Conditionally Updating Rows of a Data Frame

Javalobby Syndicated Feed - Tue, 17-Mar-15 22:30
In a blog post I wrote a couple of days ago about cohort analysis I had to assign a monthNumber to each row in a data frame and started out with the following code: Preview Text:  The reason for the poor performance is that we process each row of the data table individually due to the call to group_by on the second line. One way we can refactor...
Categories: Java

Initialization in Modern C++ vs. Apple's Swift

Javalobby Syndicated Feed - Tue, 17-Mar-15 22:30
Initialization in Modern C++ vs Apple’s SwiftA little while ago I wrote a short post comparing some of the basic features of Modern C++ and Apple’s Swift. Preview Text:  Initialization is very important in any language and both C++ and Swift have built in constructs and checks to help make sure that objects get completely initialized...
Categories: Java

Thread Slivers eBook at Amazon

Syndicate content