Thursday, June 2, 2011

Finally...Google Plus One is here!

The Google "Plus One" button seems to be a good contender to the Facebook "Like" Personally, I adore the "Like" button, but I would want to see how the "Plus One" competes. I just managed to add the Google +1 button to my blog posts.
It is pretty straight forward.

1) Log into your Account.
2) Go to Dashboard --> Design --> Edit HTML.
3) Check the "Expand Widget Template" Checkbox
4) Paste the below code just under the <data:post.body/> tag

<!-- Google +1 button Start -->
        <p></p>
        <div style='float:left;padding:10px;'>
               <script type="text/javascript" src="http://apis.google.com/js/plusone.js"></script>
               <g:plusone expr:href="data:post.url" size="standard" count="true"></g:plusone>
        </div>
<!-- Google +1 button End -->


And Voila ~~*~ You are done!
You can control the size of the button by the "size" attribute, which accepts values of small, medium, standard and tall

This snippet positions the +1 Button after each posts. If you prefer it on the top of each post, place the above snippet before <data:post.body/> instead of after it.

You can also change the value of "float" attribute to right or left to position it horizontally.


~~~ Have Fun ~~~

Thursday, June 19, 2008

Webservices on Weblogic 10.0

I recently started working on Webservices. All these years, I was always scared of this stuff. But I was quite wrong. Webservicies are a good way to communicate between multiple applications. It is possible to communicate between multiple platform applications also. (For eg, Java and .Net)
So I ventured around the area of Webservices and Weblogic was my first choice. And I realized that it was not so tough. So my next topic would be on Webservices on Weblogic 10.0

Stay tuned

Monday, May 26, 2008

package jms.queue;
import java.util.Hashtable;
import javax.jms.*;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

public class MyListener implements MessageListener{
// Define the connection for the provider private static String ProviderUrl = "t3://localhost:7001";
// Defines the JNDI context factory. public static String JNDI_FACTORY="weblogic.jndi.WLInitialContextFactory";
// Defines the JMS connection factory for the queue. public static String JMS_FACTORY="jms/MyConnectionFactory";
// Defines the queue. public static String QUEUE="jms/MyQueue";
private QueueConnectionFactory queueConnectionFactory;
private QueueConnection queueConnection;
private QueueSession queueSession;
private QueueReceiver queueReceiver;
private Queue queue; private boolean quit = false;
/** * On Message() gets triggered on receipt of a message */

public void onMessage(Message msg) {
try {
String msgText;
if (msg instanceof TextMessage) {
msgText = ((TextMessage)msg).getText();
} else {
msgText = msg.toString();
}
System.out.println("Message Received... "+ msgText );
if (msgText.equalsIgnoreCase("quit")) {
synchronized(this) {
quit = true; // Notify main thread to quit this.notifyAll();
}
}
} catch (JMSException jmse) { jmse.printStackTrace(); } }
/** * Initializes the context for setting up JMS... * */
public void init(Context ctx, String queueName) throws NamingException, JMSException {
System.out.println( "Looking up connection factory:" + JMS_FACTORY );
queueConnectionFactory = (QueueConnectionFactory) ctx.lookup(JMS_FACTORY);
queueConnection = queueConnectionFactory.createQueueConnection();
queueSession = queueConnection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
System.out.println( "Listening to Queue..." + queueName ); queue = (Queue) ctx.lookup(queueName);
queueReceiver = queueSession.createReceiver(queue);
queueReceiver.setMessageListener(this);
queueConnection.start();
}
/** * Closes JMS objects gracefully * */
public void close()throws JMSException {
queueReceiver.close();
queueSession.close();
queueConnection.close();
}
/** * public static void main() (Entry point). * */
public static void main(String[] args) throws Exception {
InitialContext ic = getInitialContext(ProviderUrl);
MyListener myListener = new MyListener();
myListener.init(ic, QUEUE);
System.out.println("JMS MyListener Module ready to listen for messages. Module will exit on getting a \"quit\" message....");
// Wait until a "quit" message has been received.

synchronized(myListener) {
while (! myListener.quit) {
try {
myListener.wait();
} catch (InterruptedException ie) {}
}
}
myListener.close(); }
/* * gets the initial context object * */
private static InitialContext getInitialContext(String url) throws NamingException {
Hashtable env = new Hashtable();
env.put(Context.INITIAL_CONTEXT_FACTORY, JNDI_FACTORY);
env.put(Context.PROVIDER_URL, url);
return new InitialContext(env);
}
}

Wednesday, May 21, 2008

Java Messaging with Weblogic - Part 3

Now that we have configured a JMS Server and set up a JMS Module along with a Connection Factory and Queue, all you need is to write a client sender application that will send messages to this queue, and a client receiver application that will read messages from this queue.

Assuming the below JNDI configurations were set up in the previous step, we go ahead to the sender and receiver modules
1) JMS_FACTORY = "jms/MyConnectionFactory";
2) QUEUE = "jms/MyQueue";

Sample Sender Source code

// import statements
package jms;
import java.io.IOException;

import java.util.Date;
import java.util.Hashtable;
import javax.jms.JMSException;

import javax.jms.Queue;
import javax.jms.QueueConnection;
import javax.jms.QueueConnectionFactory;
import javax.jms.QueueSender;
import javax.jms.QueueSession;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
// MySender Class
public class MySender {

// context parameters
public static String JNDI_FACTORY = "weblogic.jndi.WLInitialContextFactory";
public String JMS_FACTORY = "weblogic.examples.jms.QueueConnectionFactory";
public String QUEUE = "weblogic.examples.jms.exampleQueue";

// declaration of Main()
public static void main(String args[]) throws JMSException {
Context ctx;

try {
// Get the initial Context
ctx = getInitialContext("t3://localhost:7001");
QueueConnectionFactory qconFactory;

QueueConnection connection;
QueueSession session;
QueueSender sender;
Queue queue;
TextMessage msg;
// JNDI Lookup on the created Connection Factory
qconFactory = (QueueConnectionFactory) ctx.lookup

("jms/MyConnectionFactory");
connection = qconFactory.createQueueConnection();
session = connection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
// JNDI Lookup on the created Queue
queue = (Queue) ctx.lookup("jms/MyQueue");
msg = session.createTextMessage();
sender = session.createSender(queue);
// Send 5 sample messages
for (int i = 0; i <>
msg.setText("Message Id-" +i);
System.out.println("Sending Message" + msg.getText());
connection.start();
sender.send(msg);
}
// close the session and the connection
session.close();
connection.close();
}
// exception handling
catch (NamingException e) {
e.printStackTrace();
}
}
// function to return the initial context
private static InitialContext getInitialContext(String url) throws NamingException {

Hashtable env = new Hashtable();
env.put (Context.INITIAL_CONTEXT_FACTORY, JNDI_FACTORY);
env.put(Context.PROVIDER_URL, url);
return new InitialContext(env);
}
}

The next step will be to write a Sample Receiver to actually read the messages from the queue...

Java Messaging with Weblogic - Part 2

Now that we know the silly basics of Java Messaging, let us get our hands dirty by writing a little example.

Step 1 - If you dont already have Weblogic installed, you need to download and install the Weblogic Server from the BEA website (Try http://commerce.bea.com/showallversions.jsp?family=WLS ). I recommend the latest 10.x version. The setup file is huge, so be aware of the network traffic congestion and download charges (if applicable to you)

BEA provides a cool IDE for devloping Enterprise Java applications. It is called Workshop Platform. It is Eclipse based and you would find it easy to work wth. So, once you install the application, (assuming you have a Windows OS) you will be able to locate the command to start the Weblogic Server in Start --> Programs --> BEA Products --> Weblogic Server

It will open up a command prompt window and show you the scripts for loading the server. Once the server is up, you will be able to see the status on the console (Server Status changed to RUNNING) You can verify it by browsing to http://localhost:7001/console/ (You may need to enter the credentials that you supplied while installing Weblogic)



Step 2 - Now get to understand some more basics of JMS
In Webogic server, you would need to create a new JMS server. Then you need to create a JMS Connection Factory and JMS Queues so that your application will be able to publish messages into the Queue.

Dont you mind the technical Jargon mentioned above, take it off your head, follow the procedures and you will slowly uderstand what each term means.

So, let us go back to the Admin Console. Go to Home --> Services (Under Domain Configurations) -> JMS Servers (Under Messaging)

Next click on "Lock and Edit" from the Change Center, so that it allows you to edit the configuration.

Click on "NEW" to Create a new JMS Server and Enter the Server name here and click Next to select a store type. (Create a File Store)

Then select the target Weblogic server instance where you need to deploy this JMS Server. You can use the default server instance. Click Finish.

You have created a JMS Server.

Now you need to Create JMS Modules
Select Home --> Services --> Messaging --> JMS Modules.
Enter the required name and create a new JMS System module and select a target server instance for this module

Now you need to add resources (queue, connection factory etc) to this JMS Module
Click on the newly created JMS Module. In the next screen, select "Connection Factory" from the list of resources specified and click of Next and Enter the details for the Connection Factory. Enter Name and JNDI Name (this is required for connecting through the application later)
Next select a target server instance and then Finish

In the same way add a new destination Queue / Topic to the created JMS system Module resource. Enter the Queue name and JNDI name (This also will be required later for the application to connect) In the next step, select the JMS Server that you created earlier.


So we have seen how to create a JMS Server and a JMS Module within the server. In the
module we created a JMS connection Factory and a resource or a JMS queue. The
newly created resources can be seen in the resource summary.

Now next we will see how to connect to this resource through a Java Application :-)

Java Messaging with Weblogic - Part 1

I learnt JMS a few months ago when we had to develop a messaging application for a prospective client. The requirement was to enable communication between an application built on .NET with another application (to-be) built on enterprise Java. We could have used plain old web-services, but we decided to go ahead with a SOA (service oriented architecture) so as to build a scalable, reusable and robust system.

This tutorial is spread across in parts so as not to bore my tiny audience. And btw, I am not sure how much will this activity of mine be welcomed. Anyways, the only intent of KT (Knowledge Transfer) through blogging is to impart knowledge.

By no chance is it meant to boast around!
So here we go!

What is JMS?
************

JMS stands for Java Messaging Service and is a set of APIs that facilitate communication between a java virtual environment (JVM) and a messaging broker (also known as JMS Provider) There are a few many JMS providers in market - Websphere MQ, Weblogic JMS, Active MQ, Open JMS etc etc.

I would recommend Weblogic JMS over others due to its simplicity and ease of use. But mind you, its not free for production!

So the JMS API exposes interfaces through which you can send or recieve a text message. Applications can subscribe to a JMS provider and can publish / post messages to the provider. Other applications that are also subscribed to the provider can receive these messages.
Now, there are two types of Messaging1) Point to Point (P2P)2) Publish - Subscribe (PS)
The first one indicates one to one communication between two applications, while the second one is like a bulletin board where multiple applications can subscribe and the messages will be published to all the subscribers.

So that is about the basics and to set the ball rolling! Next we will have some more details.Stopping for the day so that you wont be overloaded!
Stay Tuned