欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

JMS的一个简单同步实现

程序员文章站 2024-03-26 08:03:30
...

Sender类,主要用来发送JMS消息

import java.io.OutputStreamWriter;

import javax.jms.DeliveryMode;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.MessageProducer;
import javax.jms.Queue;
import javax.jms.QueueConnection;
import javax.jms.QueueConnectionFactory;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

import org.apache.log4j.ConsoleAppender;
import org.apache.log4j.Logger;
import org.apache.log4j.PatternLayout;

public class JMSSender {
    private static final Logger log = Logger.getLogger(JMSSender.class);
    
    private static QueueConnectionFactory factory = null;
    
    private static String requestMessage = "Hello, JMS!";
    
    public static void main(String[] args){
        String initialContextFactory = "com.tibco.tibjms.naming.TibjmsInitialContextFactory";
        String providerURL = "tibjmsnaming://gftsint.nam.nsroot.net:7222";
        String queueConnectionFactory = "tech.na.cars.QueueCF";
        String requestQueue = "tech.na.cars.vcs2.web.RequestQueue";
        
        ConsoleAppender appender = new ConsoleAppender();
        appender.setWriter(new OutputStreamWriter(System.out));
        appender.setLayout(new PatternLayout("%-5p [%t]: %m%n"));
        log.addAppender(appender);
        
        int emsMessageTimeout = 60 * 1000 * 60 * 24;
        
        try {
            Context context = new InitialContext();
            context.addToEnvironment(Context.INITIAL_CONTEXT_FACTORY, initialContextFactory);
            context.addToEnvironment(Context.PROVIDER_URL, providerURL);
            factory = (QueueConnectionFactory) context.lookup(queueConnectionFactory);
            QueueConnection requestConnection = factory.createQueueConnection();
            Session requestSession = requestConnection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
            Destination destination = (Queue) context.lookup(requestQueue);
            
            log.info("Create message producer....");
            MessageProducer producer = requestSession.createProducer(destination);
            producer.setDeliveryMode(DeliveryMode.PERSISTENT);
            producer.setTimeToLive(emsMessageTimeout);
            
            log.info("Creating message....");
            TextMessage message = requestSession.createTextMessage(requestMessage);
            
            log.info("Sending message....");
            producer.send(message);
        } catch (NamingException e) {
            log.error("Exception", e);
        } catch (JMSException e) {
            log.error("Exception", e);
        }
    }
}

 

接下来是receiver类,用来接收发送的消息:

import java.io.OutputStreamWriter;

import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageConsumer;
import javax.jms.Queue;
import javax.jms.QueueConnection;
import javax.jms.QueueConnectionFactory;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

import org.apache.log4j.ConsoleAppender;
import org.apache.log4j.Logger;
import org.apache.log4j.PatternLayout;

public class JMSReceiver {
    private static final Logger log = Logger.getLogger(JMSReceiver.class);
    
    private static QueueConnectionFactory factory = null;
    
    public static void main(String[] args){
        String initialContextFactory = "com.tibco.tibjms.naming.TibjmsInitialContextFactory";
        String providerURL = "tibjmsnaming://gftsint.nam.nsroot.net:7222";
        String queueConnectionFactory = "tech.na.cars.QueueCF";
        String responseQueue = "tech.na.cars.vcs2.web.RequestQueue";
        
        ConsoleAppender appender = new ConsoleAppender();
        appender.setWriter(new OutputStreamWriter(System.out));
        appender.setLayout(new PatternLayout("%-5p [%t]: %m%n"));
        log.addAppender(appender);
        
        try {
            Context responseContext = new InitialContext();
            responseContext.addToEnvironment(Context.INITIAL_CONTEXT_FACTORY, initialContextFactory);
            responseContext.addToEnvironment(Context.PROVIDER_URL, providerURL);
            factory = (QueueConnectionFactory) responseContext.lookup(queueConnectionFactory);
            QueueConnection responseConnection = factory.createQueueConnection();
            Session responseSession = responseConnection.createSession(false, Session.CLIENT_ACKNOWLEDGE);
            Destination responseDestination = (Queue) responseContext.lookup(responseQueue);
            
            log.info("Create message consumer....");
            MessageConsumer consumer = responseSession.createConsumer(responseDestination);
            responseConnection.start();
            log.info("Receive message....");
            Message message = null;
            while(message == null){
                message = consumer.receive();
            }
            
            String stringMessage = ((TextMessage) message).getText();
            log.info("The response message content is " + stringMessage);
        } catch (NamingException e) {
            log.error("Exception", e);
        } catch (JMSException e) {
            log.error("Exception", e);
        }
    }
}

 手工编写代码的缺陷是每个类都得定义factory和URL等东西,不利于管理