Brought to you by…


Wassup party people???!!! It’s early morning and you’re here because you like to waste time in the morning instead of focusing on those twenty bug reports that are filling your email inbox. (Who reads those things anywayz? I mean, really! don’t we already know which parts are broken way before the bug report comes in?) I thought I’d give you a little something something to start your day with… a little something Groovy. You see on this site you never know what neat tip or off the hook idea I’ll be putting up. It could be something Mac related, it could be a Linux hack. It could be something you can run on your Blackberry (haven’t gotten that far yet but with all the current JavaME stuff it’s bound to happen.) Or it could be just a general “this is another way IntelliJ Idea can save you 100 man hours…” (For real though, if you ain’t using IntelliJ Idea download a copy. I don’t work for JetBrains, they don’t pay me a dime. I just love their product that much.)

That’s what connections is all about…
(Time in, time out, whisper to a shout…)
If you follow my site long enough you’ll notice subtle references to some of my favorite songs and artists sprinkled here and there. you may not get some of the quotes (unless you grew up in the hood) but they’re all relevant in one way or another. Back to the point. Here’s what I got for you this morning. S’pose you wanna talk Java between servers. Let’s pretend you got a list of artists and songs from an MP3 collection on one machine and you wanna zap it to another machine that does something nice to the list. You could install Tomcat and write a Java client that uses URL’s and posts stuff but that sucks cuz Tomcat is fat (not phat) and complicated and The URL object doesn’t give you what you want. You could do better and install Jetty on the server (quicker/easier) and use Commons HTTP client but that’s still not quick and dirty enough. You could whip out into the java.net package and just use Sockets. Now we’re getting somewhere… but eww sockets? Well with Groovy any Java package goes from yuk to wow. Just a handful of code you could slam whatever you want from your client to your server without breaking a sweat. Check it out (*note you wanna use Groovy-1.1 for this example otherwise use the withStreams and wrap its params with ObjectStreams):

Client

new Socket("localhost", 9876).withObjectStreams { input, output ->
	def myFavoriteArtistsSongs = [BustaRhymes:"Bladow!!", MOP:"Ante Up", BigPun:"I don't wanna be a playa"]
	output << myFavoriteArtistsSongs
	println input.readObject()
}

Server

def serverPort = 9876
def serverSocket = new ServerSocket(serverPort)
println "Server is locked and ready to get its serve on..."

def doSomethingWithStuff(def stuff) {
	println "I got some stuff, check it out: "
	println stuff.toString()
	return "That stuff is real"
}

while(true) {
	serverSocket.accept() { connection ->
		connection.withObjectStreams { input, output ->
			def stuff = input.readObject()
			output << doSomethingWithStuff(stuff)
		}
	}
}

Not too shabby for zipping stuff back and forth between boxes, eh? Look for tips like this and more in the upcoming weeks as I prepare to spread the word of Groovy nation-wide. Rory has this nifty idea (that I haven ‘t read entirely) about representing in-coming links. It’s sorta like a brought to you by… thing. so I’m gonna shamelessly bite his idea (bite.equals(“copy-cat”);) for the time being. Honestly I believe I was the originator of the concept when I started babbling about Java Interfaces. So in summary this message has been brought to you by:
Rory – because the world needs another Spider Fighting Action hero
Juxy – The best way to spit shine you XSLT stylesheets
Merlyn – Cuz you all need to get your ErLang on!
JetBrains – Because if you’re still using Eclipse to write Java you just don’t know…

One thought on “Brought to you by…

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s