Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you our best selling eBooks for FREE!

1. JPA Mini Book

2. JVM Troubleshooting Guide

3. JUnit Tutorial for Unit Testing

4. Java Annotations Tutorial

5. Java Interview Questions

and many more ....

Featured FREE Whitepapers

What's New Here?


Test Driven Traps, part 2

The Story of a Unit in Unit Tests In the previous part of this article, you could see some bad, though popular, test samples. But I’m not a professional critic (also known as a troll, or a hater), to grumble about without having anything constructive to say. Years of TDD have taught me more than just how bad the things can go. There are many simple but effective tricks, that can make you test-life much easier. Imagine this: you have a booking system for a small conference room in a small company. By some strange reason, it has to deal with off-line booking. People post their booking requests to some frontend, and once a week you get a text file with working hours of the company, and all the bookings (for what day, for how long, by whom, submitted at what point it time) in random order. Your system should produce a calendar for the room, according to some business rules (first come, first served, only in office business hours, that sort of things). As part of the analysis, we have a clearly defined input data, and expected outcomes, with examples. Beautiful case for TDD, really. Something that sadly never happens in the real life. Our sample test data looks like this: class TestData { static final String INPUT_FIRST_LINE = '0900 1730\n'; static final String FIRST_BOOKING = '2011-03-17 10:17:06 EMP001\n' + '2011-03-21 09:00 2\n'; static final String SECOND_BOOKING = '2011-03-16 12:34:56 EMP002\n' + '2011-03-21 09:00 2\n'; static final String THIRD_BOOKING = '2011-03-16 09:28:23 EMP003\n' + '2011-03-22 14:00 2\n'; static final String FOURTH_BOOKING = '2011-03-17 10:17:06 EMP004\n' + '2011-03-22 16:00 1\n'; static final String FIFTH_BOOKING = '2011-03-15 17:29:12 EMP005\n' + '2011-03-21 16:00 3';static final String INPUT_BOOKING_LINES = FIRST_BOOKING + SECOND_BOOKING + THIRD_BOOKING + FOURTH_BOOKING + FIFTH_BOOKING;static final String CORRECT_INPUT = INPUT_FIRST_LINE + INPUT_BOOKING_LINES;static final String CORRECT_OUTPUT = '2011-03-21\n' + '09:00 11:00 EMP002\n' + '2011-03-22\n' + '14:00 16:00 EMP003\n' + '16:00 17:00 EMP004\n' + ''; } So now we start with a positive test: BookingCalendarGenerator bookingCalendarGenerator = new BookingCalendarGenerator();@Test public void shouldPrepareBookingCalendar() { //when String calendar = bookingCalendarGenerator.generate(TestData.CORRECT_INPUT);//then assertEquals(TestData.CORRECT_OUTPUT, calendar); } It looks like we have designed a BookingCalendarGenerator with a “generate” method. Fair enough. Lets add some more tests. Tests for the business rules. We get something like this: @Test public void noPartOfMeetingMayFallOutsideOfficeHours() { //given String tooEarlyBooking = '2011-03-16 12:34:56 EMP002\n' + '2011-03-21 06:00 2\n';String tooLateBooking = '2011-03-16 12:34:56 EMP002\n' + '2011-03-21 20:00 2\n';//when String calendar = bookingCalendarGenerator.generate(TestData.INPUT_FIRST_LINE + tooEarlyBooking + tooLateBooking);//then assertTrue(calendar.isEmpty()); }@Test public void meetingsMayNotOverlap() { //given String firstMeeting = '2011-03-10 12:34:56 EMP002\n' + '2011-03-21 16:00 1\n';String secondMeeting = '2011-03-16 12:34:56 EMP002\n' + '2011-03-21 15:00 2\n';//when String calendar = bookingCalendarGenerator.generate(TestData.INPUT_FIRST_LINE + firstMeeting + secondMeeting);//then assertEquals('2011-03-21\n' + '16:00 17:00 EMP002\n', calendar); }@Test public void bookingsMustBeProcessedInSubmitOrder() { //given String firstMeeting = '2011-03-17 12:34:56 EMP002\n' + '2011-03-21 16:00 1\n';String secondMeeting = '2011-03-16 12:34:56 EMP002\n' + '2011-03-21 15:00 2\n';//when String calendar = bookingCalendarGenerator.generate(TestData.INPUT_FIRST_LINE + firstMeeting + secondMeeting);//then assertEquals('2011-03-21\n15:00 17:00 EMP002\n', calendar); }@Test public void orderingOfBookingSubmissionShouldNotAffectOutcome() { //given List<String> shuffledBookings = newArrayList(TestData.FIRST_BOOKING, TestData.SECOND_BOOKING, TestData.THIRD_BOOKING, TestData.FOURTH_BOOKING, TestData.FIFTH_BOOKING); shuffle(shuffledBookings); String inputBookingLines = Joiner.on('\n').join(shuffledBookings);//when String calendar = bookingCalendarGenerator.generate(TestData.INPUT_FIRST_LINE + inputBookingLines);//then assertEquals(TestData.CORRECT_OUTPUT, calendar); } That’s pretty much all. But what if we get some rubbish as the input. Or if we get an empty string? Let’s design for that: @Test(expected = IllegalArgumentException.class) public void rubbishInputDataShouldEndWithException() { //when String calendar = bookingCalendarGenerator.generate('rubbish');//then exception is thrown }@Test(expected = IllegalArgumentException.class) public void emptyInputDataShouldEndWithException() { //when String calendar = bookingCalendarGenerator.generate('');//then exception is thrown } IllegalArgumentException is fair enough. We don’t need to handle it in any more fancy way. We are done for now. Let’s finally write the class under the test: BookingCalendarGenerator. And so we do. And it comes out, that the whole thing is a little big for a single method. So we use the power of Extract Method pattern. We group code fragments into different methods. We group methods and data those operate on, into classes. We use the power of Object Oriented programming, we use Single Responsibility Principle, we use composition (or decomposition, to be precise) and we end up with a package like this:We have one public class, and several package-scope classes. Those package scope classes clearly belong to the public one. Here’s a class diagram for clarity:Those aren’t stupid data-objects. Those are full fledged classes. With behaviour, responsibility, encapsulation. And here’s a thing that may come to our Test Driven minds: we have no tests for those classes. We have only for the public class. That’s bad, right? Having no tests must be bad. Very bad. Right? Wrong. We do have tests. We fire up our code coverage tool and we see: 100% methods and classes. 95% lines. Not bad (I’ll get to that 5% of uncertainty in the next post).But we have only a single unit test class. Is that good? Well, let me put some emphasis, to point the answer out: It’s a UNIT test. It’s called a UNIT test for a reason! The unit does not have to be a single class. The unit does not have to be a single package. The unit is up to you to decide. It’s a general name, because your sanity, your common sense, should tell you where to stop. So we have six classes as a unit, what’s the big deal? How about if somebody wants to use one of those classes, apart from the rest. He would have no tests for it, right? Wrong. Those classes are package-scope, apart from the one that’s actually called in the test. This package-scope thing tells you: “Back off. Don’t touch me, I belong to this package. Don’t try to use me separately, I was design to be here!”. So yeah, if a programmer takes one of those out, or makes it public, he would probably know, that all the guarantees are voided. Write your own tests, man. How about if somebody wants to add some behaviour to one of those classes, I’ve been asked. How would he know he’s not breaking something? Well, he would start with a test, right? It’s TDD, right? If you have a change of requirements, you code this change as a test, and then, and only then, you start messing with the code. So you are safe and secure. I see people writing test-per-class blindly, without giving any thought to it, and it makes me cry. I do a lot of pair-programming lately, and you know what I’ve found? Java programmers in general do not use package-scope. Java programmers in general do not know, that protected means: for me, all my descendants, and EVERYONE in the same package. That’s right, protected is more than package-scope, not less a single bit. So if Java programmers do not know what a package-scope really is, and that’s, contrary to Groovy, is the default, how could they understand what a Unit is? How high can I get? Now here’s an interesting thought: if we can have a single test for a package, we could have a single test for a package tree. You know, something like this:We all know that packages in Java are not really tree-like, that the only thing those have with the directory structure is by a very old convention, and we know that the directory structure is there only to solve the collision-of-names problem, but nevertheless, we tend to use packages, like if the had some meaning. Like if we could hide one package inside another. Or build layers of lasagne with them. So is it O.K. to have a single test class for a tree of packages? Yes it is. But if so, where is the end to that? Can we go all the way up in the package tree, to the entry point of our application? Those… those would be integration tests, or functional tests, perhaps. Could we do that? Would that be good? The answer is: it would. In a perfect world, it would be just fine. In our shitty, hanging-on-the-edge-of-a-knife, world, it would be insane. Why? Because functional, end-to-end test are slow. So slow. So horribly slow, that it makes you wanna throw them away and go some place where you would not have to be always waiting for something. A place of total creativity, constant feedback, and lightning fast safety. And you’re back to unit testing. There are even some more reasons. One being, that it’s hard to test all flows of the application, testing it end-to-end. You should probably do that for all the major flows, but what about errors, bad connections, all those tricky logic parts that may throw up at one point or another. No, sometimes it would be just too hard, to set up the environment for integration test like that, so you end up testing it with unit tests anyway. The second reason is, that though functional tests do not pour concrete over your code, do not inhibit your creativity by repeating you algorithm in the test case, they also give no safety for refactoring. When you had a package with a single public class, it was quite obvious what someone can safely do, and what he cannot. When you have something enclosed in a library, or a plugin, it’s still obvious. But if you have thousands of public classes, and you are implementing a new feature, you are probably going to use some of them, and you would like to know that they are fine. So, no, in our world, it doesn’t make sense to go with functional tests only. Sorry. But it also doesn’t make sense to create a test per class. It’s called the UNIT test, for a reason. Use that. Happy coding and don’t forget to share! Reference: Test Driven Traps, part 2 from our JCG partner Jakub Nabrdalik at the Solid Craft blog....

Disrupt Tech Recruiting II – So You Want Ari Gold?

After publishing How to Disrupt Technical Recruiting – Hire an Agent and reading the subsequent feedback from readers at Hacker News and elsewhere, it is clear that at least some subset of engineers believe two things:The technical recruiting industry is at times remarkably flawed, and the financial incentives inherent to the system will not always lead a recruiter to represent the job seeker’s best interests. There is some demand for a talent/agent model for tech professionals, and it is a service several would be willing to pay for.And it is also worth noting that the only agent that engineers have know is Ari Gold (or at least that is the agent they want). Not even ONE Jerry Maguire reference? It seems engineers want to hug it out and care less about being shown the money. During my dissection of the industry, I somehow overlooked perhaps the biggest flaw that is at the absolute core of the issues with the recruiting industry. I’m a bit embarrassed that I missed it, and it wasn’t mentioned in the threads, so here goes: Secrecy and privacy of information is THE cornerstone to traditional recruiting. Why is that? Well think about it. If hiring companies knew every local candidate that possessed the skills they were seeking, many would not use an agency recruiter to handle the process and would simply have HR/managers contact them directly. Likewise, there would be many candidates that distrust recruiters applying directly to jobs if every available job were listed in one single place to search. There would certainly be some companies and candidates that recognize the value the recruiter brings beyond the initial introduction, but if all information were available to both sides we would find much less demand in the industry. Why do you think recruiters don’t generally list their clients’ names publicly? Why are there so many complaints about recruiters being unwilling to share a deep amount of detail about projects or their open jobs? There are two main reasons.To prevent other recruiters from learning about who they represent (NOTE: this is a big one, particularly for recruiters that work exclusively with start-ups that fly under the radar). To prevent candidates from applying directly to those companies, thus cutting out the chance for the recruiter’s fee.So in order to preserve their chances of being paid a fee, recruiters need to keep candidates and companies from knowing about each other until a precise moment where they make the introduction to both sides, and then hope that the company doesn’t respond with some evidence of prior contact and that the candidate doesn’t say that he/she applied to that job yesterday. If either of those two scenarios happen, the recruiter has some incentive for that deal to NOT work out, even though it is his/her client! Let’s assume that company has one vacancy, and my candidate applied to it a day before I discussed it with him/her. My incentive instantly goes from wanting my candidate to get the job (so I can get a fee) to a financial incentive that the candidate isn’t successful (so the job will still be available for another candidate I can find). That is a very dark and unfortunate set of incentives inherent to the system. It’s very easy to see how the incentives are built in this model. Recruiters have the incentive to help out their client companies to fill jobs, but only if the hire comes from my agency. The recruiter has the incentive to help a candidate find a job, but only if he/she takes a job at one of our clients. This is the unfortunate symptom of contingency work, and thankfully in my current business model (which isn’t contingency) I don’t have these same incentives to the same degree. Who are contingency recruiters really providing a service to anyway? Why do you think recruiters see LinkedIn as both a blessing and a curse? We use it to find new candidates and keep in touch with past candidates and clients, yet we realize that everyone else now has access to the same information and companies can much more easily find candidates. As a recruiter, you are probably likely to list your contacts as ‘private’ for this reason. The agent model would not have this privacy incentive built into the system. I could see an argument made where an agent might not want other agents to know who he/she is representing, but I think that is a much smaller problem than what we have today. When one of an agent’s talents is seeking work, there would be a campaign of open information to try and get the talent noticed. Having a business model based on privacy and secrecy is much less attractive than an agent model with openness. Enough about what’s broken, let’s talk about solutions. Based on the discussions, here are the services that the talent knows they want:Negotiation in job changes – Engineers seem to agree that an agent would probably be better at negotiating compensation packages. Another added benefit to having an agent in act as a proxy in negotiations is the preserved comfort factor after you potentially start the job. Picture a very trying or heated negotiation between an engineer and a start-up CEO that comes to an eventual agreement, and then the two must work in adjacent cubicles the following Monday. A buffer would have helped prevent any potential awkwardness. Job identification and coordination – Many in the community made it pretty clear that they are not interested or skilled in cold calling and introducing their services to potential clients. Having an agent do the legwork to identify potential employers and then to contact them and schedule meetings on the talent’s behalf will save lots of time to do other things. For consultants paid hourly, remember that every hour spent on job search is an hour you can’t bill to a client. Competitive salary/rate information – There seems to be a general distrust for websites that provide market information, and engineers may be somewhat insulated when it comes to what others with similar skills are earning. An agent would have some solid evidence regarding street value of any talent and would do the research necessary. Marketing/PR/Selling – It has become clear to me over the years that there is a sizable percentage of talented engineers that are uncomfortable talking up their own skills, and that was mentioned a couple times as another asset of an agent. Marketing and PR would probably mean different things for different career levels, but it could include work to increase the talent’s blog traffic, booking a presenter’s invite to a users group/meetup, or strategy on how to build the talent’s brand.There were some services and advantages that were somewhat surprisingly not mentioned:Handling incoming job solicitations – All of the complaints about recruiter spam and cold calls are gone for the agent’s talent. Forward calls, emails and LinkedIn spam to your agent, and he/she will investigate. Put your agent’s name on your website and LinkedIn profile. If the opportunity is legitimate and meets the criteria the talent sets for sharing, your will get the details. If not, you never have to waste your time hearing about it. Interview coaching – Not just your standard fare interview coaching you can find on web sites, but also any inside tips on the specific interview. This could include past experiences others have had with the company, some research on the background of the people you are meeting, and anything else that will be helpful to the talent. Remember that the agent is representing you at this point and not the company. As one reader noted, a good agent would not want to do companies a disservice by providing specific interview questions (as that would harm the agent model, the hiring company, and the agent himself/herself), but it is fair to offer the talent some guidance on what format to expect and who they will be meeting. Internal company information – As your representative, the agent should be providing talent with details on both their current employer and any that you may be considering for future employment. The information learned from discussions with others in the business will be helpful to the agent’s entire stable of talent. Consider it the type of info you may find on Glassdoor, but from verified sources. Career advice in non-search situations – Are you thinking about moving from code into management, or considering taking a leadership position on a project doomed for failure? Considering a jump into contracting, or thinking about abandoning your current independent consulting project to join a start-up? Want to talk about it? The value of this advice could be quite large yet difficult to quantify. Negotiation of promotions or raises – Changing jobs is not the only scenario where negotiation may take place. An agent could help you get the best deal from your current employer, either through direct negotiation or by coaching you on how to maximize your chances of getting a good number. Resume creation/curating – This one isn’t a huge service but it’s a few hours out of your work year. Transparency! – The agent model is completely transparent to everyone involved. The agent represents the talent, and the talent pays for that representation. There is no question about the agent’s loyalty as there is in contingency recruiting models (is the recruiter representing me or the company?).Some potential issues were identified in the commentsWould there be any conflict of interest when representing multiple candidates? Doubtful. I guess there could be a rare situation where two talents vie for the same position, and in that scenario the agent would simply represent both fairly and let the best person for the job win. The agent would probably have no financial incentive favoring either candidate. Transparency would be important (revealing the situation to both talents). Would there be backlash by hiring entities against candidates who use an agent? It’s hard to tell, but it’s conceivable that there would be some organizations that might react hesitantly at first. However, the key here is that an agent would be providing a service to a company for free (or if not completely free, definitely much less expensive than recruiters). It would be hard to think that firms would not be very pleased with a cold call from an agent stating that there is a qualified candidate interested in your company, and you will get all the normal recruiter services for the process with no recruiter fee at the back-end. How much would the agent need to know about technology? This one comes up quite a bit in criticisms of technical recruiters and then in the discussion of an agent. The agent needs to know enough about tech that he/she won’t misrepresent the talent, and will not waste time marketing talent to positions that are not a technical fit. An agent should also have some sense of tech trends, what skills are in demand, and the overall market for different types of talent. The biggest complaint seems to be wasting time with discussions of jobs that are not a fit or being sent on interviews that are not appropriate. An agent would not have the same incentive to send you everywhere imaginable, as that incentive is not built into the agent model. Would the service be just for contractors? No, not the way I see it. The pricing model for contractors is very easy to consider, as paying a percentage of hourly rate to the agent is already a common practice for staffing firms (what is called ‘margin’). The value of an agent’s service to engineers with salaried positions seems very obvious in helping to manage the overall career as well as the career moves.How do we compensate the agent? This is a bit tricky and I believe that the pricing model might be different for contractors and talent in direct salaried positions. For contractors, the existing model (a ‘per hour’ cut) is well-known and accepted. I think the main difference is an agent would have some fixed transparent rate ($x/hr or y%/hr. Many recruiting firms do not reveal the full bill rate to the contractor, which tends to cause distrust when the contractor finds out the actual rate. Having a fixed percentage or dollar per hour figure would make the relationship much more comfortable for both sides. For talent in permanent salaried positions, there needs to be more discussion. My vision would be some flat annual representation fee for service with an additional charge/bonus for times of active job search (where the level of services and time invested would increase). The job search fee could have several models built in – perhaps an hourly rate, flat fee per week/month, flat fee per job search, or some percentage of salary. The problem with the percentage of salary model is that it gives some incentive for the agent to suggest you take the highest paying job even if that is not best for your career. Another model would be a negotiation bonus above some level set and agreed upon by the agent and talent. “For anything I get you over 120K, I get a one-time bonus of 50% of the difference.” That leads to some incentive to take the highest paying job, but it also gives more motivation to negotiate. The reason I include an annual representation fee into the discussion is it helps to eliminate an agent’s incentive for candidates to change jobs, and it allows the agent to justify investing time with the talent to discuss career-related issues that come up. In the traditional recruiting model, the recruiter hopes everyone is looking for a job at all times. An agent would have much less of a financial incentive for you to change employers, so the agent wouldn’t be as likely to suggest you leave a job that is right for you. CONCLUSION Traditional contingency recruiting incentives are:Privacy and secrecy of client company and candidate information. Only help client company when it results in a fee. Only help candidates when they take jobs at my clients. A desire for people to leave jobs as often as possible.Agent model incentives:Keep the talent happy so they remain your talent. ???Let’s keep the dialogue going on this. Based on lots of direct feedback, it seems the community could be on to something. Agree, disagree, suggest? Reference: Disrupt Tech Recruiting II – So You Want Ari Gold? from our JCG partner Dave Fecak at the Job Tips For Geeks blog....

Pure Java JavaFX 2.0 Menus

In recent posts on JavaFX, I have focused on using JavaFX 2.0’s new Java APIs without use of the JavaFX 1.x’s JavaFXScript and without use of JavaFX 2.0’s new FXML. All of these examples have been compiled with the standard Java compiler and executed with the standard Java launcher. In this post, I continue the theme of using pure Java APIs supported by JavaFX 2.0 while demonstrating development of JavaFX 2.0 menus. I list the entire code listing for this example later in this post, but I first show snippets of the code to make it easier to focus on each piece. A good starting point for using JavaFX 2.0 menus is to instantiate an instance of MenuBar. This is straightforward as shown next. Instantiating a javafx.scene.control.MenuBarfinal MenuBar menuBar = new MenuBar(); A MenuBar can contain Menu instances as its children and each Menu instance can have instances of MenuItem as its children. The next code listing demonstrates instantiation of a Menu, adding of MenuItem instances (or an instance of SeparatorMenuItem) to that Menu instance, and then adding the Menu instance to the instance of MenuBar.Adding Newly Instantiated Menu and MenuItem Instances to MenuBar// Prepare left-most 'File' drop-down menu final Menu fileMenu = new Menu('File'); fileMenu.getItems().add(new MenuItem('New')); fileMenu.getItems().add(new MenuItem('Open')); fileMenu.getItems().add(new MenuItem('Save')); fileMenu.getItems().add(new MenuItem('Save As')); fileMenu.getItems().add(new SeparatorMenuItem()); fileMenu.getItems().add(new MenuItem('Exit')); menuBar.getMenus().add(fileMenu); The example above is too simplified for realistic uses. There are no event handlers or actions associated with clicking on any of the menu items and there are no ways to select the menu items via keystroke rather than via mouse clicking. The next code listing demonstrates instantiation of MenuItem instances that include more than just a text string. In this code listing, there is an example of using MenuItemBuilder to build a much more complex MenuItem that includes association to a key combination and includes an association to an action handler.More Sophisticated MenuItem Instantiation with Keystroke and Event Associations// Prepare 'Help' drop-down menu final Menu helpMenu = new Menu('Help'); final MenuItem searchMenuItem = new MenuItem('Search'); searchMenuItem.setDisable(true); helpMenu.getItems().add(searchMenuItem); final MenuItem onlineManualMenuItem = new MenuItem('Online Manual'); onlineManualMenuItem.setVisible(false); helpMenu.getItems().add(onlineManualMenuItem); helpMenu.getItems().add(new SeparatorMenuItem()); final MenuItem aboutMenuItem = MenuItemBuilder.create() .text('About') .onAction( new EventHandler<ActionEvent>() { @Override public void handle(ActionEvent e) { out.println('You clicked on About!'); } }) .accelerator( new KeyCodeCombination( KeyCode.A, KeyCombination.CONTROL_DOWN)) .build(); helpMenu.getItems().add(aboutMenuItem); menuBar.getMenus().add(helpMenu); Besides demonstrating MenuItemBuilder, associating a key combination (CTRL-A in this case) with a menu item, and associating an action with a menu item, this code example also demonstrates making a menu item disabled (grayed out) with setDisable(boolean) or making it not appear at all with setVisible(boolean). Although I could have specified disabling the menu item or making the menu item invisible with a MenuItemBuilder, I intentionally used ‘set’ methods on the MenuItems in this example to contrast that approach with using the MenuItemBuilder. For completeness, here is the entire code listing of my (The Complete Listing)package dustin.examples;import static java.lang.System.out;import javafx.application.Application; import; import javafx.event.ActionEvent; import javafx.event.EventHandler; import javafx.scene.Group; import javafx.scene.Scene; import javafx.scene.control.*; import javafx.scene.input.KeyCode; import javafx.scene.input.KeyCodeCombination; import javafx.scene.input.KeyCombination; import javafx.scene.paint.Color; import javafx.stage.Stage;/** * Example of creating menus in JavaFX. * * @author Dustin */ public class JavaFxMenus extends Application { /** * Build menu bar with included menus for this demonstration. * * @param menuWidthProperty Width to be bound to menu bar width. * @return Menu Bar with menus included. */ private MenuBar buildMenuBarWithMenus(final ReadOnlyDoubleProperty menuWidthProperty) { final MenuBar menuBar = new MenuBar();// Prepare left-most 'File' drop-down menu final Menu fileMenu = new Menu('File'); fileMenu.getItems().add(new MenuItem('New')); fileMenu.getItems().add(new MenuItem('Open')); fileMenu.getItems().add(new MenuItem('Save')); fileMenu.getItems().add(new MenuItem('Save As')); fileMenu.getItems().add(new SeparatorMenuItem()); fileMenu.getItems().add(new MenuItem('Exit')); menuBar.getMenus().add(fileMenu);// Prepare 'Examples' drop-down menu final Menu examplesMenu = new Menu('JavaFX 2.0 Examples'); examplesMenu.getItems().add(new MenuItem('Text Example')); examplesMenu.getItems().add(new MenuItem('Objects Example')); examplesMenu.getItems().add(new MenuItem('Animation Example')); menuBar.getMenus().add(examplesMenu);// Prepare 'Help' drop-down menu final Menu helpMenu = new Menu('Help'); final MenuItem searchMenuItem = new MenuItem('Search'); searchMenuItem.setDisable(true); helpMenu.getItems().add(searchMenuItem); final MenuItem onlineManualMenuItem = new MenuItem('Online Manual'); onlineManualMenuItem.setVisible(false); helpMenu.getItems().add(onlineManualMenuItem); helpMenu.getItems().add(new SeparatorMenuItem()); final MenuItem aboutMenuItem = MenuItemBuilder.create() .text('About') .onAction( new EventHandler<ActionEvent>() { @Override public void handle(ActionEvent e) { out.println('You clicked on About!'); } }) .accelerator( new KeyCodeCombination( KeyCode.A, KeyCombination.CONTROL_DOWN)) .build(); helpMenu.getItems().add(aboutMenuItem); menuBar.getMenus().add(helpMenu);// bind width of menu bar to width of associated stage menuBar.prefWidthProperty().bind(menuWidthProperty);return menuBar; }/** * Start of JavaFX application demonstrating menu support. * * @param stage Primary stage. */ @Override public void start(final Stage stage) { stage.setTitle('Creating Menus with JavaFX 2.0'); final Group rootGroup = new Group(); final Scene scene = new Scene(rootGroup, 800, 400, Color.WHEAT); final MenuBar menuBar = buildMenuBarWithMenus(stage.widthProperty()); rootGroup.getChildren().add(menuBar); stage.setScene(scene);; }/** * Main executable function for running examples. * * @param arguments Command-line arguments: none expected. */ public static void main(final String[] arguments) { Application.launch(arguments); } } The next series of screen snapshots attempt to demonstrate what this application looks like when executed using the java launcher. The images show the initial appearance of the application, the drop-down menu presented when ‘File’ menu is clicked on, the drop-down menu presented when the ‘Help’ menu is clicked on, and finally an image that shows the message written to standard output when the ‘About’ menu item is clicked on under the ‘Help’ menu. The code in the example featured in this post has numerous syntax features that should look familiar to Swing developers. In fact, many of the JavaFX classes used above have the same names as AWT classes and so care must be used to import the correct class when using the IDE’s automatic import suggestions. The example above also provides an example of JavaFX binding. In particular, the width of the menu bar is bound to the width of the stage’s width. This is useful because it looks better to have the menu bar span the entire top of the visual rather than being just wide enough to hold the menu labels. Building menus is fairly straightforward in JavaFX 2.0 and can be implemented using basic Java tools and the JavaFX 2.0 JAR. Happy coding and don’t forget to share! Reference: (Pure Java) JavaFX 2.0 Menus from our JCG partner Dustin Marx at the Inspired by Actual Events blog....

Android books giveaway for celebrating Packt’s 1000 title

Fellow geeks, We are pleased to announce that we have once again teamed up with Packt Publishing and we are organizing another giveaway for you! The occasion is the celebration of 1000 IT titles by Packt! So, 4 lucky winners will have the chance to win a copy of Packt’s best-selling books on Android (ebook format). Read along to find more details about the contest. Packt Publishing is about to publish its 1000th title. Packt books are renowned among developers for being uniquely practical and focused, covering highly specific tools and technologies.     For this occasion, Packt is giving everyone a surprise gift! However, the surprise won’t be revealed until September 28, 2012, which is when the 1000th title is expected to be unveiled. To receive this gift, simply register for an account on the Packt website and return between September 28 and 30.     Another thing to notice is that Packt supports many of the Open Source projects covered by its books through a project royalty donation, which has contributed over £300,000 to Open Source projects up to now. As part of the celebration Packt is allocating $30,000 to share between projects and authors, soon to be disclosed on their website. And of course Packt was kind enough to provide some cool Android books only for the readers of Java Code Geeks. Here are the books!     AndEngine for Android Game Development Cookbook: RAWOverview of the uber-cool AndEngine game engine for Android Game Development. Includes step by step detailed instructions and information on a number of AndEngine functions, including illustrations and diagrams for added support and resultsThis book is currently available as a RAW (Read As we Write) book. Android 3.0 Application Development CookbookQuickly develop applications that take advantage of the very latest mobile technologies, including web apps, sensors, and touch screens. Excellent for developing a full Android application. Appcelerator Titanium Smartphone App Development CookbookLeverage your JavaScript skills to write mobile applications using Titanium Studio tools with the native advantage! Android User Interface Development: Beginner’s GuideLeverage the Android platform’s flexibility and power to design impactful user-interfaces. How to Enter? Just send an email here using as subject “Packt Android Giveaway”. An empty email will do, it’s that simple! Optionally in your email, you may mention which book would you prefer the most! We are waiting for your emails! (Note: By entering the contest you will be automatically included in the forthcoming Java Code Geeks Newsletter.)     Again, don’t forget to sign-up for a free account in Packt before the 30th of September, 2012. Visit their website: A pleasant surprise gift is in order for each one of you! We would love it if you shared this! Spread the world people! ;-)...

Trying to understand CAP

The CAP theorem, stated by Brewer and proved by Gilbert and Lynch specifies a property of distributed systems. It states that such a system cannot guarantee at the same time Consistency, Availability and Partition tolerance. It is also often said as a catchy phrase: Consistency, Availability, Partition Tolerance – pick any two used mostly when talking about NoSQL databases and suggesting that a distributed system can be characterized as either CA, AP or CP (see e.g. here). For some time I’ve been trying to understand the different combinations and what do they mean in practice; having some time at various airports I caught up on reading, and here’s what I came up with. What’s C, A and P? First let’s define how I understand the three guarantees, basing on some of the articles I’ve read. Consistency is the easiest one. It roughly means that the clients get the same view of data. By saying that a system is consistent we often mean strong consistency, but it also can come in different flavors, e.g. casual. Availability is a property saying that every request to a non-failing node will return a (meaningful) response. The response may not contain all data (so the harvest will not be 100%, see the appropriate section in [3]), but it should be useful for the client. Partition tolerance means that the system will continue working even if any number of messages sent between nodes is lost. This can be e.g. a network failure between two datacenters, where nodes in each datacenter form a partition. Also note that a failure of any number of nodes forms a partition (it is not possible to distinguish between a network failure and a node failing and stopping to respond to messages). The hardest part for me is understanding the difference between Availability and Partition tolerance. Also, the various articles don’t specify what they mean by saying that a system is “working” after being e.g. partitioned – does it mean that every request gets a response with useful data, or are responses “Sorry, I can’t give you data right now” acceptable also? P+C? Let’s assume that a system is partition tolerant and that it has more than one node. If a partition is formed, splitting the system in two, the system should continue working. Hence both partitions allow clients to write. But then, how to guarantee consistency? If one client writes to partition 1, and another to partition 2? Hence: P => ~C. A+~P? Suppose now that a system is available and that we have more than one node. As the system is available, it should respond to requests even if some nodes die. As noted above, some nodes dying are equivalent to a partition. So if the system is still working, we have partition tolerance. Hence: A => P. A+C? Summarizing the two implications above (A => P and P => ~C), we get: A => ~C, so that an available system cannot be consistent (if it has more than one node). In practice however, there are of course AC systems, e.g. single-node RDBMS. Or even master-slave/master-master replicated RDBMS, provided there’s a central router knowing which nodes live and directing client appropriately. Such a router is then a single point of failure (SPoF). Relax? I suspect that in reality, when e.g. NoSQL/NewSQL systems are characterized with the CAP properties, they assume some relaxed form of C/A/P. Unfortunately, all of the definitions flying around seem to be pretty vague and are more of hand-waving than proper, precise statements. I think it would be much easier to explore the ever-growing ecosystem of new datastores if they could be more easily characterized; maybe the CAP vocabulary is just not enough? Please correct me if I’m wrong somewhere, and I probably am! :) And don’t forget to share! Reference: Trying to understand CAP from our JCG partner Adam Warski at the Blog of Adam Warski blog....

Motivation, Creativity and Innovation

I am a strong believer in that every individual on this earth can be creative and that hard focused work beats natural talent in every way. Personally I get really inspired by observing skillful people come up with amazing and creative ideas. Others may feel rivalry and/or envy. I am not sure why certain people react one way or another but dare I speculate that it insecurity have something to do about it? I feel confident to say that truly creative people does not feel intimidated or sentimental about their ideas being trumped by better ideas. In fact they encourage it. Because they know competition will inspire and push themselves further. There is nothing to fear if you are humble enough to accept and build on other peoples ideas to find ways to continuously elevate your intellect. Isaac Newton indicated that innovation is the continuous process of creating value from existing ideas, knowledge and inventions. So it is reasonable to think that most companies want to have motivated employers that are original, think differently and are highly creative. Right. And CEOs should not forget that this is just as important for them as well. So how do you engage people to be high performing, innovative and creative? I think Dan Pink is spot on.Autonomy: The desire to be self directed. Mastery: The urge to get better at stuff. Purpose: People are happiest when working for something larger than ourselves.Economical rewards are great. But they stop working as motivator for intellectual work when reaching a certain level – it is a lot better to empower people with freedom to express their creativity. I have observed that management sometimes carry out this form of hypocrisy, pushing employees in this direction without giving them the freedom to actually start doing it. As an example, do you recognize the following leadership style? According to Command-and-Conquer, managers-slash-leaders figure out where the business is going to go, and then issue the appropriate orders to their lieutenants to move the business in that direction. Their lieutenants in turn divide up the tasks into smaller chunks and command their reports to implement them. This continues down the org-chart until eventually someone at the bottom actually does some work. In this model, a programmer is a cog in the machine: a typist who carries out one part of management’s orders. Some businesses actually run this way. You can always tell when you are dealing with such a business, because the person you are talking to is doing something infuriating and senseless, and they know it, and they might even care, but there’s nothing they can do about it. Joel Spolsky: The Development Abstraction Layer This is an excellent example of how to efficiently kill motivation and creativity; encouraging a workforce with breaths smelling like urine and an awful way of growing skillful developers. Visionary people who have learned self-motivation and able to visualize patterns and connect the dots in a conceptual age and converging world will not accept this kind management and will find ways to escape it one way or another. Leadership is a crucial part in business and military success in the same way as creating a culture of innovation. If innovation does not happen in the organization, your leaders are probably killing it one way or another. For example, only trusting your users to tell you how to run your business. If I had asked people what they wanted, they would have said faster horses. Henry Ford Users know what problem they have, which is important and you should listen to your users. But listening to users *exclusively* will never make you entrepreneurial. Steve Jobs told customers what they wanted instead of asking for feedback. And before i forget – Henry Ford built cars… What will happen if peoples ideas are rejected over and over again, maybe because their leaders does not trust them? They will quickly stop sharing them because clearly their ideas are worthless to the company. But lets assume that you have a lot of creative people with mind blowing, innovative and relevant ideas. It takes almost as much creativity to understand a good idea as to have it in the first place. Alan Kay Having wonderful ideas is not enough. If you have to spend just as much time to convince people of the idea as on the idea themselves there will be no innovation left at the end of the day. Leaders must realize that innovation is fragile and must actively try to find and encourage it. Stakeholders must also accept risk and communicate that failure is expected and not punish failures. At the same time – execution is critical. Ideas are not useful if they are irrelevant to business or if you cannot make reality out of them. It is important to build excellent teams that are able take an idea and work with it. If you give a good idea to a mediocre group, they’ll screw it up. If you give a mediocre idea to a good group, they’ll fix it. Or they’ll throw it away and come up with something else. Ed Catmull, Pixar: Keep Your Crises Small This is why people should be allowed to work on the things that motivates them. Motivation is of course not a guarantee for success. But with a good ideas, passion and dreams your chances for success increase drastically since individuals will be way more productive and caring than on average. Ignore this and you may find your co-workers spending most of their time at the coffee machine or surfing the web instead of doing incredible work. Some say “ignorance is bliss” and I guess they are right. But I do not want to take the easy way out… My brain is simply allergic to ignorance. Fish rot from the head as do organisations. Remember that. Reference: Motivation, Creativity and Innovation from our JCG partner Kristoffer Sjogren at the deephacks blog....

Useful Ant build tags

Questions: How to do following tasks in ant file?Make zip file. Run command. Copy files to remote machine. Run commands on Remote Linux machine. Open an input box and respond to input value. Make an ant call.Answers: 1. Make zip file: Following is the xml for making zip file in ant: <zip destfile='${destination.folder}/'> <fileset dir= '${Source.folder}' /> </zip> In here “destfile” is the name and location of the created zip file. Inside fileset tag the dir attricute is used to specify source folder form where all files will be zipped. 2. Run commands: Here I will show you how to start tomcat in ant file to demonstrate how to run commands in ant. Following is the xml for this: <exec dir='${tomcat.home}/bin' executable='cmd' os='Windows XP'> <arg line='/c startup.bat'/> </exec> Here “${tomcat.home}” is the path of the tomcat folder. The command is given in “<arg>” tag in “line” attribute. Note: To run following commands you will need JSCH jar. 3. Copy files to remote machine: If remote machine is in your LAN and you can access it directly as \\remote_machine\path then you can use simple copy command as follows: <copy file='${source.folder.file} ' todir='\\remote\path'> </copy> To copy files on remote machine that supports SCP use following tag: <scp file='${source.folder.file} ' todir='${remote.user}@${}:${}' password='${remote.password}' trust='true'> </scp> In above both commands “file” is the source file which you want to copy with its path. And “todir” is the remote machine folder path where you want to copy the file. 4. Run commands on remote machine: You can use following tag to execute commands on remote Linux machine. <sshexec host='${}' username='${remote.username}' password='${remote.password}' command='${}' trust='true' /> For example to start tomcat use following tag: <sshexec host='${}' username='${remote.user}' password='${remote.password}' command='sh ${tomcat.home}/' trust='true' />5. Open an input box and respond to input value: To open an input dialog use following tag: <input message='Enter id: ' addproperty=''> </input> Here “” in “addproperty” is the variable name which holds input value. Now to check if user has denied to enter value in input: <condition property='do.abort'> <equals arg1='n' arg2='${}'/> </condition> <fail if='do.abort'>Build aborted by user.</fail> And if user enters value and press OK then after you can refer to entered value as “${}”. 6. Make an ant call: <antcall target='targetName'> </antcall> Here “target” is the name of the target that will be executed. Note: In above examples all values starting with “${”and ending with “}’’ are variables and you may have to put appropriate values in them to successfully run them. Reference: Useful Ant build tags from our JCG partner Harsh Raval at the harryjoy blog....

Checked and unchecked exceptions in Java

Java has two types of exceptions – checked and unchecked. In short – checked are meant for cases when the developer can reasonably recover from the exception, while unchecked exceptions are programming errors that can’t be dealt with. This article explains when to use which. But it is not that simple – checked exceptions are making the code “uglier”. They force the developer to write try/catch blocks or to rethrow the exception. But rethrowing hides another problem – some exceptions should not cross module boundaries. The most common thing to do when you are forced to catch a checked exception, which you have no idea what to do with, is to simply wrap it in a RuntimeException and rethrow it. Actually, it might not be the most common – especially novice programmers tend to swallow exceptions, with empty catch blocks. Log & rethrow leads to doubled stacktraces sometimes, if there is some general layer for exception handling. Anyway, there are multiple bad practices here that lead to code that’s hard to debug and maintain. Some say checked exceptions should be eliminated completely, given the verbosity, tediousness and error-proneness they introduce. C# does not have checked exceptions at all. Of course eliminating them should take into account backward compatibility. I, however, believe the decision to have these two types of exceptions has its plus(es). It forces the developer to think that an exception is likely to happen in this situation, so he must take measures. The API declares that it will throw exceptions and the developer sees this compile-time. It enforces compile-time safety. You should not wait until the code goes to production to discover that something might fail. Javadoc? Well, a good option to say that, but I bet no one will read the javadoc until the exception actually happens. So, how to have the “best of both worlds” ? I had a bizarre idea (explained in details here) for APIs to define two interfaces (linked through inheritance, so that only one interface is actually supported), and providing a way, through a factory, to get either an implementation whose methods throw checked exceptions, or one that wraps the checked exceptions into unchecked ones. It may be viable, it may be stupid, I can’t tell. For now it just looks strange. But the above is a workaround at best. Then another idea came – introduce @RethrowExceptions annotation on methods. It will tell the compiler that in this method you don’t want to handle checked exceptions, but you also don’t want to declare them to be thrown. (The name of the annotation can be improved). In the simplest implementation that comes to my mind this can simply tell the compiler to surround the whole method body with try {..} catch (Exception ex) { throw new RuntimeException(ex);}. The benefits:you are still warned by the compiler that the method you are using can throw an exception and you must think of handling it you don’t make your code ugly by needless try/catches. And you don’t force your callers to think what to do with the exception the possibility of swallowing the exception is decreased.So in short – such an annotation would mark a method as one that is unable to handle the exception and does not want to propagate this decision to its callers. This idea sounds less bizarre. I guess it can be implemented even now, using a compiler plug-in. Or perhaps it is already implemented in something like project lombok? Don’t forget to share! Reference: Checked and Unchecked Exceptions in Java from our JCG partner Bozhidar Bozhanov at the Bozho’s tech blog blog....

Fault-tolerance primitives in Scala: links and traps

Over the last decade Actor Model is actively marketed as a simple, effective and save concept for building concurrency. Being popularized by Erlang in early nineties, and used as a primary concurrency construct in Scala, Actors offer share-nothing event-driven model, where creating and maintaining a single actor is cheap (so that you can keep running millions of them), and remote actors working at distributed nodes look and feel as the local ones. However, being used unwisely, actors may bring even more pain and boilerplate, than thread-based concurrency or STM. Lack of the in-depth documentation on actor-based modeling leads to misconceptions and myths that can be broken working through best-practices and design guidelines of building fault-tolerant systems in Erlang. Fault-tolerance primitives Defensive programing forces you to put a huge effort to protect the system against all kinds of unexpected behavior you may think of, checking inputs and current state whenever possible, and defining ways to escalate the problem on a higher level. Being a defensive programmer you should always be proactive and look for a new potential problems to handle, because even the smallest one causing a failure in insignificant component may lead to a huge system crash. Fault-tolerant systems as apposed don’t tend to predict all possible failure reasons (to prevent a single component from crash), but rather isolate the component from the rest of the system and restart it, keeping the system work consistently. If restart of the component doesn’t help, the problem may be propagated to the higher level, in order to [possibly] restart the parties that exchange messages with the component. As Joe Armstrong says on his book “Programming Erlang”, when one actor dies in the room full of actors, others should probably notice that and start fixing the problem (cleaning out the bodies). In Erlang and Scala this behavior is achieved by linking actors. In the most basic form, when two actors are linked, and one of them dies, it sends an exit signal to another actor to terminate it too. Actors linking is bidirectional operation, so, when you link actor A to B, B is on the background linked to A, and death of any of them cause sending an exit signal to another. In Erlang it’s possible to create unidirectional linking with monitors (when monitored process dies, ‘DOWN’ message is sent to the handler). There’s no analogue to the monitors in Scala standard library, however implementing it on demand would be easy. Linked actor can create an exit trap, so that exit signal will be processed as a normal message, not causing actor termination. Exit signal normally contains the reference to the failed actor (that can be used to restart it), and failure reason. In “Programming Erlang” several link/trap exit scenarios are represented with a simple example: -module(edemo1). -export([start2]). start(Bool, M) -> A = spawn(fun() -> a() end), B = spawn(fun() -> b(A, Bool) end), C = spawn(fun() -> c(B, M) end), sleep(1000), status(b, B), status(c, C). In the code above three processes are created, synchronized with sleep, in order to give them time to process the passed messages (ugly in general, but works for a simple example), and afterwards, their state is checked. Actors are defined as follows: a() -> process_flag(trap_exit, true), wait(a). b(A, Bool) -> process_flag(trap_exit, Bool), link(A), wait(b). c(B, M) -> link(B), case M of {die, Reason} -> exit(Reason); {divide, N} -> 1N, wait(c); normal -> true end. Process A always traps exits, process B is linked to A and traps exits depending on the function input, process C linked to B receives messages and either makes computation or fails. wait(Prog) -> receive Any -> io:format('Process ~p received ~p~n' ,[Prog, Any]), wait(Prog) end. Method wait recursively receives messages printing out the message content. Being translated into Scala, using standard Actors library, the example looks as follows: object LinkedActors { case class Die(reason:AnyRef) case class Divide(number: Int) def status(name: String, actor: Actor) = println('Actor %s is %s' format(name, actor.getState)) def printMessage(actorName: String):PartialFunction[Any, Unit] = {case msg => println('Actor %s received %s' format(actorName, msg))} def start(isTrapExit: Boolean, message: Any) = { val A = actor{ self.trapExit = true loop(react(printMessage('a'))) } val B = actor{ self.trapExit = isTrapExit self link A loop(react(printMessage('b'))) } val C = actor{ self link B loop{ react{ case Die(reason) => exit(reason) case Divide(number) => 1number } } } C! message Thread.sleep(1000) status('b', B) status('c', C) } } Essentially, code is the same with the difference that messages accepted by actor C are classified with case classes and receive behavior of actors B and C is represented with partial functions. Let’s pass some inputs to the start method in order to see, how will the chained actors behave, when some of them die: scala> start(false, Die('abc')) Actor a received Exit(scala.actors.Actor$$anon$1@dc8f6d,abc) Actor b is Terminated Actor c is Terminated Actor C receives message Die and exists with the reason “abc”. Actor B linked to C doesn’t trap exits, therefore it terminates too. As long as A connected to B traps exits, when actor B terminates it sends A a message with the reason why it has failed (case class with the following signature): ** An `Exit` message (an instance of this class) is sent to an actor * with `trapExit` set to `true` whenever one of its linked actors * terminates. * * @param from the actor that terminated * @param reason the reason that caused the actor to terminate * case class Exit(from: AbstractActor, reason: AnyRef) In the same time, when exit is expected (not caused with computational exception), linked actors are untouched: scala> start(false, Die('normal)) Actor b is Suspended Actor c is Terminated In the snippet below, unhandled division by zero exception causes C and B to die: scala> start(false, Divide(0)) Actor a received Exit(scala.actors.Actor$$anon$1@113eb9c,UncaughtException (scala.actors.Actor$$anon$1@1a1446d,Some(Divide(0)),Some(scala.act ors.ActorProxy@14f83d1),java.lang.ArithmeticException: by zero)) Actor b is Terminated Actor c is Terminated If we force B to trap exit, the actor stays alive in all the scenarios described above: scala> start(true, Die('abc')) Actor b received Exit(scala.actors.Actor$$anon$1@13e49a8,abc) Actor b is Suspended Actor c is Terminated Compared to the first snippet, now B receives exit message from C. Unhandled errors are also not propagated to A: scala> start(true, Divide(0)) Actor b received Exit(scala.actors.Actor$$anon$1@119f779,UncaughtException (scala.actors.Actor$$anon$1@119f779,Some(Divide(0)),Some(scala.act ors.ActorProxy@14f83d1),java.lang.ArithmeticException: by zero)) Actor b is Suspended Actor c is TerminatedBasic Supervisor Exit message contains the reference to the failed actor that can be used to restart it. It’s possible to implement a very simple actor supervisor, by analogy with supervisor behavior in Erlang. case class ChildSpecification(worker: Worker, restartBehavior: Child RestartBehavior.Value = permanent) case class OneForOne(maxRestarts: Long = 3, maxTime: Long = 3000) extends RestartStrategy case class OneForAll(maxRestarts: Long = 3, maxTime: Long = 3000) extends RestartStrategy class Supervisor(supervisorId: String, strategy: RestartStrategy, childSpecs: List[ChildSpecification]) extends Worker { ... override def act = { self.trapExit = true linkWorkers loop { react{ case Exit(worker: Worker, reason) => println('Worker [%s] has failed due to [%s]' format(, reason)) if(worker.restartsInPeriod(strategy.maxTime) >= strategy .maxRestarts) exit('Maximum restart intensity for %s is reached!' format( strategy match { case str:OneForOne => restartWorker(worker) case str:OneForAll => childSpecs.foreach{spec => restartWorker(spec.worker)} } case Terminate(reason) => println('Supervisor terminated with reason [%s]' format(reason)) exit(reason) } } } ... } Supervisor itself is a normal Scala Actor that traps messages from the other actors linked to it (workers, in terms of supervision terminology), and restarts either only one failed actor or all supervised actors. When restart frequency reaches the limit specified by the restart strategy, supervisor terminates, so that supervisor at the higher hierarchy position may try to handle the problem. In the simplest scenario, supervisor restarts the actor terminated due to uncaught exception: 'Actor terminated due to uncaught exception is restarted by the supervisor' in { val worker = new SimpleWorker('simple_worker') Supervisor('basic_supervisor', OneForOne(), List(ChildSpecification(worker))).start worker !? (1000, Divide(0)) (worker !? (1000, Divide(1))).asInstanceOf[Option[Int]] must be equalTo Some(1) } Output of the specification is: Starting worker simple_worker Worker [simple_worker] has failed due to [UncaughtException(com.vasilrem.linked. SupervisorSpec$SimpleWorker@fd54ec,Some(Divide(0)),Some(scal a.actors.Channel@16167ab),java.lang.ArithmeticException: by zero)] Restarting worker [simple_worker]... [info] + Actor terminated due to uncaught exception is restarted by the supervisor In a more complex scenario, when supervisors are linked in a tree, high-level supervisor restarts low-level supervisor, when it dies, that causes restart of the workers linked to it: 'High-level supervisor restarts low-level supervisor and the wrokers linked to it' in{ val worker = new SimpleWorker('simple_worker') val lowLevel = Supervisor('lowlevel_supervisor', OneForOne(), List(ChildSpecification(worker))) val highLevel = Supervisor('lowlevel_supervisor', OneForOne(), List(ChildSpecification(lowLevel))).start worker.getState must not be equalTo(State.Terminated) lowLevel ! Terminate('Kill lowlevel') Thread.sleep(1000) worker.getState must not be equalTo(State.Terminated) } Test output is following: Starting worker lowlevel_supervisor Starting worker simple_worker Supervisor terminated with reason [Kill lowlevel] Worker [lowlevel_supervisor] has failed due to [Kill lowlevel] Restarting worker [lowlevel_supervisor]... Starting worker simple_worker [info] + High-level supervisor restart low-level supervisor You can find more specifications and code of the supervisor here. Happy coding and don’t forget to share! Reference: Fault-tolerance primitives in Scala: links and traps from our JCG partner Vasil Remeniuk at the Vasil Remeniuk blog blog....

Android: Copy-paste with Intent and support for HTML

Android provides support for copy and paste feature using ClipBoardManager. The provides a brief description along with an image which helps to understand the Copy-paste framework. “To copy data, an application puts a ClipData object on the ClipboardManager global clipboard. The ClipData contains one or more ClipData.Item objects and one ClipDescription object. To paste data, an application gets the ClipData, gets its MIME type from the ClipDescription, and gets the data either from the ClipData.Item or from the content provider referred to by ClipData.Item.”This tutorial covers the latest feature introduced in JellyBean, supporting the styled text. We will cover the methods listed below.HTML supported methodsClipData.newHtmlText() with clipboard Intent.putExtra() with Intent.EXTRA_HTML_TEXT Intent.setClipData() with newHtmlText() methodProject Information : Meta-data about the project. Platform Version : Android API Level 16. IDE : Eclipse Helios Service Release 2 Emulator : Android 4.1(API 16) Prerequisite : Preliminary knowledge of Android application framework and Intent.Example source code To start with, create project by Eclipse > File> New Project>Android Application Project. The following dialog box will appear. Fill the required field, i.e Application Name, Project Name and Package. Now press the next button.Once the dialog box appears, select the BlankActivity and click the next button.Fill the Activity Name and Layout file name in the dialog box shown below and hit the finish button.This process setups the basic project files. Now we are going to add view components in the layout activity_jbclipboard.xml file. You can modify the layout file using either Graphical Layout editor or xml editor. In this layout, we will add four buttons, namely copy, paste, send html Intent, and send clipdata intent and attach onClick methods with CopyHtml, pasteHtml, sendHtmlIntent and sendClipdataIntent buttons, respectively. These methods will be defined in JBClipboard class. We will also include two radio buttons i.e Paste HTML and Paste Text. These radio buttons help to select the required type of text to be extracted from the clipboard. We also have three EditViews to show the text. The first Editview contains the Styled text, the second one shows either HTML or text string. The third one shows the Coerce HTML string. The layout file is shown below. <LinearLayout xmlns:android='' xmlns:tools='' android:layout_width='match_parent' android:layout_height='match_parent' android:orientation='vertical'><EditText android:id='@+id/etCopy' android:layout_width='fill_parent' android:layout_height='wrap_content' android:padding='@dimen/padding_medium' android:gravity='top' android:scrollHorizontally='false' android:inputType='textMultiLine' /> <RadioGroup android:id='@+id/rbgTextHTML' android:orientation='horizontal' android:layout_width='fill_parent' android:layout_height='wrap_content' > <RadioButton android:id='@+id/rbHtml' android:layout_weight='.5' android:layout_width='0dp' android:layout_height='wrap_content' android:checked='true' android:text='@string/rbHtml'/> <RadioButton android:id='@+id/rbText' android:layout_weight='.5' android:layout_width='0dp' android:layout_height='wrap_content' android:text='@string/rbText'/> </RadioGroup> <LinearLayout android:layout_width='match_parent' android:layout_height='wrap_content' android:orientation='horizontal'> <Button android:layout_width='0dp' android:layout_height='wrap_content' android:layout_weight='.5' android:onClick='copyHtml' android:text='@string/btCopy'/> <Button android:layout_width='0dp' android:layout_height='wrap_content' android:layout_weight='.5' android:onClick='pasteHtml' android:text='@string/btPaste'/> </LinearLayout> <LinearLayout android:layout_width='fill_parent' android:layout_height='wrap_content' android:orientation='horizontal'> <Button android:layout_width='0dp' android:layout_weight='.5' android:layout_height='wrap_content' android:onClick='sendHtmlIntent' android:text='@string/btSendHtmlIntent'/> <Button android:layout_width='0dp' android:layout_weight='.5' android:layout_height='wrap_content' android:onClick='sendClipdataIntent' android:text='@string/btSendClipdataIntent'/> </LinearLayout> <TextView android:layout_width='fill_parent' android:layout_height='wrap_content' android:text='@string/tvCopiedText'/> <EditText android:id='@+id/etPaste' android:layout_width='fill_parent' android:layout_height='wrap_content' android:inputType='textMultiLine' android:gravity='top' android:scrollHorizontally='false' /> <TextView android:layout_width='fill_parent' android:layout_height='wrap_content' android:text='@string/tvcoerceText'/> <EditText android:id='@+id/etPasteCoerceText' android:layout_width='fill_parent' android:layout_height='wrap_content' android:gravity='top' android:scrollHorizontally='false' android:inputType='textMultiLine'/> </LinearLayout> Now we need to add two more layout files for two different activities. Let’s define the first Layout file by Eclipse > File > Android XML file. Eventually we get the following dialog box. Make sure that the Layout resource type is selected. Name the layout file as activity_htmlintent and then click finish.Add two TextViews for showing text tags and two EditTexts view for showing the HTML and text strings. This layout file will be attached with which gets invoked once “text/html” type intent is broadcasted. The content of the activity_htmlintent.xml is given below: <LinearLayout xmlns:android='' android:layout_width='match_parent' android:layout_height='match_parent' android:orientation='vertical' > <TextView android:layout_width='match_parent' android:layout_height='wrap_content' android:text='@string/tvIntentHtml' /> <EditText android:id='@+id/etHtml' android:layout_width='match_parent' android:layout_height='wrap_content' android:inputType='textMultiLine' android:padding='@dimen/padding_medium' android:scrollHorizontally='false' /> <TextView android:layout_width='match_parent' android:layout_height='wrap_content' android:text='@string/tvIntentText' /> <EditText android:id='@+id/etText' android:layout_width='match_parent' android:layout_height='wrap_content' android:inputType='textMultiLine' android:padding='@dimen/padding_medium' android:scrollHorizontally='false' /> </LinearLayout> Now we will define another layout file called activity_clipdataintent.xml. Follow the similar steps given above to generate the layout xml file. This file is similar to the above file and contains two EditView and two TextViewThe. The content of the file is given below. This layout file will be attached to the ClipdataIntentActivity class which gets invoked when an intent with Clipdata object is passed. <LinearLayout xmlns:android='' android:layout_width='match_parent' android:layout_height='match_parent' android:orientation='vertical' ><TextView android:layout_width='match_parent' android:layout_height='wrap_content' android:text='@string/tvIntentClipdataHtml' /><EditText android:id='@+id/etClipBoardHtml' android:layout_width='match_parent' android:layout_height='wrap_content' android:inputType='textMultiLine' android:padding='@dimen/padding_medium' android:scrollHorizontally='false' /><TextView android:layout_width='match_parent' android:layout_height='wrap_content' android:text='@string/tvIntentClipdataText' /><EditText android:id='@+id/etClipBoardText' android:layout_width='match_parent' android:layout_height='wrap_content' android:inputType='textMultiLine' android:padding='@dimen/padding_medium' android:scrollHorizontally='false' /> </LinearLayout> You may have notice there are string resources have been used in the layout file. Define this string constants in string.xml as shown below. <resources><string name='app_name'>JellyBeanClipboard</string> <string name='menu_settings'>Settings</string> <string name='title_activity_jbclipboard'>JBClipboard</string> <string name='title_activity_htmlintent'>Html Intent Activity</string> <string name='title_activity_clipdataintent'>ClipData Intent Activity</string> <!-- CDATA tag is required otherwise you can't have the html text properly parsed in Textview --> <string name='tvHtml'><![CDATA[<b>Link:</b> <a href=''>Code4Reference</a>]]></string> <!-- Text string for button --> <string name='btCopy'>Copy</string> <string name='btPaste'>Paste</string> <string name='btSendHtmlIntent'>send HTML Intent</string> <string name='btSendClipdataIntent'>send ClipData Intent</string> <!-- Text string for RadioButton --> <string name='rbHtml'>Paste Html</string> <string name='rbText'>Paste Text</string> <!-- Text string for Text View --> <string name='tvCopiedText'><b><i>Copied text</i></b></string> <string name='tvcoerceText'><b><i>Copied coerce Text</i></b></string> <string name='tvIntentText'><b><i>Intent Text</i></b></string> <string name='tvIntentHtml'><b><i>Intent Html</i></b></string> <string name='tvIntentClipdataText'><b><i>Intent Clipdata Text</i></b></string> <string name='tvIntentClipdataHtml'><b><i>Intent Clipdata Html</i></b></string> </resources> Once we are done with the layout files, it’s time to define the activity classes. Let’s define the main activity called JBClipboard. This activity has various methods which use the HTML supported APIs. The embedded comments will help to understand the code. package com.code4reference.rakesh.jellybeanclipboard;import; import android.content.ClipData; import android.content.ClipDescription; import android.content.ClipboardManager; import android.content.Context; import android.content.Intent; import android.os.Bundle; import android.text.Html; import android.text.Spannable; import android.view.View; import android.widget.EditText; import android.widget.RadioButton; import android.widget.Toast;import com.example.jellybeanclipboard.R;public class JBClipboard extends Activity {EditText etCopy; EditText etPaste; EditText etPasteCoerceText; RadioButton rbText; RadioButton rbHtml; ClipboardManager mClipboard;ClipboardManager.OnPrimaryClipChangedListener mPrimaryChangeListener = new ClipboardManager.OnPrimaryClipChangedListener() { /** * This method is a callback. It get called when the primary clip * on the clipboard changes. */ public void onPrimaryClipChanged() { //Toast message will appear whenever the clipboad //primary data changes. Utility.showToastMessage(getApplicationContext(), 'Primary clipdata changed', Toast.LENGTH_SHORT); } };@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_jbclipboard); etCopy = (EditText) findViewById(; etPaste = (EditText) findViewById(; etPasteCoerceText = (EditText) findViewById(; etCopy.setText(Html.fromHtml(getString(R.string.tvHtml))); rbText = (RadioButton) findViewById(; rbHtml = (RadioButton) findViewById(; mClipboard = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE); mClipboard.addPrimaryClipChangedListener(mPrimaryChangeListener); }/** * This method gets called when 'Copy' button get pressed. * @param view */ public void copyHtml(View view) { String htmlText = getHtmltxt(etCopy); String plainText = getOnlyText(etCopy); mClipboard.setPrimaryClip(ClipData.newHtmlText('HTML Text', plainText, htmlText));} /** * This method gets called when 'Paste' button get pressed. * @param view */ public void pasteHtml(View view) { // Check if there is primary clip exsiting. // If it does then echeck the mime type to make sure // it has HTML content. if (mClipboard.hasPrimaryClip() && mClipboard.getPrimaryClipDescription().hasMimeType( ClipDescription.MIMETYPE_TEXT_HTML)) { // Get the very first item from the clip. ClipData.Item item = mClipboard.getPrimaryClip().getItemAt(0);// If 'Paste HTML' radio button is selected then paste // HTML in the Textview. if (rbHtml.isChecked()) { etPaste.setText(item.getHtmlText()); } else { // Paste the only text version. etPaste.setText(item.getText()); } // Paste the CoerceText . etPasteCoerceText.setText(item.coerceToText(this)); } } /** * This method gets called when 'send Html Intent' button get pressed. * @param view */ public void sendHtmlIntent(View view) { // This kind of intent can be handle by this application // Or other application which handle text/html type Intent Intent intent = new Intent(Intent.ACTION_SEND);String htmlText = getHtmltxt(etCopy); String text = getOnlyText(etCopy); intent.putExtra(Intent.EXTRA_HTML_TEXT, htmlText); intent.putExtra(Intent.EXTRA_TEXT, text); intent.setType('text/html'); startActivity(Intent.createChooser(intent, null)); }/** * This method gets called when 'send Clipdata Intent' button get pressed. * * @param view */ public void sendClipdataIntent(View view) { String htmlText = getHtmltxt(etCopy); String plainText = getOnlyText(etCopy); Intent intent = new Intent(this, ClipdataIntentActivity.class); //create a clipdata object with HTML text. //and associate with the intent. intent.setClipData(ClipData.newHtmlText( 'HTML text in Intent's clipdata', plainText, htmlText)); //Start the activity which can handle clipData object. startActivity(intent); } @Override protected void onDestroy() { super.onDestroy(); //Remove the ClipChanged Listener to save the resources. mClipboard.removePrimaryClipChangedListener(mPrimaryChangeListener); } /** * This method gets the EditText object and returns the HTML text. It * can be called only with EditTexts having spannable object with * the HTML text. * * @param editText * @return */ private String getHtmltxt(EditText editText) { //get the spannable object from EditText Spannable spannable = (Spannable) editText.getText(); //return the HTML text from spannable object. return Html.toHtml(spannable); }/** * This method takes the EditText object which has spannable object with HTML * text and returns the simple text without HTML tags. * * @param editText * @return */ private String getOnlyText(EditText editText) { return editText.getText().toString(); } }Now we are going to define HTMLIntentActivitywhich handles intent with HTML text. The intended intent will be triggered by the main activity (JBClibboard) package com.code4reference.rakesh.jellybeanclipboard;import com.example.jellybeanclipboard.R;import; import android.content.Intent; import android.os.Bundle; import android.widget.EditText;public class HtmlIntentActivity extends Activity {private EditText etHtml; private EditText etText; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_htmlintent); etHtml = (EditText) findViewById(; etText = (EditText) findViewById(;//Get the intent that started this activity Intent intent = getIntent(); //Make sure intent and its type is not null. if (intent != null && intent.getType() != null && intent.getType().equals('text/html')) { //This contition will full-fill when this application receive the //intent who's type is 'test/html'. In this application sendHtmlIntent //method sends this type of Intent. Bundle bundle = intent.getExtras(); if(bundle != null){ etHtml.setText(bundle.getCharSequence(Intent.EXTRA_HTML_TEXT)); etText.setText(bundle.getCharSequence(Intent.EXTRA_TEXT)); } } } } Now we will define another activity called ClipdataIntentActivity which handles the Intent having clilpdata object. The content of the ClipdataIntentActivity class is given below. package com.code4reference.rakesh.jellybeanclipboard;import; import android.content.ClipboardManager; import android.content.Intent; import android.os.Bundle; import android.widget.EditText; import android.widget.Toast; import android.content.ClipData; import android.content.ClipDescription;import com.example.jellybeanclipboard.R;public class ClipdataIntentActivity extends Activity { private EditText etHtml; private EditText etText; ClipboardManager mClipboard;@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_clipdataintent); etHtml = (EditText) findViewById(; etText = (EditText) findViewById(;//Get the intent that started this activity Intent intent = getIntent(); if (intent != null) { ClipData clipdata = intent.getClipData(); //Make sure clipdata object is not null and it has HTML MIME type. if (clipdata != null && clipdata.getDescription().hasMimeType( ClipDescription.MIMETYPE_TEXT_HTML)) {ClipData.Item item = clipdata.getItemAt(0); etHtml.setText(item.getHtmlText()); etText.setText(item.getText()); } else { Utility.showToastMessage(this, 'Intent clipdata doesn't have HTML', Toast.LENGTH_SHORT); }} }}Create another class called Utility. It contains method to display the toast message. As you may have noticed that this function is static, so there is no need to create an object to invoke this method. It’s always a good idea to put the utility methods in a separate file and access this file in different places. In this way your code will be organized. package com.code4reference.rakesh.jellybeanclipboard;import android.content.Context; import android.widget.Toast;public class Utility {public static void showToastMessage(Context context, String message, int duration){ Toast.makeText(context, message, duration).show(); } } And finally define the Anroid Manifest file which basically provides Application information to the Android system. Here, you should notice that the HtmlIntentActivity Activity has an intent filter and specifies the Intent type as “text/html”. It basically means that the activity can handle intent whose type is text/html. Remaining part of the file is simple and easy to understand. <manifest xmlns:android='' package='com.example.jellybeanclipboard' android:versionCode='1' android:versionName='1.0' ><uses-sdk android:minSdkVersion='16' android:targetSdkVersion='16' /><application android:icon='@drawable/ic_launcher' android:label='@string/app_name' android:theme='@style/AppTheme' > <activity android:name='com.code4reference.rakesh.jellybeanclipboard.JBClipboard' android:label='@string/title_activity_jbclipboard' > <intent-filter> <action android:name='android.intent.action.MAIN' /> <category android:name='android.intent.category.LAUNCHER' /> </intent-filter> </activity> <activity android:name='com.code4reference.rakesh.jellybeanclipboard.ClipdataIntentActivity' android:label='@string/title_activity_clipdataintent' > </activity> <activity android:name='com.code4reference.rakesh.jellybeanclipboard.HtmlIntentActivity' android:label='@string/title_activity_htmlintent' > <intent-filter> <action android:name='android.intent.action.SEND' /> <category android:name='android.intent.category.DEFAULT' /> <!-- This activity will get launched when proper Intent type will match. In this case Intent type is 'text/html' --> <data android:mimeType='text/html' /> </intent-filter> </activity> </application></manifest> Once you are done with the coding, just execute it. You can launch this application on your android device or emulator. Make sure that the Android version is 16(Jelly Bean) or higher, only then this application will work. Screen-shots of the applications are shown below.You can get the source code here at github. Happy coding and don’t forget to share! Reference: Copy-paste with Intent and support for HTML on Android. from our JCG partner Rakesh Cusat at the Code4Reference blog....
Java Code Geeks and all content copyright © 2010-2015, Exelixis Media Ltd | Terms of Use | Privacy Policy | Contact
All trademarks and registered trademarks appearing on Java Code Geeks are the property of their respective owners.
Java is a trademark or registered trademark of Oracle Corporation in the United States and other countries.
Java Code Geeks is not connected to Oracle Corporation and is not sponsored by Oracle Corporation.
Do you want to know how to develop your skillset and become a ...
Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you our best selling eBooks for FREE!

Get ready to Rock!
To download the books, please verify your email address by following the instructions found on the email we just sent you.