Deploy

Integrate your Eclipse Juno IDE with Openshift

Hi,

In this article we will discuss how can we integrate our Eclipse IDE to OpenShift and create new applications. In previous articles we discussed about how we can connect to OpenShift and create application through command line and management console ? But of-course it would be really great if we can create, build, test, modify and publish our applications directly from the IDE itself.

I am going to use Eclipse Juno, which is the latest eclipse IDE and will create a sample JBossAS-7 application

Download Eclipse from here.

Download Eclipse Juno depending on your Operating System. I chose Windows-64 bit version. If you are already running an older version of Eclipse, you can always upgrade it or try to follow the same steps with little tweaks 🙂

Step-1: Start Eclipse and navigate to Help tab. Here select Eclipse Marketplace and search for JBoss Tools, click on install.

Select the OpenShift tools as shown in image :

Once done, click next and accept the License Agreements and then restart Eclipse. It will come up with a welcome page like below :

Step-2 : Lets create an application now. On the welcome page itself click on OpenShift Application. It will ask you to sign-in with your OpenShift account. Saving the password is completely your choice.

Now give a name to your application and select what type of application you want to create from the available options shown in below image. Also select the embedded cartridges, if you want to use any.

I am not using any embedded cartridge for now, now click next.

Here we get an option, whether we want to create a new project or would like to use an existing project. Also it is good to check the option for creating a server, which will help us to easily test and publish our application. Click next.

Now we need to setup clone destination, basically the application created on OpenShift Server will be cloned to this directory. Make sure that directory exists, before this setup 🙂

Step-3 : Now it is time to configure SSH keys, if you don’t.. you would end up with below.

Click on SSH2 Preferences to configure your keys. Navigate to Key Management and click on Generate RSA Key.

Let’s save the private key and click Ok.

Step-4 : Now you must add this public key to your OpenShift account. For this

login to your OpenShift account. Navigate to My Account page and add your Public Key there.

Let’s carry forward the Step-3 and click on finish. This will prompt you to enter a passphrase for your private key.

Click Ok and you will get a local copy of the application on your machine. You may see an error mark against your application in Package Explorer view, this can be solved by updating you maven configuration. Check below :

Step-5 : Now lets make some changes in to the application. I made a change in “index.html”. once you save this, you might get a popup stating that there has been a change in one of your file, would you like to publish it ? you can click on yes and the change would be pushed to OpenShift server.

Right click on your project and run the application on server.

This will first build your application :

and then it will publish it as well, see the changes that we made :

You can also navigate to the Servers tab, right click on your OpenShift Server and click on Publish.

If you want to tail the log files, check environment variables or try port-forwarding it is all there. See below :

So once the environment has been set, you can make multiple changes and can publish them at your will. 🙂

Regards,
Middlewaremagic Team 🙂


How to create a Topic in JBoss AS 7 ?

In this article we would show how to create a Topic in JBoss AS7 which uses HornetQ, in this example we would use CLI command to create a Topic as well to deploy an EAR file which would consists of both a message sender as well as an MDB which would work as the consumer to consume the messages by the sender code.

We would be using the standalone mode for this article and in JBoss AS 7.1.0.CR1 version we have a different xml file called standalone-full.xml which has the all the HornetQ related configuration, but it has been said that the same would be integrated in the standalone.xml file in coming JBoss AS 7.1 version.

Before creating a Topic lets create an MDB and a Topic sender program which would be used to send and receive the messages.

Steps to create an MDB & Topic Sender

  1. Create a folder called MDB-3.
  2. In “MDB-3” folder create three folders called MDB3.jar, META-INF, TopicSender.war
  3. In “MDB3.jar” folder create MyMDB.java file and copy the below code in it and compile it as well.
  4. import javax.jms.Message;
    import javax.jms.TextMessage;
    import javax.jms.JMSException;
    import javax.jms.MessageListener;
    import javax.ejb.MessageDriven;
    import javax.ejb.ActivationConfigProperty;
    
    @MessageDriven(activationConfig =
            {
            @ActivationConfigProperty(propertyName="destinationType", propertyValue="javax.jms.Topic"),
            @ActivationConfigProperty(propertyName="destination", propertyValue="topic/MyTopic"),
            //************************* RECONNECT PARAMERTERS  *****************************
            @ActivationConfigProperty(propertyName="reconnectAttempts", propertyValue="60"),
            @ActivationConfigProperty(propertyName="reconnectInterval", propertyValue="10")
            })
    public class MyMDB implements MessageListener{
        public void onMessage(Message message) {
            TextMessage textMessage = (TextMessage) message;
            try {
                System.out.println("===> MyMDB Received: "+ textMessage.getText());
            } catch (JMSException e) {
                e.printStackTrace();
            }
        }
    }
    
  5. In “META-INF” folder create application.xml file and copy the below code in it.
  6. <?xml version="1.0" encoding="UTF-8"?>
    <application version="5" xmlns="http://java.sun.com/xml/ns/javaee"
                  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                  xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/application_5.xsd">
       <module>
           <ejb>MDB3.jar</ejb>
       </module>
       <module>
    	<web>
    	   <web-uri>TopicSender.war</web-uri>
    	   <context-root>TopicSender</context-root>
    	</web>
       </module>
    </application>
    
  7. In “TopicSender.war” folder create WEB-INF folder which has classes folder and web.xml inside it and copy the below code in “web.xml”.
  8. <web-app>
    <servlet>
    	<servlet-name>TopicSenderServlet</servlet-name>
    	<servlet-class>TopicSenderServlet</servlet-class>
    	<load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
    	<servlet-name>TopicSenderServlet</servlet-name>
    	<url-pattern>/TopicSenderServlet</url-pattern>
    </servlet-mapping>
    </web-app>
    
  9. Then In “classes” folder create TopicSenderServlet.java file and copy the below code in it and compile it as well
  10. import java.io.*;
    import java.util.*;
    import javax.jms.*;
    import javax.naming.*;
    import javax.transaction.*;
    import javax.servlet.http.*;
    import javax.rmi.PortableRemoteObject;
    import javax.servlet.ServletException;
    
    public class TopicSenderServlet extends HttpServlet 
    {
    	static PrintWriter out;
    	public void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 
    	{
    		try
    		{
    			out=response.getWriter();
    			InitialContext ic = getInitialContext();
    			init(ic, TOPIC_NAME);
    			sendMsg(this);
    		}
    		catch(Exception e)
    		{
    			e.printStackTrace();
    		}	
    	}
    	
    	//*************** Connection Factory JNDI name *************************
    	public final static String CONN_FACTORY="/ConnectionFactory";
    	//*************** Topic JNDI name *************************
    	public final static String TOPIC_NAME="topic/MyTopic";
    	
    	protected TopicConnectionFactory tconFactory;
    	protected TopicConnection tcon;
    	protected TopicSession tsession;
    	protected static TopicPublisher tpublisher;
    	protected Topic topic;
    	protected static TextMessage msg;
    
    	public void init(Context ctx, String topicName) throws NamingException, JMSException
    	{
    		tconFactory = (TopicConnectionFactory) PortableRemoteObject.narrow(ctx.lookup(CONN_FACTORY),TopicConnectionFactory.class);
    		tcon = tconFactory.createTopicConnection();
    		tsession = tcon.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);
    		topic = (Topic) PortableRemoteObject.narrow(ctx.lookup(topicName), Topic.class);
    		tpublisher = tsession.createPublisher(topic);
    		msg = tsession.createTextMessage();
    		tcon.start();
    	}
    
    	private static void sendMsg(TopicSenderServlet ts) throws IOException, JMSException
    	{
    		boolean readFlag=true;
    		out.println("<HTML>");
    		out.println("<BODY>");
    		out.println("<CENTER>");
    		out.println("<H1>Topic Sender Servlet</H1>");
    		out.println("Following Messages has been sent !!!<br>");
    		out.println("====================================");
    		for(int j=1;j<=3;j++) 
    		{
            		msg.setText(""+j); 		// Messages 
            		tpublisher.publish(msg); 	// Messages sent
       			out.println("<BR>Message Sent = "+j);
    		}
    		out.println("<BR>====================================");
    		out.println("</CENTER>");
    		out.println("</BODY>");
    		out.println("</HTML>");
    	}
    
    	private static InitialContext getInitialContext() throws NamingException
    	{
    		return new InitialContext();
    	}
    }
    
  11. Once you have done with all this you now just have to create an EAR file out of this, for that you need to be in the MDB-3 folder and run the below command (make sure you have setted the java/bin in your PATH). Note: you would have to give the (.) dot at last as well
  12. MDB-3] jar -cvf MDB-3.ear .
    

Steps for creating a Topic in JBoss AS 7

  1. Now we would have to start our JBoss AS 7 with standalone-full.xml using -c parameter during the start-up of JBoss
  2. bin] ./standalone.sh -c standalone-full.xml
    
  3. Once the JBoss server is up and running properly then you can run the below CLI command with using the a Topic-setup.cli file which would make sure that a new Topic MyTopic is been created and it would also deploy the MDB-3.ear to the server.
  4. Create a file called Topic-setup.cli in the bin folder of JBoss and copy the below content

    connect
    add-jms-topic --name=MyTopic --entries=topic/MyTopic
    deploy /home/urs/JMS/MDB-3/MDB-3.ear
    

    Run the below command

    bin] ./jboss-admin.sh --file=Topic-setup.cli
    

Testing


Once you can successfully created a Topic and deployed the application using the above CLI command now its time for testing our newly created Topic

  1. Open a browser and hit the below URL (I am supposing that you are running JBoss on default port and IP) and that would send 3 messages to the Topic which can be seen on the browser itself
  2. http://localhost:8080/TopicSender/TopicSenderServlet
    

  3. After hitting the above URL you would see the below messages been received by the MDB on the running JBoss server prompt
  4. 14:32:48,295 INFO  [stdout] (Thread-1 (group:HornetQ-client-global-threads-1623557144)) ===> MyMDB Received: 1
    14:32:48,295 INFO  [stdout] (Thread-0 (group:HornetQ-client-global-threads-1623557144)) ===> MyMDB Received: 2
    14:32:48,296 INFO  [stdout] (Thread-2 (group:HornetQ-client-global-threads-1623557144)) ===> MyMDB Received: 3
    

How to create a Queue in JBoss AS 7 ?

In this article we would show how to create a queue in JBoss AS7 which uses HornetQ, in this example we would use CLI command to create a queue as well to deploy an EAR file which would consists of both a message sender as well as an MDB which would work as the consumer to consume the messages by the sender code.

We would be using the standalone mode for this article and in JBoss AS 7.0.1 Final version we have a different xml file called standalone-preview.xml which has the all the HornetQ related configuration, but it has been said that the same would be integrated in the standalone.xml file in coming JBoss AS 7.1 version.

Before creating a queue lets create an MDB and a Queue sender program which would be used to send and receive the messages.

Steps to create an MDB & Queue Sender

  1. Create a folder called MDB-3.
  2. In “MDB-3” folder create three folders called MDB3.jar, META-INF, QueueSender.war
  3. In “MDB3.jar” folder create MyMDB.java file and copy the below code in it and compile it as well.
  4. import javax.jms.Message;
    import javax.jms.TextMessage;
    import javax.jms.JMSException;
    import javax.jms.MessageListener;
    import javax.ejb.MessageDriven;
    import javax.ejb.ActivationConfigProperty;
    
    @MessageDriven(activationConfig =
            {
            @ActivationConfigProperty(propertyName="destinationType", propertyValue="javax.jms.Queue"),
            @ActivationConfigProperty(propertyName="destination", propertyValue="queue/TestQ")
            })
    
    public class MyMDB implements MessageListener
    {
        public void onMessage(Message message)
        {
            TextMessage textMessage = (TextMessage) message;
            try
           {
                System.out.println("===> MyMDB Received: "+ textMessage.getText());
            }
            catch (JMSException e)
            {
                e.printStackTrace();
            }
        }
    }
    
  5. In “META-INF” folder create application.xml file and copy the below code in it.
  6. <?xml version="1.0" encoding="UTF-8"?>
    <application version="5" xmlns="http://java.sun.com/xml/ns/javaee"
                  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                  xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
                  http://java.sun.com/xml/ns/javaee/application_5.xsd">
       <module>
           <ejb>MDB3.jar</ejb>
       </module>
       <module>
    	<web>
    	   <web-uri>QueueSender.war</web-uri>
    	   <context-root>QueueSender</context-root>
    	</web>
       </module>
    </application>
    
  7. In “QueueSender.war” folder create WEB-INF folder which has classes folder and web.xml inside it and copy the below code in “web.xml”.
  8. <web-app>
    <servlet>
    	<servlet-name>QueueSenderServlet</servlet-name>
    	<servlet-class>QueueSenderServlet</servlet-class>
    	<load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
    	<servlet-name>QueueSenderServlet</servlet-name>
    	<url-pattern>/QueueSenderServlet</url-pattern>
    </servlet-mapping>
    </web-app>
    
  9. Then In “classes” folder create QueueSenderServlet.java file and copy the below code in it and compile it as well
  10. import java.io.*;
    import javax.jms.*;
    import javax.naming.*;
    import javax.servlet.http.*;
    import javax.servlet.ServletException;
    
    public class QueueSenderServlet extends HttpServlet
    {
    	static PrintWriter out;
    	public void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
     	 {
    		try
    		{
    			out=response.getWriter();
    			InitialContext ic = getInitialContext();
    			init(ic, QUEUE_NAME);
    			sendMsg(this);
    		}
    		catch(Exception e)
    		{
    			e.printStackTrace();
    		}
    	}
    
    	//*************** Connection Factory JNDI name *************************
    	public final static String CNN_FACTORY="/ConnectionFactory";
    
    	//*************** Connection Factory JNDI name *************************
    	public final static String QUEUE_NAME="queue/TestQ";
    
    	private QueueConnectionFactory qconFactory;
    	private QueueConnection qcon;
    	private QueueSession qsession;
    	private static QueueSender qsender;
    	private Queue queue;
    	private static TextMessage msg;
    
    	public void init(Context ctx, String queueName)throws NamingException, JMSException
    	{
    		qconFactory = (QueueConnectionFactory) ctx.lookup(CNN_FACTORY);
    		qcon = qconFactory.createQueueConnection();
    		qsession = qcon.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
    		queue = (Queue) ctx.lookup(queueName);
    		qsender = qsession.createSender(queue);
    		msg = qsession.createTextMessage();
    		qcon.start();
    	}
    
    	private static void sendMsg(QueueSenderServlet qs) throws IOException, JMSException
    	{
    		boolean readFlag=true;
    		out.println("");
    		out.println("");
    		out.println("");
    		out.println("<h1>Queue Sender Servlet</h1>");
    		out.println("Following Messages has been sent !!!");
    		out.println("====================================");
    		for(int j=1;j<=3;j++)
    		{
            		msg.setText(""+j); 	// Messages
            		qsender.send(msg); 	// Messages sent
       			out.println("Message Sent = "+j);
    		}
    		out.println("====================================");
    		out.println("");
    		out.println("");
    		out.println("");
    	}
    
    	private static InitialContext getInitialContext() throws NamingException
    	{
    		return new InitialContext();
    	}
    }
    
  11. Once you have done with all this you now just have to create an EAR file out of this, for that you need to be in the MDB-3 folder and run the below command (make sure you have setted the java/bin in your PATH). Note: you would have to give the (.) dot at last as well
  12. MDB-3] jar -cvf MDB-3.ear .
    

Steps for creating a Queue in JBoss AS 7

  1. Now we would have to start our JBoss AS 7 with standalone-preview.xml using –server-config parameter during the start-up of JBoss
  2. bin] ./standalone.sh --server-config=standalone-preview.xml
    
  3. Once the JBoss server is up and running properly then you can run the below CLI command with using the a queue-setup.cli file which would make sure that a new queue TestQ is been created and it would also deploy the MDB-3.ear to the server.
  4. Create a file called queue-setup.cli in the bin folder of JBoss and copy the below content

    connect
    add-jms-queue --name=TestQ --entries=queue/TestQ
    deploy /home/urs/JMS/MDB-3/MDB-3.ear
    

    Run the below command

    bin] ./jboss-admin.sh --file=queue-setup.cli
    

Testing


Once you can successfully created a queue and deployed the application using the above CLI command now its time for testing our newly created queue

  1. Open a browser and hit the below URL (I am supposing that you are running JBoss on default port and IP) and that would send 3 messages to the queue which can be seen on the browser itself
  2. http://localhost:8080/QueueSender/QueueSenderServlet
    

  3. After hitting the above URL you would see the below messages been received by the MDB on the running JBoss server prompt
  4. 14:42:06,398 INFO  [stdout] (Thread-0 (group:HornetQ-client-global-threads-980087547)) ===> MyMDB Received: 1
    14:42:06,403 INFO  [stdout] (Thread-3 (group:HornetQ-client-global-threads-980087547)) ===> MyMDB Received: 2
    14:42:06,403 INFO  [stdout] (Thread-2 (group:HornetQ-client-global-threads-980087547)) ===> MyMDB Received: 3
    

Copyright © 2010-2012 Middleware Magic. All rights reserved. |