This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<featureManager> | |
<feature>javaee-7.0</feature> | |
<feature>localConnector-1.0</feature> | |
<feature>wasJmsServer-1.0</feature> | |
<feature>jms-2.0</feature> | |
<feature>mdb-3.2</feature> | |
<feature>wasJmsClient-2.0</feature> | |
</featureManager> | |
<wasJmsEndpoint host="*" wasJmsPort="7276" wasJmsSSLPort="7286"/> | |
<connectionManager id="ConMgr1" maxPoolSize="2"/> | |
<messagingEngine> | |
<queue forceReliability="ReliablePersistent" id="MyLibertyQueue" maxMessageDepth="10"/> | |
</messagingEngine> | |
<jmsQueueConnectionFactory connectionManagerRef="ConMgr1" jndiName="jms/MyLibertyQCF"> | |
<properties.wasJms nonPersistentMapping="ExpressNonPersistent" persistentMapping="ReliablePersistent" remoteServerAddress="localhost:7276:BootstrapBasicMessaging"/> | |
</jmsQueueConnectionFactory> | |
<jmsActivationSpec id="jms2-0.0.1-SNAPSHOT/MessageBean"> | |
<properties.wasJms destinationRef="myResponseQueue" destinationType="javax.jms.Queue" remoteServerAddress="localhost:7276:BootstrapBasicMessaging"/> | |
</jmsActivationSpec> | |
<jmsQueue id="myResponseQueue" jndiName="jndi/MyLibertyQueue"> | |
<properties.wasJms priority="1" queueName="MyLibertyQueue" readAhead="AsConnection" timeToLive="60000s"/> | |
</jmsQueue> |
[WARNING ] CNTR4015W: The message endpoint for the MessageBean message-driven bean cannot be activated because the jms2-0.0.1-SNAPSHOT/MessageBean activation specification is not available. The message endpoint will not receive messages until the activation specification becomes available.Code snippet below is a simple MDB app for the sake of this testing purpose:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@MessageDriven | |
public class MessageBean implements MessageListener { | |
@Override | |
public void onMessage(Message msg) { | |
String receiveMsg; | |
try { | |
receiveMsg = ((TextMessage)msg).getText(); | |
System.out.println(receiveMsg); | |
} | |
catch (JMSException e) { | |
e.printStackTrace(); | |
} | |
} | |
} |
Connect to MDB without using JNDI
Now the server is ready to accept the incoming messages. The sample below is a standalone Java program that will establish a connection to Liberty Profile, it doesn’t use the JNDI to send the message to MDB. This is the hardest part as not much information available on the web. Eventually, I have identified 3 libraries which are needed in order to build this program, there are available in the WAS installation path located at AppServer/runtimes. Here are the 3 libraries:- com.ibm.ws.ejb.thinclient_8.5.0.jar
- com.ibm.ws.sib.client.thin.jms_8.5.0.jar
- com.ibm.ws.orb_8.5.0.jar
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import javax.jms.Connection; | |
import javax.jms.Destination; | |
import javax.jms.JMSException; | |
import javax.jms.MessageProducer; | |
import javax.jms.Session; | |
import javax.jms.TextMessage; | |
import javax.naming.NamingException; | |
import com.ibm.websphere.sib.api.jms.JmsConnectionFactory; | |
import com.ibm.websphere.sib.api.jms.JmsFactoryFactory; | |
public class JmsClient { | |
private static void main(String agrs[]) throws JMSException { | |
JmsFactoryFactory jff = JmsFactoryFactory.getInstance(); | |
JmsConnectionFactory jcf = jff.createConnectionFactory(); | |
jcf.setProviderEndpoints("localhost:7276:BootstrapBasicMessaging"); | |
jcf.setBusName("MyBus"); | |
Connection c = jcf.createConnection(); | |
c.start(); | |
Destination d = jff.createQueue("MyLibertyQueue"); | |
Session s = c.createSession(false, Session.AUTO_ACKNOWLEDGE); | |
MessageProducer mp = s.createProducer(d); | |
TextMessage tm = s.createTextMessage("hello"); | |
System.out.println("Message sent to MyLibertyQueue"); | |
mp.send(tm); | |
s.close(); | |
c.stop(); | |
c.close(); | |
} | |
} |
Caused by: com.ibm.websphere.sib.exception.SIIncorrectCallException: CWSIT0003E: No busName property was found in the connection properties. at com.ibm.ws.sib.trm.client.ClientAttachProperties.< init>(ClientAttachProperties.java:109) at com.ibm.ws.sib.trm.client.TrmSICoreConnectionFactoryImpl.createConnection(TrmSICoreConnectionFactoryImpl.java:295) at com.ibm.ws.sib.trm.client.TrmSICoreConnectionFactoryImpl.createConnection(TrmSICoreConnectionFactoryImpl.java:222) at com.ibm.ws.sib.api.jmsra.impl.JmsJcaConnectionFactoryImpl.createCoreConnection(JmsJcaConnectionFactoryImpl.java:711) at com.ibm.ws.sib.api.jmsra.impl.JmsJcaConnectionFactoryImpl.createCoreConnection(JmsJcaConnectionFactoryImpl.java:647) at com.ibm.ws.sib.api.jmsra.impl.JmsJcaConnectionFactoryImpl.createConnection(JmsJcaConnectionFactoryImpl.java:376) at com.ibm.ws.sib.api.jms.impl.JmsManagedConnectionFactoryImpl.createConnection(JmsManagedConnectionFactoryImpl.java:162) ... 3 more
Connect to MDB with JNDI
This is much easier to implement as compare to the previous client app just because it uses JNDI. It doesn’t require any additional library from WAS server. Long story short, here is the code:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@WebServlet("/JmsClient") | |
public class JmsWebClient extends HttpServlet { | |
public JmsClientWebApp() { | |
super(); | |
} | |
protected void doGet(HttpServletRequest request, HttpServletResponse response) { | |
try { | |
QueueConnectionFactory qcf = (QueueConnectionFactory) new InitialContext().lookup("java:comp/env/jms/MyLibertyQCF"); | |
Queue queue = (Queue) new InitialContext().lookup("java:comp/env/jndi/MyLibertyQueue"); | |
QueueConnection con = qcf.createQueueConnection(); | |
con.start(); | |
QueueSession sessionSender = con.createQueueSession(false, javax.jms.Session.AUTO_ACKNOWLEDGE); | |
QueueSender send = sessionSender.createSender(queue); | |
TextMessage msg = sessionSender.createTextMessage(); | |
msg.setText("Liberty Sample Message"); | |
send.send(msg); | |
if (con != null) | |
con.close(); | |
System.out.println("Message send completed."); | |
} | |
catch (NamingException | JMSException e) { | |
//TODO Auto-generated catch block | |
e.printStackTrace(); | |
} | |
} | |
} |
No comments:
Post a Comment