0
Im新的java消息驅動bean,我有一點問題。我做了一個測試web項目(服務器是glassfish 4.0)。消息驅動的bean只輸出一個值
在messageclient中,我向消息隊列發送3個字符串。如果我正確理解消息驅動的bean,則消息驅動bean應該將所有值寫入我的控制檯,但是它只寫入隊列中的第一個值。 我試圖實現這一點,因爲我需要不同的線程從我的Web應用程序發送電子郵件給客戶。有沒有更好的方法?
這裏是我的郵件客戶端的代碼:
@Named(value = "messageDrivenbean")
@ViewScoped
public class MessageDrivenBeanClient implements Serializable
{
ArrayList <String>mail;
@Resource(name="connFactory", mappedName="mailConnFactory")
private QueueConnectionFactory QueueConnectionFactory;
@Resource(name="jmsQueue", mappedName="Queue")
private Queue queue;
/**
* Creates a new instance of testingbean
*/
public MessageDrivenBeanClient()
{
}
@PostConstruct
public void init(){
}
Connection conn;
public void start() throws JMSException
{
String text;
Message msg = null;
final int NUM_MSGS = 3;
conn = QueueConnectionFactory.createConnection();
Session sesion=conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
MessageProducer msgProducer=sesion.createProducer(queue);
msg=sesion.createTextMessage();
for (int i = 0; i < NUM_MSGS; i++) {
text = "This is message " + (i + 1);
System.out.println("Sending message: " + text);
msg.setStringProperty("message", text);
msgProducer.send(msg);
}
}
}
我的消息驅動bean:
@MessageDriven(activationConfig =
{
@ActivationConfigProperty
(propertyName="destinationType",propertyValue="javax.jms.Queue"),
@ActivationConfigProperty(propertyName = "destinationLookup", propertyValue = "Queue")
})
public class MsgDrivnBean implements MessageListener
{
@Resource
private MessageDrivenContext mdc;
public MsgDrivnBean()
{
}
@Override
public void onMessage(Message message)
{
TextMessage tm=null;
try{
if(message instanceof TextMessage){
tm= (TextMessage) message;
System.out.println(message.getStringProperty("message"));
System.out.print("sem v msgdrivenbeanu"+ tm.getStringProperty("message"));
//logger.error(tm.getText());
}
else
{
System.out.println("Error");
}
}
catch(JMSException jms)
{
jms.printStackTrace();
mdc.setRollbackOnly();
}
}
}
我的電流輸出爲:
Sending message: This is message 1
INFO: Sending message: This is message 2
INFO: Sending message: This is message 3
INFO: This is message 1
INFO: sem v msgdrivenbeanuThis is message 1
我認爲它也應該寫出消息2和消息3,但事實並非如此。
請幫幫我。
我也這麼認爲。不確定這是否有助於它,但我沒有看到你關閉客戶端代碼中的會話和連接。也許嘗試添加到客戶端。 – mikemil
試過但沒有運氣.... – Salko