JMSメッセージタイプJMSのテスト(5)
3449 ワード
JMS仕様は、永続伝送モードと非永続伝送モードの2つのメッセージ伝送モードを定義する.DeliveryMode.PERSISTENT持続化DeliveryMode.NON_PERSISTENTの非永続化は生産者によってMessageProducer producer=sessionを設定する.createProducer(queue); producer.setDeliveryMode(DeliveryMode.PERSISTENT); 永続化情報:生産者が情報を送信する際にサーバがクラッシュしたり、他の障害が発生したりした場合、サーバメッセージを再起動すれば消費者に任意に送信でき、重要なメッセージの配信率が保証されることを示す.非永続化情報:すべての条件が順調な場合にのみ、メッセージが届くことを保証します.テストソース:生産者コード:import javax.jms.Connection; import javax.jms.ConnectionFactory; import javax.jms.DeliveryMode; import javax.jms.JMSException; import javax.jms.MessageProducer; import javax.jms.Queue; import javax.jms.Session; import org.apache.activemq.ActiveMQConnection; import org.apache.activemq.ActiveMQConnectionFactory; import org.apache.activemq.command.ActiveMQQueue;
}
消費者コード:
import javax.jms.ConnectionFactory; import javax.jms.JMSException; import javax.jms.Message; import javax.jms.MessageConsumer; import javax.jms.MessageListener; import javax.jms.Queue; import javax.jms.Session; import javax.jms.TextMessage;
import org.apache.activemq.ActiveMQConnection; import org.apache.activemq.ActiveMQConnectionFactory; import org.apache.activemq.command.ActiveMQQueue;
public class Receiver { public static void main(String[] args) throws JMSException { ConnectionFactory connectionFactory=new ActiveMQConnectionFactory(ActiveMQConnection.DEFAULT_USER,ActiveMQConnection.DEFAULT_PASSWORD,”tcp://localhost:61616”); Connection connection=connectionFactory.createConnection(); connection.start();
}
public class Sender {
/**
* @param args
* @throws JMSException
*/
public static void main(String[] args) throws JMSException {
ConnectionFactory connectionFactory=new ActiveMQConnectionFactory(ActiveMQConnection.DEFAULT_USER,ActiveMQConnection.DEFAULT_PASSWORD,"tcp://localhost:61616");
Connection connection=connectionFactory.createConnection();
connection.start();
Queue queue=new ActiveMQQueue("testQueue");
Session session=connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
// DeliveryMode.PERSISTENT
MessageProducer producer=session.createProducer(queue);
producer.setDeliveryMode(DeliveryMode.PERSISTENT); //
producer.send(session.createTextMessage("this is presisitent message"));
// DeliveryMode.NON_PERSISTENT
MessageProducer producer1=session.createProducer(queue);
producer1.setDeliveryMode(DeliveryMode.NON_PERSISTENT);//
producer1.send(session.createTextMessage("this is no presisient message"));
//session.commit();
System.out.println(" 。");
}
}
消費者コード:
import javax.jms.Connection;
import javax.jms.ConnectionFactory; import javax.jms.JMSException; import javax.jms.Message; import javax.jms.MessageConsumer; import javax.jms.MessageListener; import javax.jms.Queue; import javax.jms.Session; import javax.jms.TextMessage;
import org.apache.activemq.ActiveMQConnection; import org.apache.activemq.ActiveMQConnectionFactory; import org.apache.activemq.command.ActiveMQQueue;
public class Receiver { public static void main(String[] args) throws JMSException { ConnectionFactory connectionFactory=new ActiveMQConnectionFactory(ActiveMQConnection.DEFAULT_USER,ActiveMQConnection.DEFAULT_PASSWORD,”tcp://localhost:61616”); Connection connection=connectionFactory.createConnection(); connection.start();
Queue queue=new ActiveMQQueue("testQueue");
Session session=connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
MessageConsumer consumer=session.createConsumer(queue);
consumer.setMessageListener(new MessageListener() {
@Override
public void onMessage(Message arg0) {
try {
System.out.println(" "+((TextMessage)arg0).getText());
} catch (JMSException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
}
}