Posts Tagged J2EE

Adobe Flex, BlazeDS, and TIBCO EMS

I’ve searched high and low and can’t really find a good walk through on how to configure TIBCO EMS and BlazeDS. The documentation can also be a little gray on how to correctly use the JMS adapter so i’ve taken the liberty of putting together this little pictorial on how it’s done.

First things first, get yourself a fresh copy of Eclipse. For this setup, I used Eclipse Ganymede (3.4, J2EE Edition) on Windows. You’ll need the Java stuff in addition to Flex Builder because we’re going to launch our own tomcat inside of Eclipse instead of using BlazeDS Turnkey or any of those. Also, if you want to add in Java remoting later you’ll appreciate having the components already there.

Next, install the Flex plugin for Eclipse and update according. I downloaded and installed the Flex 3.3 SDK and changed my default SDK in the preferences. Once the eclipse platform is installed, updated, and configured the way you like it, let’s make a new project.

Note: Click any one of these images for the full size version.
Step 1 - Create a new Project of type "Other"

Step 1 - Create a new Project of type "Other"

Step 2 - Expand the Server category and then select Server

Step 2 - Expand the Server category and then select Server

Step 3 - Create an Apache Tomcat v6.0 Server on Localhost

Step 3 - Create an Apache Tomcat v6.0 Server on Localhost

Step 4 - Set Installation Directory and JRE, Install if Necessary

Step 4 - Set Installation Directory and JRE, Install if Necessary

Step 5 - No projects yet, just hit Finish

Step 5 - No projects yet, just hit Finish

Step 6 - Create another new project of type "Other"

Step 6 - Create another new project of type "Other"

Step 7 - Now expand the Flex Builder category and select "Flex Project"

Step 7 - Now expand the Flex Builder category and select "Flex Project"

Step 8 - Set it for a J2EE application server and enable the combined Java/Flex project using WTP

Step 8 - Set it for a J2EE application server and enable the combined Java/Flex project using WTP

Step 9 - Connect the project to your Tomcat server and select the blazeds.war that you downloaded from Adobe

Step 9 - Connect the project to your Tomcat server and select the blazeds.war that you downloaded from Adobe

Step 10 - Everything should be good here

Step 10 - Everything should be good here

Step 11 - If everything went well, you should see have the following files available

Step 11 - If everything went well, you should see have the following files available

Step 12 - Open the web.xml, messaging-config.xml, and services-config.xml

Step 12 - Open the web.xml, messaging-config.xml, and services-config.xml

First, let’s edit web.xml. This file should be okay out of the box. Here’s mine as a reference.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
	<display-name>blaze1</display-name>
 
	<context-param>
		<param-name>flex.class.path</param-name>
		<param-value>/WEB-INF/flex/hotfixes,/WEB-INF/flex/jars</param-value>
	</context-param>
 
	<!-- Http Flex Session attribute and binding listener support -->
	<listener>
		<listener-class>flex.messaging.HttpFlexSession</listener-class>
	</listener>
 
	<!-- MessageBroker Servlet -->
	<servlet>
		<servlet-name>MessageBrokerServlet</servlet-name>
		<servlet-class>flex.messaging.MessageBrokerServlet</servlet-class>
		<init-param>
			<param-name>services.configuration.file</param-name>
			<param-value>/WEB-INF/flex/services-config.xml</param-value>
		</init-param>
		<init-param>
			<param-name>flex.write.path</param-name>
			<param-value>/WEB-INF/flex</param-value>
		</init-param>
		<load-on-startup>1</load-on-startup>
	</servlet>
 
	<servlet-mapping>
		<servlet-name>MessageBrokerServlet</servlet-name>
		<url-pattern>/messagebroker/*</url-pattern>
	</servlet-mapping>
	<welcome-file-list>
		<welcome-file>index.html</welcome-file>
		<welcome-file>index.htm</welcome-file>
		<welcome-file>index.jsp</welcome-file>
		<welcome-file>default.html</welcome-file>
		<welcome-file>default.htm</welcome-file>
		<welcome-file>default.jsp</welcome-file>
	</welcome-file-list>
 
	<!-- for WebSphere deployment, please uncomment -->
	<!--
		<resource-ref>
		<description>Flex Messaging WorkManager</description>
		<res-ref-name>wm/MessagingWorkManager</res-ref-name>
		<res-type>com.ibm.websphere.asynchbeans.WorkManager</res-type>
		<res-auth>Container</res-auth>
		<res-sharing-scope>Shareable</res-sharing-scope>
	</resource-ref>
	-->
</web-app>

Now let’s edit services-config.xml. Key things to note here is that i’ve disabled the inclusion of the remoting-config.xml and the proxy-config.xml since i’m not using them. I’ve also changed the default AMF channel from “my-amf” to “AmfChannel1″ and updated the endpoint URL. Everything else is pretty much the same.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
<?xml version="1.0" encoding="UTF-8"?>
<services-config>
 
    <services>
    	<!--
        <service-include file-path="remoting-config.xml" />
        <service-include file-path="proxy-config.xml" />
        -->
        <service-include file-path="messaging-config.xml" />        
    </services>
 
    <security>
        <login-command class="flex.messaging.security.TomcatLoginCommand" server="Tomcat"/>
        <!-- Uncomment the correct app server
        <login-command class="flex.messaging.security.TomcatLoginCommand" server="JBoss">
		<login-command class="flex.messaging.security.JRunLoginCommand" server="JRun"/>        
        <login-command class="flex.messaging.security.WeblogicLoginCommand" server="Weblogic"/>
        <login-command class="flex.messaging.security.WebSphereLoginCommand" server="WebSphere"/>
        -->
 
        <!-- 
        <security-constraint id="basic-read-access">
            <auth-method>Basic</auth-method>
            <roles>
                <role>guests</role>
                <role>accountants</role>
                <role>employees</role>
                <role>managers</role>
            </roles>
        </security-constraint>
         -->
    </security>
 
    <channels>
        <channel-definition id="AmfChannel1" class="mx.messaging.channels.AMFChannel">
            <endpoint url="http://{server.name}:{server.port}/{context.root}/messagebroker/AmfChannel1" class="flex.messaging.endpoints.AMFEndpoint"/>
        </channel-definition>
    </channels>
 
    <logging>
        <target class="flex.messaging.log.ConsoleTarget" level="Error">
            <properties>
                <prefix>[BlazeDS] </prefix>
                <includeDate>false</includeDate>
                <includeTime>false</includeTime>
                <includeLevel>false</includeLevel>
                <includeCategory>false</includeCategory>
            </properties>
            <filters>
                <pattern>Endpoint.*</pattern>
                <pattern>Service.*</pattern>
                <pattern>Configuration</pattern>
            </filters>
        </target>
    </logging>
 
    <system>
        <redeploy>
            <enabled>false</enabled>
            <!-- 
            <watch-interval>20</watch-interval>
            <watch-file>{context.root}/WEB-INF/flex/services-config.xml</watch-file>
            <watch-file>{context.root}/WEB-INF/flex/proxy-config.xml</watch-file>
            <watch-file>{context.root}/WEB-INF/flex/remoting-config.xml</watch-file>
            <watch-file>{context.root}/WEB-INF/flex/messaging-config.xml</watch-file>
            <watch-file>{context.root}/WEB-INF/flex/data-management-config.xml</watch-file>
            <touch-file>{context.root}/WEB-INF/web.xml</touch-file>
             -->
        </redeploy>
    </system>
 
</services-config>

And last but not least, messaging-config.xml. This file is where the JMS connection is made to the TIBCO EMS server. And no, you do not have to put anything in context.xml when you’re doing it this way. :)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
<?xml version="1.0" encoding="UTF-8"?>
<service id="message-service" 
    class="flex.messaging.services.MessageService">
 
    <adapters>
        <adapter-definition id="actionscript" class="flex.messaging.services.messaging.adapters.ActionScriptAdapter" default="true" />
        <adapter-definition id="jms" class="flex.messaging.services.messaging.adapters.JMSAdapter"/>
    </adapters>
 
    <default-channels>
        <channel ref="AmfChannel1"/>
    </default-channels>
 
	<destination id="jmsDest1">
		<properties>
			<jms>
				<!-- topic/queue -->
				<destination-type>topic</destination-type>
				<!-- What type of message we expect -->
				<message-type>javax.jms.TextMessage</message-type>
				<!-- This is the JNDI name of the topic/queue connection factory on the server -->
				<connection-factory>TopicConnectionFactory</connection-factory>
				<!-- This is the JNDI name of the topic/queue on the server -->
				<destination-jndi-name>testTopic</destination-jndi-name>
				<!-- Keep JMS headers when the message is delivered to Flex -->
				<preserve-jms-headers>true</preserve-jms-headers>
				<!-- If we're producing on this destination, what parameters should be used -->				
				<delivery-mode>NON_PERSISTENT</delivery-mode>
				<message-priority>DEFAULT_PRIORITY</message-priority>
				<acknowledge-mode>AUTO_ACKNOWLEDGE</acknowledge-mode>
				<!-- The following lines are for the JNDI connection -->
				<initial-context-environment>
					<property>
						<name>Context.INITIAL_CONTEXT_FACTORY</name>
						<value>com.tibco.tibjms.naming.TibjmsInitialContextFactory</value>             
					</property>
					<property>
						<name>Context.PROVIDER_URL</name>
						<value>tcp://localhost:7222</value>             
					</property>
					<property>
						<name>Context.SECURITY_PRINCIPAL</name>
						<value>admin</value>
					</property>
					<!-- Uncomment if you have a password
					<property>
						<name>Context.SECURITY_CREDENTIALS</name>
						<value>password</value>
					</property>
					-->
				</initial-context-environment>
			</jms>
		</properties>
		<channels>
			<channel ref="AmfChannel1"/>
		</channels>
		<adapter ref="jms"/>
	</destination>
 
</service>

Now we have our Tomcat instance set up, time to import some class libraries.

Step 13 - Right Click the lib directory under WebContent/WEB-INF/lib select Import

Step 13 - Right Click the lib directory under WebContent/WEB-INF/lib select Import

Step 14 - Select General and then File System

Step 14 - Select General and then File System

Step 15 - Select the TIBCO EMS 5.x library directory and select all of the .jar files (There are more than what is pictured here)

Step 15 - Select the TIBCO EMS 5.x library directory and select all of the .jar files (There are more than what is pictured here)

Step 16 - Open up the server view by selecting Window -> Show View -> Other

Step 16 - Open up the server view by selecting Window -> Show View -> Other

Step 17 - Select the Server category and select Servers

Step 17 - Select the Server category and select Servers

Step 18 - Click the Servers tab that appeared at the bottom, highlight the Tomcat Server and click the Debug icon

Step 18 - Click the Servers tab that appeared at the bottom, highlight the Tomcat Server and click the Debug icon

Step 19 - Start the EMS Server instance

Step 19 - Start the EMS Server instance

Step 20 - Create the topic that was referenced in the messaging-config.xml file

Step 20 - Create the topic that was referenced in the messaging-config.xml file

Now jump back into Eclipse and set up the application. I started with a base form, added a text area and called it a day. Here’s the code to add in the message consumer, channel configuration, and event handlers.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
	applicationComplete="messageConsumer.subscribe()">
	<mx:Script>
		<![CDATA[
			// Import the required namespaces
			import mx.messaging.events.MessageEvent;
			import mx.messaging.events.ChannelEvent;
			import mx.messaging.messages.AsyncMessage;
 
			private function incomingMessageHandler(event:MessageEvent):void
			{
				logArea.text += (event.message.body + "\r");
			}
 
			private function connectEventHandler(event:ChannelEvent):void
			{
				logArea.text += (event.channel.endpoint + " is now connected.\r");
			}
 
			private function disconnectEventHandler(event:ChannelEvent):void
			{
				logArea.text += (event.channel.endpoint + " is now disconnected.\r");
			}
		]]>
	</mx:Script>
	<mx:TextArea id="logArea" editable="false" width="95%" height="95%"/>
	<mx:Consumer id="messageConsumer"
		destination="jmsDest1"
		message="incomingMessageHandler(event)"
		channelConnect="connectEventHandler(event)"
		channelDisconnect="disconnectEventHandler(event)">
		<mx:channelSet>
			<mx:ChannelSet>
				<mx:channels>
					<mx:AMFChannel
						url="http://localhost:8080/blaze1/messagebroker/AmfChannel1"/>
				</mx:channels>
			</mx:ChannelSet>
		</mx:channelSet>		
	</mx:Consumer>
</mx:Application>

That should do it for the code work … now for the fun stuff. :)

Step 21 - Fire up the debugger (use the dropdown next to the button)

Step 21 - Fire up the debugger (use the dropdown next to the button)

Step 22 - Select Debug As, then Debug on Server

Step 22 - Select Debug As, then Debug on Server

Step 23 - Select the installed Tomcat image

Step 23 - Select the installed Tomcat image

Step 24 - The project should appear on the right, press Finish

Step 24 - The project should appear on the right, press Finish

Note : The first debug attempt will fail because Eclipse doesn’t correctly deploy the application. What I haven’t figured out is how to correctly associate the project without using the Debug or Run options. If you know a better way, leave a comment. :)
Step 25 - Hit Debug As again and hit the "Flex Application" choice

Step 25 - Hit Debug As again and hit the "Flex Application" choice

Step 26 - If everything worked, you should now be staring at the application and should have a message that the channel has connected.

Step 26 - If everything worked, you should now be staring at the application and should have a message that the channel has connected.

Step 27 - Now send a text message to the JMS topic using your favorite JMS client and enjoy the result!

Step 27 - Now send a text message to the JMS topic using your favorite JMS client and enjoy the result!

That should do it! Enjoy your new found JMS happiness. As a reference, a Channel is what the Flex application uses to communicate with BlazeDS and can be HTTP or AMF. A Destination is where you send the messages to and from. A lot of the examples i’ve found don’t show you how to declare what channel you’re talking to as it’s supposed to be handled automatically, i’ve found this to be unreliable and once I started specifying the channel everything started working flawlessly so i’m staying with it. Also, you can do multiple channels for redundancy and such, but that’s a lesson for another day. ;)

, , , , , , , , , , , ,

2 Comments