Skip to main content

Glassfish + ActiveMQ; producer sends to wrong queue

No replies
kost_ru
Offline
Joined: 2013-12-27
Points: 0

Helo, guys! I've spent the whole day googling to sort out my problem. So here the problem is:

I have Glassfish server v 4.0 and ActiveMQ v 5.9. I followed these topics:

http:// geertschuring.wordpress.com/2012/04/20/how-to-connect-glassfish-3-to-activemq-5/
http:// codelevain.wordpress.com/2012/12/21/glassfish_activemq/
In my case i've done:

Queue "sftptask" was created in ActiveMQ;
Resource Adapter & Connection Pool & Connection Factory were created in Glassfish (ping works fine);
Admin Object Resources (jndi:"jms/queue/sftptask", PhysicalName:"sftptask").
Then I created to EJB project "test":

1) mdbean.java

package ru.test;
import javax.ejb.ActivationConfigProperty;
import javax.ejb.MessageDriven;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.TextMessage;

@MessageDriven
public class mdbean implements MessageListener {

public mdbean() {
// TODO Auto-generated constructor stub
}

public void onMessage(Message message) {
System.out.println("onMessage");
if (message instanceof TextMessage) {
TextMessage textMessage = (TextMessage)message;
try {
System.out.println(textMessage.getText());
} catch (JMSException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} else {
try {
System.out.println(message.getJMSType());
} catch (JMSException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

}

}
2) ejb-jar.xml

<?xml version="1.0" encoding="UTF-8"?>
xmlns="http://java.sun.com/xml/ns/javaee" xmlns:ejb="http://java.sun.com/xml/ns/javaee/ejb-jar_3_0.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/ejb-jar_3_1.xsd"
version="3.1">


mdbean

jms/queue/sftptask
javax.jms.Queue
jms/queue/sftptask



3) glassfish-ejb-jar.xml

<?xml version='1.0' encoding='UTF-8'?>




mdbean

jms/activeMQConnectionFactory


activemq-rar-5.9.0


DestinationType
javax.jms.Queue


destination
sftptask





It wokrs as expected: I could send a message from ActiveMQ console to "sftptask" queue and see 2 new lines in server.log. Also I see one consumer in "sftptask" queue at ActiveMQ console.

Also i wrote "test2" project with session bean (like a producer):

1) timer.java

package ru.test;

import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import javax.ejb.Singleton;
import javax.ejb.Startup;
import javax.ejb.Timeout;
import javax.ejb.Timer;
import javax.ejb.TimerConfig;
import javax.ejb.TimerService;
import javax.jms.ConnectionFactory;
import javax.jms.JMSException;
import javax.jms.Queue;
import javax.jms.QueueBrowser;
import javax.jms.Session;
import javax.jms.TextMessage;

@Startup
@Singleton
public class timer {

@Resource
private TimerService timerService;

@Resource(name = "jms/activeMQConnectionFactory")
private ConnectionFactory cf;

@Resource(mappedName = "jms/queue/sftptask")
private Queue sftpTaskQueue;

@PostConstruct
public void init() {
System.out.println("init");

long interval = 10000L;
timerService.createIntervalTimer(0L, interval, new TimerConfig(null,
false));
}

@Timeout
public void process(Timer timer) {
javax.jms.Connection conn = null;
javax.jms.Session s = null;
javax.jms.MessageProducer mp = null;

System.out.println("process" + sftpTaskQueue.getClass().getCanonicalName());

if (sftpTaskQueue != null) {

try {
conn = cf.createConnection();
conn.start();
s = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
mp = s.createProducer(sftpTaskQueue);
TextMessage msg = s.createTextMessage();
msg.setText("Your text message");
mp.send(msg);
System.out.println("sent" + msg.getJMSMessageID());
QueueBrowser browser = s.createBrowser(sftpTaskQueue);
Enumeration enu = browser.getEnumeration();
List list = new ArrayList();
while (enu.hasMoreElements()) {
TextMessage message = (TextMessage) enu.nextElement();
list.add(message.getText());
}
System.out.println("Size " + list.size());
} catch (JMSException ex) {
ex.printStackTrace();
} finally {
if(conn != null) {
try {
conn.close();
} catch (JMSException e) {
e.printStackTrace();
}
}
}
} else {
System.out.println("null queue!");
}

}

}
2) ejb-jar.xml

<?xml version="1.0" encoding="UTF-8"?>
xmlns="http://java.sun.com/xml/ns/javaee" xmlns:ejb="http://java.sun.com/xml/ns/javaee/ejb-jar_3_0.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/ejb-jar_3_1.xsd"
version="3.1">


timer

jms/queue/sftptask
javax.jms.Queue
jms/queue/sftptask



3) glassfish-ejb-jar.xml

<?xml version='1.0' encoding='UTF-8'?>




timer

sftptask
jms/queue/sftptask



And this bean works extremly strange: it does send message, but not to ActiveMQ! Glassfish automatically created PhysicalDestination (server->JMS Physical Destination).

But sftpTaskQueue.getClass().getCanonicalName() = "processorg.apache.activemq.command.ActiveMQQueue"!

Also i looked into domain.xml and setted enabled="true" for "jms/queue/sftptask", then restared both servers, but nothing changed.

I've tried lots of different ways and read tones of articles, but I still can't make session bean send to ActiveMQ Queue. What am I doing wrong?

P.S.

Here is zipped projects:

https://www.dropbox.com/s/d2nwlfrye7p97h6/test.zip https://www.dropbox.com/s/rswoemdu679w5nk/test2.zip