Featured FREE Whitepapers

What's New Here?


How to manage Quartz remotely

Option 1: JMX Many people asked can they manage Quartz via JMX, and I am not sure why Quartz doc won’t even mention it. Yes you can enable JMX in quartz with the following in quartz.properties org.quartz.scheduler.jmx.export = true After this, you use standard JMX client such as $JAVA_HOME/bin/jconsole to connect and manage remotely. Option 2: RMI Another way to manage quartz remotely is to enable RMI in Quartz. If you use this, you basically run one instance of Quartz as RMI server, and then you can create second Quartz instance as RMI client. These two can talk remotely via a TCP port. For server scheduler instance, you want to add these in quartz.properties org.quartz.scheduler.rmi.export = true org.quartz.scheduler.rmi.createRegistry = true org.quartz.scheduler.rmi.registryHost = localhost org.quartz.scheduler.rmi.registryPort = 1099 org.quartz.scheduler.rmi.serverPort = 1100And for client scheduler instance, you want to add these in quartz.properties org.quartz.scheduler.rmi.proxy = true org.quartz.scheduler.rmi.registryHost = localhost org.quartz.scheduler.rmi.registryPort = 1099The RMI feature is mentioned in Quartz doc here. Quartz doesn’t have a client API, but use the same org.quartz.Scheduler for both server and client. It’s just the configuration are different. By different configuration, you get very different behavior. For server, your scheduler is running all the jobs, while for client, it’s simply a proxy. Your client scheduler instance will not run any jobs! You must be really careful when shutting down client because it does allow you to bring down the server! These configurations have been highlighted in the MySchedule project. If you run the webapp, you should see a screen like this demo, and you will see it provided many sample of quartz configurations with these remote managment config properties. If configure with RMI option, you can actually still use MySchedule web UI to manage the Quartz as proxy. You can view and drill down jobs, and you can even stop or shutdown remote server! Based on my experience, there is a down side of using Quartz RMI feature though. That is it creates a single point of failure. There is no fail over if your RMI server port is down! Reference: How to manage Quartz remotely from our JCG partner Zemian Deng at the A Programmer’s Journal blog....

Spring Profiles and Java Configuration

My last blog introduced Spring 3.1’s profiles and explained both the business case for using them and demonstrated their use with Spring XML configuration files. It seems, however, that a good number of developers prefer using Spring’s Java based application configuration and so Spring have designed a way of using profiles with their existing @Configuration annotation. I’m going to demonstrate profiles and the @Configuration annotation using the Person class from my previous blog. This is a simple bean class whose properties vary depending upon which profile is active. public class Person { private final String firstName; private final String lastName; private final int age; public Person(String firstName, String lastName, int age) { this.firstName = firstName; this.lastName = lastName; this.age = age; } public String getFirstName() { return firstName; } public String getLastName() { return lastName; } public int getAge() { return age; } } Remember that the Guys at Spring recommend that Spring profiles should only be used when you need to load different types or sets of classes and that for setting properties you should continue using the PropertyPlaceholderConfigurer. The reason I’m breaking the rules is that I want to try to write the simplest code possible to demonstrate profiles and Java configuration. At the heart of using Spring profiles with Java configuration is Spring’s new @Profile annotation. The @Profile annotation is used attach a profile name to an @Configuration annotation. It takes a single parameter that can be used in two ways. Firstly to attach a single profile to an @Configuration annotation: @Profile("test1") and secondly, to attach multiple profiles: @Profile({ "test1", "test2" }) Again, I’m going to define two profiles “test1” and “test2” and associate each with a configuration file. Firstly “test1”: @Configuration @Profile("test1") public class Test1ProfileConfig { @Bean public Person employee() { return new Person("John", "Smith", 55); } } …and then “test2”: @Configuration @Profile("test2") public class Test2ProfileConfig { @Bean public Person employee() { return new Person("Fred", "Williams", 22); } } In the code above, you can see that I’m creating a Person bean with an effective id of employee (this is from the method name) that returns differing property values in each profile. Also note that the @Profile is marked as: @Target(value=TYPE)…which means that is can only be placed next to the @Configuration annotation. Having attached an @Profile to an @Configuration, the next thing to do is to activate your selected @Profile. This uses exactly the same principles and techniques that I described in my last blog and again, to my mind, the most useful activation technique is to use the “spring.profiles.active” system property. @Test public void testProfileActiveUsingSystemProperties() { System.setProperty("spring.profiles.active", "test1"); ApplicationContext ctx = new ClassPathXmlApplicationContext("profiles-config.xml"); Person person = ctx.getBean("employee", Person.class); String firstName = person.getFirstName(); assertEquals("John", firstName); } Obviously, you wouldn’t want to hard code things as I’ve done above and best practice usually means keeping the system properties configuration separate from your application. This gives you the option of using either a simple command line argument such as: -Dspring.profiles.active="test1"…or by adding # Setting a property value spring.profiles.active=test1to Tomcat’s catalina.properties So, that’s all there is to it: you create your Spring profiles by annotating an @Configuration with an @Profile annotation and then switching on the profile you want to use by setting the spring.profiles.active system property to your profile’s name. As usual, the Guys at Spring don’t just confine you to using system properties to activate profiles, you can do things programatically. For example, the following code creates an AnnotationConfigApplicationContext and then uses an Environment object to activate the “test1” profile, before registering our @Configuration classes. @Test public void testAnnotationConfigApplicationContextThatWorks() { // Can register a list of config classes AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(); ctx.getEnvironment().setActiveProfiles("test1"); ctx.register(Test1ProfileConfig.class, Test2ProfileConfig.class); ctx.refresh(); Person person = ctx.getBean("employee", Person.class); String firstName = person.getFirstName(); assertEquals("John", firstName); } This is all fine and good, but beware, you need to call AnnotationConfigApplicationContext’s methods in the right order. For example, if you register your @Configuration classes before you specify your profile, then you’ll get an IllegalStateException. @Test(expected = IllegalStateException.class) public void testAnnotationConfigApplicationContextThatFails() { // Can register a list of config classes AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext( Test1ProfileConfig.class, Test2ProfileConfig.class); ctx.getEnvironment().setActiveProfiles("test1"); ctx.refresh(); Person person = ctx.getBean("employee", Person.class); String firstName = person.getFirstName(); assertEquals("John", firstName); } Before closing today’s blog, the code below demonstrates the ability to attach multiple @Profiles to an @Configuration annotation. @Configuration @Profile({ "test1", "test2" }) public class MulitpleProfileConfig { @Bean public Person tourDeFranceWinner() { return new Person("Bradley", "Wiggins", 32); } } @Test public void testMulipleAssignedProfilesUsingSystemProperties() { System.setProperty("spring.profiles.active", "test1"); ApplicationContext ctx = new ClassPathXmlApplicationContext("profiles-config.xml"); Person person = ctx.getBean("tourDeFranceWinner", Person.class); String firstName = person.getFirstName(); assertEquals("Bradley", firstName); System.setProperty("spring.profiles.active", "test2"); ctx = new ClassPathXmlApplicationContext("profiles-config.xml"); person = ctx.getBean("tourDeFranceWinner", Person.class); firstName = person.getFirstName(); assertEquals("Bradley", firstName); }  In the code above, 2012 Tour De France winner Bradley Wiggins appears in both the “test1” and “test2” profiles.   Reference: Spring, Enterprise Java from our JCG partner Roger Hughes at the Captain Debug’s Blog blog. ...

Guaranteed messaging for topics, the JMS spec, and ActiveMQ

Recently a customer asked me to look closer at ActiveMQ’s implementation of “persistent” messages, how it applies to topics, and what happens in failover scenarios when there are non-durable subscribers. I had understood that the JMS semantics specify that only durable subscribers to a topic are guaranteed message delivery for a persistent delivery mode, even in the face of message-broker provider failure. But what does it have to say about non-durable subscribers for persistent messages? What’s the point of sending a message “persistent” when there are no durable subscribers? Upon looking at the exact wording of the spec, I became a little unsure. So I consulted the Java Message Service book (Richards, Monson-Haefel, and Chappell) for some more discussion around guaranteed messaging, checked into the ActiveMQ source code, and consulted with some of my co-workers. First, let’s look at what the spec says: From section 4.10 of the JMS spec: Most clients should use producers that produce PERSISTENT messages. This insures once-and-only-once message delivery for messages delivered from a queue or a durable subscription. Pretty clear, right? Using persistent message delivery ensures message delivery for a queue or durable subscription. From section 6.12: Unacknowledged messages of a nondurable subscriber should be able to be recovered for the lifetime of that nondurable subscriber. So now unacked messages of a non-durable subscriber should be able to be recovered? “for the lifetime of that non-durable subscriber” I guess… But later as part of 6.12: Only durable subscriptions are reliably able to recover unacknowledged messages. and… To ensure delivery, a TopicSubscriber should establish a durable subscription. Although the spec says very clearly [to the effect] that only queues and durable subscribers can take advantage of the store-and-forward guaranteed delivery, I guess I become confused at the “messages of a non-durable subscriber should be able to be recovered for the lifetime of the non-durable subscriber”Does the persistent protocol change for a topic depending on its consumers (where the message is considered the responsibility of the broker only after the broker has persisted the message and sent the producer an ack)? Does that mean even in the event of broker failure? Or is broker failure considered and end of the life of the subscription for non-durable subs? What happens with ActiveMQ when there is a network of brokers for persistent, non-durable topics? Can messages be missed if a broker in the network fails? What are the exact differences between sending a message “persistent” vs “non-persistent” to a topic with non-durable subscribers?These are two parts that have to be considered for this discussion of guaranteed delivery. The part where the publisher is sending a message to the broker, and when the consumer is receiving a message from the broker. For persistent messages, the protocol is for the sender to send a message and the broker to ack the message only after it has been persisted to a store. On the other hand, the consumer must ack the message after the broker has delivered it to say “hey, I’ll take responsibility of the message now”. Only then will the broker relinquish responsibility and removing it from its store.   Does the protocol change for a topic depending on its consumers? So for persistent messages sent to a topic (not taking into any account consumers at the moment), does the spec say anything about whether the message is supposed to be stored before the broker sends back its ack? No, it doesn’t. It’s left up to the implementors of the JMS broker in question. In the case of ActiveMQ, if there are ONLY non-durable subscriptions on a topic, it will NOT persist the message. The synchronous nature of the protocol does not change, i.e., if the message is sent persistent, the session will consider the exchange with the broker to be synchronous, and it will wait for a response from the broker before proceeding — but the broker will not actually persist the message. In ActiveMQ, this changes if there is AT LEAST one durable subscriber. Then the broker will persist the message (per the JMS spec).   Does that mean even in the event of a broker failure? The lifetime of a non-durable subscription is indeed broken if a broker fails. So a message will not be delivered, even if it’s sent persistent, to a non-durable subscriber in the event of broker failure (or any other termination of the life of that non-durable sub). Additionally, a message will not be redelivered in the face of broker failure for non-durable subscriptions.   What happens in a network of brokers? Messages can indeed be lost. Consider this network of brokers where A -> B -> C and subscription is demand forwarded from C -> B -> A. So if we have a producer at A producing to a topic “topic.foo” and a non-durable consumer is on broker C consuming from “topic.foo”, if broker B goes down, messages thereafter sent to A will be dropped. The lifetime of the subscription as far as A knows has been terminated. Lastly,   What are the exact differences between sending a message “persistent” vs “non-persistent” to a topic with non-durable subscribers? According to the JMS spec:How Published Nondurable Subscriber Durable SubscriberNON_PERSISTENT at-most-once (missed if inactive) at-most-oncePERSISTENT once-and-only-once (missed if inactive) once-and-only-onceSo for a non-durable subscriber, a non-persistent message will be delivered “at most once” but will be missed if inactive (or broker failure). For a non-durable subscriber, a persistent message will be delivered “once and only once” but missed if inactive. The “inactive” part of the spec effectively means if there are no durable subscribers to a topic, a message could be lost and there is no guarantee of delivery regardless of whether the message is sent persistent or non-persistent. Reference: Guaranteed messaging for topics, the JMS spec, and ActiveMQ from our JCG partner Christian Posta at the Christian Posta Software blog....

Programming Language Job Trends – 2012-08

It is a little late, but it is time for the summer edition of the job trends for traditional programming languages. The languages in this update have not changed for a while as we are only looking at Java, C++, C#, Objective C, Perl and Visual Basic. Over the next few months, I will be looking at various languages to determine how this list and other job trends posts should change. Also, please review some of the other job trends posts to see if your favorite language is already in one of these posts.First, we look at the job trends from Indeed.com:Most of the job trends have declined in the past few months. Objective-C continues to show solid growth. C# had a significant drop but still leads its C++ cousin. Over the long term trends, Java and C# have very positive growth, while the other languages are tending to stagnate. There is huge growth in mobile development, especially with Objective-C leading the way in iOS development. C++ and Perl show slight declines, but still not too significant. Visual Basic continues its stable trend, showing an increase over the past 2 years but still a decline from 2005.Now, let’s look at SimplyHired’s short term trends:SimplyHired’s trends are show much more decline in recent months than Indeed. Interestingly, Objective-C is not showing much of a positive trend, but it is a much better trend than the other languages in the list. Java is showing a surprising decline over the past few months, but still retains a large lead over the other languages. C++ and C# show almost identical trends over the past year, with a decline in recent months. Visual Basic and Perl show similar declines to the other languages.Finally, here is a review of the relative scaling from Indeed. This provides an interesting trend graph based on job growth:Unsurprisingly, Objective-C has the most growth, but the growth has slowed since our last update. C# growth is solid, hovering around 100% for the past 3 years. Visual Basic and C++ continue to decline. Perl and Java are still showing signs of life, but the growth is not very significant.What does all this mean? First, it is clear the iOS development is hot as is all mobile development. However, mobile development does not seem to be affecting Java or the growth of mobile is offsetting the decline of Java in the enterprise space. Why does Java (and some of the others) show relative growth, but not strong growth in the trend graphs? Basically, we are seeing that while some of the languages are still showing increasing job postings (the relative growth chart), the percentage of postings is less than before. So, other languages not in this list may be increasing in demand quicker than these traditional languages.Reference: Traditional Programming Language Job Trends – August 2012 from our JCG partner Rob Diana at the Regular Geek blog....

Product Manager – Strategic or Not?

Are product managers really involved in strategic discussions, or are we just order takers? Adrienne Tan has poked the beehive and started a great discussion with this article. Joining in from here, hopefully adding folks to the conversation. Check it out, and chime in here or on the brainmates blog. Product Managers Taking Orders Adrienne kicks off the discussion with a great post, including the following question : “why does a whole professional group continue to defend its right to be strategic? No one else seems to think that Product Management is the rightful owner of Product Strategy except Product Management.” As I write this, there are half a dozen great comments on her post, including some powerful ideas:People don’t want to relinquish power – and “owning strategy” is powerful. Of course other people want to say that they “own” it. Product management is the business – we run into problems when the role is “tacked on” organizationally and not deeply integrated. There are two distinct roles – strategic planning and tactical support – and both have the same title (product manager), but if they are different people, you have problems. There’s too much for one person to do, but the responsibilities of people sharing the work must overlap, or they will become disconnected.As Nick points out in the comments – Adrienne’s post is a productive one, not just a rant – since getting a “seat at the table” for strategic decision making is so hard, is it worth doing?Product Strategy Product strategy happens. It may be implicit, but it is probably explicit and intentional. Product strategy, however, is just a business tactic. Your company has a strategy, and someone makes the decision that “product” will play a role in that strategy. The definition of that role constrains what someone else should do with the product, in order to realize the product’s portion of the business strategy. Most of the product management roles that I’ve seen fall into this model. There’s a “glass ceiling” for product managers – who are only given freedom to make decisions within this context. Those product managers are “doers” within these constraints – occasionally allowed to, but generally not encouraged to, and certainly not required to provide recommendations to change the business strategy.Go Where I Tell You If the CEO (the actual CEO, not the “CEO of the product”) is the rider, then the product manager is the horse – constrained to “go where I tell you.” The product manager is watching where he’s running to make sure he steps sure-footedly, looking around to see where the other horses (or wolves) are – basically responsible for the “running.” The CEO is responsible for knowing where to go, not how to get there. As long as the product manager doesn’t get the bit in his teeth, the CEO can make sure the horse goes where she wants.What Does “Strategy” Mean to You? The problem with words like strategy is that they carry a lot of symbolic baggage. Wikipedia tells us that a strategy is a “plan of action, designed to achieve a vision.” The question is one of scope – what level of “vision” are you trying to achieve? Google’s vision is so big that they don’t really articulate it – instead, they share the philosophy that shapes their vision and guides their actions. You have to pick one of the products, like Google Wallet, before you get an articulation of vision. Product management (as a “named entity” in the organization), in the teams that I’ve been a part of, has “owned” the definition of the product strategy that enables the product vision – but not been invited to participate in the business strategy that enables the company’s vision. That strategy is primarily embodied in a product roadmap that articulates how (and when) the product vision will be achieved. The product strategy and vision are components of a portfolio of strategies and visions that collectively make up the business strategy. Command and Control The weakness of this old-school command-and-control model is that the commander (CEO) only gets limited inputs from the commanders in the field (product managers). With this top-down model of decomposition of business strategy into products with specific roles, each product manager has specific responsibilities (take the town), often being expected to succeed with limited context (we’re sweeping around the enemy’s flank, but it is a feint). The product manager is only providing progress reports and possibly market information back to the overall commander. The product manager is not tasked with providing recommendations to change the business strategy. The strength of this approach is that it enables a company to execute their business strategy. As product managers, we know there is always “more to be done” – and like the metaphor of boiling the ocean, if one person tries to do everything, they won’t succeed. In his recent series on product management roles, Rich Mironov calls out that the VP of Product Management is “making sure that the company as a whole is building and shipping and supporting the right products.” Not defining the business strategy, but rather determining which products should be components of that strategy, and what roles those products should have as members of that strategy. Each product manager is responsible for defining the roadmap for their product, articulating how it will fulfill its role in the comprehensive strategy.The Eye of the Beholder Given this definition, is product management still a “strategic” role? It depends on where you’re standing in the organization. The CEO probably sees us as horses. But the teams that are building, testing, and supporting our products are operating on a shorter time horizon, with an even narrower scope. From that point of view, having a multi-release point of view about the product, and a deep understanding of the market (needed to make the right product decisions) is definitely strategic.Should We Be Pushing the Strategic Product Manager Agenda? Getting back to Adrienne’s original point, and a fantasic image from Geoffrey Anderson, “Sometimes I feel like a salmon swimming upstream to spawn, and every tier of the swim is fraught with bears waiting to eat me.” – is it worth investing our time and energy in pushing the “product managers are strategic” message? If any of these apply to you, then yes – push:I’m told what features to build, and I’m expected to just translate for the technical folks on the product team, so that they build it correctly. I’m not given the (business strategy) context that defines the role my product plays, so have no way to know if my product is succeeding. When I provide feedback about the feasibility or effectiveness of my product in its role as part of the business strategy, it is not valued and nothing changes.If none of those apply to you, they do apply to some of your peers – so push. In the past 10 years, I’ve seen each of the above situations multiple times, and they are in order of increasing frequency. What I see even more frequently these days is product managers getting pulled more and more into the operational, product owner role – shepherding teams through daily stand-ups and validating acceptance criteria – purely tactical execution roles. Those product managers are still somehow responsible for doing product management, even when not given enough time to do it. If that’s the position you find yourself in today, start winding the klaxon.Reference: Product Manager – Strategic or Not? from our JCG partner Scott Sehlhorst at the Business Analysis | Product Management | Software Requirements blog....

Android broadcast receiver: Enable and disable during runtime

Broadcast receiver is the one of the basic and important components of the Android application. There are two different ways of adding broadcast receiver in the Android application. It can be added either programmatically or in Android Manifest file. You should be careful while adding broadcast receiver because unnecessary broadcast receivers drain battery power. If you add the broadcast receiver in the Android manifest file, it’s implied that you are going to handle a particular intent in the broadcast receiver and not ignore it. There is a way to enable and disable the broadcast receiver which is added in the manifest file. Example code Application layout file. <LinearLayout xmlns:android='http://schemas.android.com/apk/res/android' xmlns:tools='http://schemas.android.com/tools' android:layout_width='match_parent' android:layout_height='match_parent' android:orientation='vertical'><Button android:layout_width='fill_parent' android:layout_height='wrap_content' android:padding='@dimen/padding_medium' android:text='@string/start_repeating_alarm' android:onClick='startRepeatingAlarm' tools:context='.EnableDisableBroadcastReceiver' /> <Button android:layout_width='fill_parent' android:layout_height='wrap_content' android:padding='@dimen/padding_medium' android:text='@string/cancel_alarm' android:onClick='cancelAlarm' tools:context='.EnableDisableBroadcastReceiver' /> <Button android:layout_width='fill_parent' android:layout_height='wrap_content' android:padding='@dimen/padding_medium' android:text='@string/enable_broadcast_receiver' android:onClick='enableBroadcastReceiver' tools:context='.EnableDisableBroadcastReceiver' /> <Button android:layout_width='fill_parent' android:layout_height='wrap_content' android:padding='@dimen/padding_medium' android:text='@string/disable_broadcast_receiver' android:onClick='disableBroadcastReceiver' tools:context='.EnableDisableBroadcastReceiver' /> </LinearLayout> In the above layout file, we have used some string constants in buttons’ text field. Let’s define these string constants in string.xml as shown below. <resources> <string name='app_name'>EnableDisableBroadcastReceiver</string> <string name='enable_broadcast_receiver'>Enable Broadcast Receiver</string> <string name='disable_broadcast_receiver'>Disable Broadcast Receiver</string> <string name='start_repeating_alarm'>Start Repeating Alarm</string> <string name='cancel_alarm'>Cancel Alarm</string> <string name='menu_settings'>Settings</string> <string name='title_activity_enable_disable_boradcast_receiver'>EnableDisableBoradcastReceiver</string> </resources>Broadcast receiver We are going to use AlarmManager to set the repeating alarm which eventually sends the intent at the specific time interval. Read this post to know more about the AlarmManager.Now create AlarmManagerBroadcastReceiver class to extend the BroadcastReceiver class. The content of the class is given below. package com.code4reference.enabledisablebroadcastreceiver;import java.text.Format; import java.text.SimpleDateFormat; import java.util.Date;import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.os.PowerManager; import android.widget.Toast;public class AlarmManagerBroadcastReceiver extends BroadcastReceiver {final public static String ONE_TIME = 'onetime'; @Override public void onReceive(Context context, Intent intent) {//You can do the processing here update the widget/remote views. StringBuilder msgStr = new StringBuilder(); //Format time. Format formatter = new SimpleDateFormat('hh:mm:ss a'); msgStr.append(formatter.format(new Date()));Toast.makeText(context, msgStr, Toast.LENGTH_SHORT).show(); } }Enable/Disable Broadcast receiver Now we will define main activity which uses alarmManager to set a repeating alarm. The repeating alarm will broadcast intent after every 3 seconds. This alarm has been set in the setRepeatingAlarm() method below. package com.code4reference.enabledisablebroadcastreceiver;import com.example.enabledisablebroadcastreceiver.R;import android.app.Activity; import android.app.AlarmManager; import android.app.PendingIntent; import android.content.ComponentName; import android.content.Context; import android.content.Intent; import android.content.pm.PackageManager; import android.os.Bundle; import android.view.View; import android.widget.Toast;public class EnableDisableBroadcastReceiver extends Activity {@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } /** * This method gets called when 'Start Repeating Alarm' button is pressed. * It sets the repeating alarm whose periodicity is 3 seconds. * @param view */ public void startRepeatingAlarm(View view) { AlarmManager am=(AlarmManager)this.getSystemService(Context.ALARM_SERVICE); Intent intent = new Intent(this, AlarmManagerBroadcastReceiver.class); PendingIntent pi = PendingIntent.getBroadcast(this, 0, intent, 0); //After after 2 seconds am.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), 1000 * 4 , pi); Toast.makeText(this, 'Started Repeating Alarm', Toast.LENGTH_SHORT).show(); } /** * This method gets called when 'cancel Alarm' button is pressed. * This method cancels the previously set repeating alarm. * @param view */ public void cancelAlarm(View view) { Intent intent = new Intent(this, AlarmManagerBroadcastReceiver.class); PendingIntent sender = PendingIntent.getBroadcast(this, 0, intent, 0); AlarmManager alarmManager = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE); alarmManager.cancel(sender); Toast.makeText(this, 'Cancelled alarm', Toast.LENGTH_SHORT).show(); } /** * This method enables the Broadcast receiver registered in the AndroidManifest file. * @param view */ public void enableBroadcastReceiver(View view){ ComponentName receiver = new ComponentName(this, AlarmManagerBroadcastReceiver.class); PackageManager pm = this.getPackageManager();pm.setComponentEnabledSetting(receiver, PackageManager.COMPONENT_ENABLED_STATE_ENABLED, PackageManager.DONT_KILL_APP); Toast.makeText(this, 'Enabled broadcast receiver', Toast.LENGTH_SHORT).show(); } /** * This method disables the Broadcast receiver registered in the AndroidManifest file. * @param view */ public void disableBroadcastReceiver(View view){ ComponentName receiver = new ComponentName(this, AlarmManagerBroadcastReceiver.class); PackageManager pm = this.getPackageManager();pm.setComponentEnabledSetting(receiver, PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP); Toast.makeText(this, 'Disabled broadcst receiver', Toast.LENGTH_SHORT).show(); } } We add the AlarmManagerBroadcastReceiver in the manifest file. This registers the Broadcast Receiver. <manifest xmlns:android='http://schemas.android.com/apk/res/android' package='com.example.enabledisablebroadcastreceiver' android:versionCode='1' android:versionName='1.0' ><uses-sdk android:minSdkVersion='15' android:targetSdkVersion='15' /> <application android:icon='@drawable/ic_launcher' android:label='@string/app_name' android:theme='@style/AppTheme' > <activity android:name='com.code4reference.enabledisablebroadcastreceiver.EnableDisableBroadcastReceiver' android:label='@string/title_activity_enable_disable_boradcast_receiver' > <intent-filter> <action android:name='android.intent.action.MAIN' /> <category android:name='android.intent.category.LAUNCHER' /> </intent-filter> </activity> <!-- Broadcast receiver --> <receiver android:name='com.code4reference.enabledisablebroadcastreceiver.AlarmManagerBroadcastReceiver' /> </application> </manifest> Once done, execute the code and you will notice the application as shown below. You can get the complete source at github/Code4Reference. You can find more Android tutorials here. Reference: Enable and disable Broadcast receiver during runtime from our JCG partner Rakesh Cusat at the Code4Reference blog....

Building ScalaFX 1.0 with Gradle 1.1

After becoming a little disenchanted with the SBT for Scala, I wanted an alternative that was more logical, simpler to understand and had a better user experience. After all, the whole point of a domain specific language is to make the writing of the script, formulae or grammar to be affordable to the users. A DSL must be comprehensible to the users, it must be relatively easy to write the script in the language of the domain, and surely must be mostly free of annoyances. The great examples of course are spreadsheets like Excel, the XML Stylesheet Language for Transformations (XSLT) and shell scripts (like DOS, BASH). I recently added a build.grade file to the ScalaFX project. Here is a screen cast about how to use Gradle build instead of the current SBT file. Building ScalaFX 1.0 with Gradle 1.1 from Peter Pilgrim on Vimeo. The only sore point so far, I have found with Gradle, is that the project takes it name from the containing folder. In other words, I found that force setting the artifactId does not work. group = 'org.scalafx' artifactId = 'ScalaFX-javaone-2012' // This does not work version = '1.0-SNAPSHOT' That might be worth considering when moving project folders around in order to make a quick research branch for a delta, or look at some other committer’s changes separately from your own. Because Gradle is written in Groovy, you have the full power of that dynamic language to play with. I was able to write a groovy task to push a UNIX bash launcher script in less than ten minutes. I was also able to run a launcher within Gradle for the Colorful Circles demo app. The Gradle documentation is a lot better than SBT, in my humble opinion. In SBT, if you missed adding a single blank line between statement declarations, or you forgot to add an extra delimiter between to Seq() or perhaps used the wrong method name “+” versus “++” then you could be lost for quite a long time. It would good to see how the Scala Plugin for Gradle could work with recently announced Zinc and Incremental Compilation from Typesafe. After reading that blog post, I think Zinc, Gradle and Scala plug-in should just work. Has anyone tried this combination yet? I have not yet. PS: Under Windows, you need to comment out the “chmod” line in the build.gradle for now. I will fix this later on before the 1.0 release. PS PS: With the Vimeo, you might prefer to click on the HD option to see improved clarity. Reference: Building ScalaFX 1.0 with Gradle 1.1 from our JCG partner Peter Pilgrim at the Peter Pilgrim’s blog blog....

Spring Scoped Proxy

Consider two Spring beans defined this way: @Component class SingletonScopedBean{ @Autowired private PrototypeScopedBean prototypeScopedBean; public String getState(){ return this.prototypeScopedBean.getState(); } }@Component @Scope(value="prototype") class PrototypeScopedBean{ private final String state; public PrototypeScopedBean(){ this.state = UUID.randomUUID().toString(); }public String getState() { return state; } } Here a prototype scoped bean is injected into a Singleton scoped bean. Now, consider this test using these beans: @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration public class ScopedProxyTest { @Autowired private SingletonScopedBean singletonScopedBean; @Test public void testScopedProxy() { assertThat(singletonScopedBean.getState(), not(equalTo(singletonScopedBean.getState()))); } @Configuration @ComponentScan("org.bk.samples.scopedproxy") public static class SpringContext{}} The point to note is that there is only 1 instance of PrototypeScopedBean that is created here – and that 1 instance is injected into the SingletonScopedBean, so the above test which actually expects a new instance of PrototypeScopedBean with each invocation of getState() method will fail. If a new instance is desired with every request to PrototypeScopedBean (and in general if a bean with longer scope has a bean with shorter scope as a dependency, and the shorter scope needs to be respected), then there are a few solutions: 1. Lookup method injection – which can be read about here 2. A better solution is using Scoped proxies – A scoped proxy can be specified this way using @Configuration: @Component @Scope(value="prototype", proxyMode=ScopedProxyMode.TARGET_CLASS) class PrototypeScopedBean{ private final String state; public PrototypeScopedBean(){ this.state = UUID.randomUUID().toString(); }public String getState() { return state; }} With this change, the bean injected into the SingletonScopedBean is not the PrototypeScopedBean itself, but a proxy to the bean (created using CGLIB or Dynamic proxies) and this proxy understands the scope and returns instances based on the requirements of the scope, the test should now work as expected. Reference: Spring Scoped Proxy from our JCG partner Biju Kunjummen at the all and sundry blog....

Integrating JavaFX and Swing

I’ve just finished rewriting a component of my app that was using Swing and now is using JavaFX, I’ve ended up with a JavaFX component that integrates with the larger swing app. It is a large app and the rewrite took me a while, in the end everything worked fine and I’m glad I did it. Reasons you might want to do this in your swing app You might want to rewrite your Swing app and change it to use JavaFX instead, the easiest way is to do this incrementally by changing each component at a time. This requires that you integrate each of the newly changed JavaFX components with the rest of your Swing app. I’ll summarize why you might want to start rewriting your app from Swing to JavaFX:It’s the future Swing is pretty much dead in the sense that it won’t get any further developments. JavaFX is the new UI toolkit for Java, it is better prepared for the future with things like touch support, 3D, built-in animation support, video and audio playback, etc.Probable future support for mobile: Android, IOS… From what I’ve been seeing, I think it’s pretty much a guarantee that Android, IOS, etc support will be made available, Oracle already has working prototypes of this that they show on public conferences, the only question is when. I think it won’t take that long, probably we’ll see more on this in the next JavaOne, coming up soon.It’s solid JavaFX is a well-designed toolkit with a rapid growing pace, a bright future and a set of good free UI tools. Furthermore unlike in the past, Oracle is giving developers feedback a great importance changing and adapting its APIs to meet their goals.It’s pretty Unlike Swing, not counting third party librarys, which was ugly by itself, JavaFX looks good right from the start. Given that users nowadays expect good looking well designed apps this is a pretty good point.Nice extras Some nice extras, like the charts API, an embedded browser that supports HTML5, etc.How you do it Back on JavaFX 1.3 you could embed Swing in JavaFX but not the other way around, at least not officially. I implemented a Swing component that allowed you to embed JavaFX content in Swing (called JXScene) and made it publicly available in the jfxtras project. It was the only way you could embed a JavaFX scene in a Swing app. Now Oracle with JavaFX 2.X made an official way of embedding JavaFX in Swing which makes more sense but unfortunately not a way to embed Swing in JavaFX, however I guess this will suffice in most cases.Arquitecture Essentially when you are embedding JavaFX in Swing you end up with 2 running UI threads: the Swing EDT thread and the JavaFX User thread. There is a chance that in the future there will only be one thread for both as is the case with SWT, making Swing run on the JavaFX User Thread, but for now we’ll have to manage our way with 2 threads. Two threads running at the same time in the UI is what complicates matters, and makes JavaFX integration not as easy as you might expect, unless you’re doing some trivial small app but I guess that is not the scenario for most of the real world use cases. If you’re doing a small app might as well do it all in JavaFX. Coding JavaFX gives you JFXPanel, which is a Swing panel that hosts a JavaFX scene. You set the scene on the JFXPanel and add the panel wherever you could add a Swing Component. To access JavaFX data you have to wrap your code in a Runnable object and call the Platform.runLater method: jbutton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { Platform.runLater(new Runnable() { @Override public void run() { fxlabel.setText("Swing button clicked!"); } }); } }); On the other side is Swing data. This data must be accessed only by the EDT. To ensure that your code is running on the EDT, wrap it into a Runnable object and call the SwingUtilities.invokeLater: SwingUtilities.invokeLater(new Runnable() { @Override public void run() { //Code to change Swing data. } });TipsJavaFX already throws exceptions when you access a JavaFX resource outside the JavaFX User Thread, but bear in mind that this does not happen always. To minimize performance costs not all situations are checked. If you use Substance third party library than an exception will also be thrown whenever a Swing resource is accessed outside the EDT. Setting Substance as your Swing look and feel might be a good solution to lessen concurrency mistakes on the swing side that you might do. Be very careful when sharing resources between the 2 UI threads, try to avoid this as much as possible. Best way to solve multi-threading problems is to avoid them, and these kind of problems are among the most difficult to solve in Software Engineering. There is a reason why Swing started off as a multi-threaded toolkit and ended changing to a single threaded one. Sometimes you might want to check if you are on the JavaFX User Thread via Platform.isFxApplicationThread() and only than issue a call to Platform.runLater(…), because if you are on the JavaFX User Thread and call runLater(...) the execution of the code that is inside will still be deferred to a later time and this might not be what you want.Other links to check out:Oracle tutorial: http://docs.oracle.com/javafx/2/swing/jfxpub-swing.htmReference: Integrating JavaFX and Swing from our JCG partner Pedro Duque Vieira at the Pixel Duke blog....

Android Jelly Bean notification tutorial

You may have heard about Android Jelly Bean (API level 16). Google has improved a lot of features and introduced new features. One of them is the notification. Now they have made the notification more versatile by introducing media rich notification. Google has come up with three special style of notification which are mentioned below. Even developer can write his own customized notification style using remote view.The old Notification class constructor has been deprecated and a brand new and enhanced version of Notification has been introduced. Notification Type:Basic Notification – Shows simple and short notification with icon. Big Picture Notification – Shows visual content such as bitmap. Big Text Notification – Shows multiline Textview object. Inbox Style Notification – Shows any kind of list, e.g messages, headline etc.Old syntax requires us to create an object of notification but now Android uses builder patter to create the notification object. Notification.Builder class has been introduced to make this task easier. This class returns the builder object which is configurable according to your requirements. The helper classes have been introduced like Notification.BigPictureStyle, Notification.BigTextStyle, and Notification.InboxStyle. These classes are re-builder classes which take object created by Notification.Builder class and modify the behavior like so. Project 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. First 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 Name. Don’t forget to select the Build SDK version (for this tutorial Google API 16 has been selected). 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 for the dialog box shown below and hit the finish button.This process will setup the basic project files. Now we are going to add four buttons in the activity_main.xml file. You can modify the layout file using either Graphical Layout editor or xml editor. The content of the file should look like this. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:gravity="center_horizontal"><Button android:id="@+id/btBasicNotification" android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="center_horizontal|center_vertical" android:onClick="sendBasicNotification" android:text="@string/btBasicNotification" android:background="@drawable/button_background" android:textColor="#000000" /> <Button android:id="@+id/btBigTextNotification" android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="center_horizontal|center_vertical" android:onClick="sendBigTextStyleNotification" android:text="@string/btBigTextNotification" android:background="@drawable/button_background" android:textColor="#000000" /> <Button android:id="@+id/btBigPictureNotification" android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="center_horizontal|center_vertical" android:onClick="sendBigPictureStyleNotification" android:text="@string/btBigPictureNotification" android:background="@drawable/button_background" android:textColor="#000000" /> <Button android:id="@+id/btInboxStyleNotification" android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="center_horizontal|center_vertical" android:onClick="sendInboxStyleNotification" android:text="@string/btInboxStyleNotification" android:background="@drawable/button_background" android:textColor="#000000"/> </LinearLayout> You may have noticed that onClick methods are associated with respective buttons. If you don’t know how to define and use the background file for view then ignore the android:background field. Now we are going to define the methods sendBasicNotification, sendBigTextStyleNotification, sendBigPictureStyleNotification and sendInboxStyleNotification. As the method name suggests, it sends that particular kind of notification. In each method we are creating Notification.Builder object, and customizing the object. Here builder pattern has been used to customize the object. Once the customization is done, call build() method to get the notification object. In this new notification system, at most three actions can be associated to a notification, which are displayed below the notification content. This can be achieved by calling addAction() method on the builder object. The same number of icons you will see on the notification as you will notice for sendBigPictureStyleNotifcation() method. Notification priority can also be set by calling setPriority() method as shown in sendBigTextStyleNotification() method. In the code given below, intent has been used to invoke the HandleNotificationActivity. package com.example.jellybeannotificationexample;import android.app.Activity; import android.app.Notification; import android.app.Notification.Builder; import android.app.NotificationManager; import android.app.PendingIntent; import android.content.Intent; import android.graphics.BitmapFactory; import android.os.Bundle; import android.view.Menu; import android.view.View;public class NotificationMainActivity extends Activity {@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); }@Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.activity_main, menu); return true; }public void sendBasicNotification(View view) { Notification notification = new Notification.Builder(this) .setContentTitle("Basic Notification") .setContentText("Basic Notification, used earlier") .setSmallIcon(R.drawable.ic_launcher_share).build(); notification.flags |= Notification.FLAG_AUTO_CANCEL; NotificationManager notificationManager = getNotificationManager(); notificationManager.notify(0, notification); }public void sendBigTextStyleNotification(View view) { String msgText = "Jeally Bean Notification example!! " + "where you will see three different kind of notification. " + "you can even put the very long string here.";NotificationManager notificationManager = getNotificationManager(); PendingIntent pi = getPendingIntent(); Builder builder = new Notification.Builder(this); builder.setContentTitle("Big text Notofication") .setContentText("Big text Notification") .setSmallIcon(R.drawable.ic_launcher) .setAutoCancel(true); .setPriority(Notification.PRIORITY_HIGH) .addAction(R.drawable.ic_launcher_web, "show activity", pi); Notification notification = new Notification.BigTextStyle(builder) .bigText(msgText).build(); notificationManager.notify(0, notification); }public void sendBigPictureStyleNotification(View view) { PendingIntent pi = getPendingIntent(); Builder builder = new Notification.Builder(this); builder.setContentTitle("BP notification") // Notification title .setContentText("BigPicutre notification") // you can put subject line. .setSmallIcon(R.drawable.ic_launcher) // Set your notification icon here. .addAction(R.drawable.ic_launcher_web, "show activity", pi) .addAction( R.drawable.ic_launcher_share, "Share", PendingIntent.getActivity(getApplicationContext(), 0, getIntent(), 0, null));// Now create the Big picture notification. Notification notification = new Notification.BigPictureStyle(builder) .bigPicture( BitmapFactory.decodeResource(getResources(), R.drawable.big_picture)).build(); // Put the auto cancel notification flag notification.flags |= Notification.FLAG_AUTO_CANCEL; NotificationManager notificationManager = getNotificationManager(); notificationManager.notify(0, notification); }public void sendInboxStyleNotification(View view) { PendingIntent pi = getPendingIntent(); Builder builder = new Notification.Builder(this) .setContentTitle("IS Notification") .setContentText("Inbox Style notification!!") .setSmallIcon(R.drawable.ic_launcher) .addAction(R.drawable.ic_launcher_web, "show activity", pi);Notification notification = new Notification.InboxStyle(builder) .addLine("First message").addLine("Second message") .addLine("Thrid message").addLine("Fourth Message") .setSummaryText("+2 more").build(); // Put the auto cancel notification flag notification.flags |= Notification.FLAG_AUTO_CANCEL; NotificationManager notificationManager = getNotificationManager(); notificationManager.notify(0, notification); }public PendingIntent getPendingIntent() { return PendingIntent.getActivity(this, 0, new Intent(this, HandleNotificationActivity.class), 0); }public NotificationManager getNotificationManager() { return (NotificationManager) getSystemService(NOTIFICATION_SERVICE); } } We have defined basic HandleNotificationActivity which just shows a simple message when intent is fired for this activity. The content of the file is as following. package com.example.jellybeannotificationexample;import android.app.Activity; import android.os.Bundle;public class HandleNotificationActivity extends Activity {@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.handle_notification_activity); } } The corresponding layout file(handle_notification_activity.xml) is given below <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:gravity="center_horizontal|center_vertical" ><TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/tvHandleNotification" android:textSize="20dp" android:textStyle="bold|italic" /></LinearLayout> Now you have to define the Android manifiest file. HandleNotificationActivity should be included in the manifest file and then put the intent filter for this activity. <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.jellybeannotificationexample" 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"> <activity android:name=".NotificationMainActivity" android:label="@string/title_activity_main" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name=".HandleNotificationActivity" android:label="@string/title_activity_main" > <intent-filter> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application></manifest> Once you are done with the coding, just execute it. You will see the application as shown in the picture below. On clicking the button you will see the corresponding notification on the upper part of the screen. If you drag the notification down then you can see the entire message and corresponding icon. The pictures below are the notification when they were dragged down.Big Text StyleApplicationInbox StyleBig Picture StyleBasic notificationIf you are interested to know more about source code, you can find it over here. Reference: Tutorial on new Android Jelly Bean notification from our JCG partner Rakesh Cusat at the Code4Reference blog....
Java Code Geeks and all content copyright © 2010-2014, 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 two of our best selling eBooks for FREE!

Get ready to Rock!
You can download the complementary eBooks using the links below: