`
dyllove98
  • 浏览: 1387062 次
  • 性别: Icon_minigender_1
  • 来自: 济南
博客专栏
73a48ce3-d397-3b94-9f5d-49eb2ab017ab
Eclipse Rcp/R...
浏览量:38480
4322ac12-0ba9-3ac3-a3cf-b2f587fdfd3f
项目管理checkList...
浏览量:78934
4fb6ad91-52a6-307a-9e4f-816b4a7ce416
哲理故事与管理之道
浏览量:132077
社区版块
存档分类
最新评论

jms两种模式例子

阅读更多

超越昨天的自己系列(2)

1、P2P模型
在P2P模型中,有下列概念:消息队列(Queue)、发送者(Sender)、接收者(Receiver)。每个消息都被发送到一个特定的队 列,接收者从队列中获取消息。列保留着消息,直到它们被消费或超时
每个消息只有一个消费者 (Consumer)(即一旦被消费,消息就不再在消息队列中)
发送者和接收者之间在时间上没有依赖性 ,也就是说当发送者发送了消息之后,不管接收者有没有正在运行,它不会影响到消息被发送到队列。
接收者在成功接收消息之后需向队列应答成功
如果你希望发送的每个消息都应该被成功处理 的话,那么你需要P2P模型。

   适用场合:想让接收者进行且只进行一次处理

 

customer:

 

import javax.jms.Connection;

public class Consumer {

    private static String brokerURL = "tcp://localhost:61616";
    private static transient ConnectionFactory factory;
    private transient Connection connection;
    private transient Session session;
    
    private String jobs[] = new String[]{"suspend", "delete"};
    
    public Consumer() throws JMSException {
        factory = new ActiveMQConnectionFactory(brokerURL);
        connection = factory.createConnection();
        connection.start();
        session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
    }
    
    public void close() throws JMSException {
        if (connection != null) {
            connection.close();
        }
    }    
    
    public static void main(String[] args) throws JMSException {
        Consumer consumer = new Consumer();
        for (String job : consumer.jobs) {
            Destination destination = consumer.getSession().createQueue("JOBS." + job);
            MessageConsumer messageConsumer = consumer.getSession().createConsumer(destination);
            messageConsumer.setMessageListener(new Listener(job));
        }
    }
    
    public Session getSession() {
        return session;
    }


}

 

 

 

listener:

import javax.jms.Message;

public class Listener implements MessageListener {

    private String job;
    
    public Listener(String job) {
        this.job = job;
    }

    public void onMessage(Message message) {
        try {
            //do something here
            System.out.println(job + " id:" + ((ObjectMessage)message).getObject());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}

publish:

import javax.jms.Connection;

public class Publisher {

    private static String brokerURL = "tcp://localhost:61616";
    private static transient ConnectionFactory factory;
    private transient Connection connection;
    private transient Session session;
    private transient MessageProducer producer;
    
    private static int count = 10;
    private static int total;
    private static int id = 1000000;
    
    private String jobs[] = new String[]{"suspend", "delete"};
    
    public Publisher() throws JMSException {
        factory = new ActiveMQConnectionFactory(brokerURL);
        connection = factory.createConnection();
        connection.start();
        session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        producer = session.createProducer(null);
    }    
    
    public void close() throws JMSException {
        if (connection != null) {
            connection.close();
        }
    }    
    
    public static void main(String[] args) throws JMSException {
        Publisher publisher = new Publisher();
        while (total < 1000) {
            for (int i = 0; i < count; i++) {
                publisher.sendMessage();
            }
            total += count;
            System.out.println("Published '" + count + "' of '" + total + "' job messages");
            try {
              Thread.sleep(1000);
            } catch (InterruptedException x) {
            }
          }
        publisher.close();

    }
    
    public void sendMessage() throws JMSException {
        int idx = 0;
        while (true) {
            idx = (int)Math.round(jobs.length * Math.random());
            if (idx < jobs.length) {
                break;
            }
        }
        String job = jobs[idx];
        Destination destination = session.createQueue("JOBS." + job);
        Message message = session.createObjectMessage(id++);
        System.out.println("Sending: id: " + ((ObjectMessage)message).getObject() + " on queue: " + destination);
        producer.send(destination, message);
    }    

}

 

 

pub/sub方式
概念:topic,publisher,subscriber,主题,发布者,订阅者三个角色。主题和订阅者是一对多关系,一个主题可以被多个订阅者订 阅。当发布者向某个主题发送一条消息时,所有的订阅者都会收到。
如何理解订阅的概念呢,个人理解,分两种情况:
一、 创建一个订阅者时使用session.createDurableSubscriber(topic, clientId )方法创建为持久型订阅者时,该topic和clientId的订阅关系将被保存在服务器上,即产生了订阅关系。这样clientId这个id的订阅者将 在离线的时候,也不会丢失消息。这个订阅关系也可以理解为被持久化在jms服务器上了,使用jmx的监视控制台(我使用的activeMq),可以看到有 一个Subscription的节点,下面有你订阅主题时给定的客户端名字。可以使用unsubscribe 方法 取消订阅关系。

二、 创建一个非持久化方式的订阅者时,只有在客户端订阅者连接到jms服务器时,订阅关系成立,订阅者离线时,订阅关系即取消,不会保存在服务器上,这也是非 持久化方式订阅者不能离线接收消息的原因吧。默认为广播方式,在没有订阅者连接到服务器时,发送的消息将丢失,不会保存在服务器。

     subSession.createConsumer(destination);
     subSession.createSubscriber(topic);

subSession.createDurableSubscriber(topic, name);    //name  是 一个jms 用来区别定阅者的id

 取消定阅

subscriber.close();

session.unsubscribe(name);  //只对持久定阅,

 

customer:

import javax.jms.Connection;

public class Consumer {

    private static String brokerURL = "tcp://localhost:61616";
    private static transient ConnectionFactory factory;
    private transient Connection connection;
    private transient Session session;
    
    public Consumer() throws JMSException {
        factory = new ActiveMQConnectionFactory(brokerURL);
        connection = factory.createConnection();
        connection.start();
        /*
            通知方式                    效果
        DUPS_OK_ACKNOWLEDGE         session延迟通知。如果JMS服务器宕机,会造成重复消息的情况。程序必须保证处理重复消息而不引起程序逻辑的混乱。
        AUTO_ACKNOWLEDGE             当receive或MessageListener方法成功返回后自动通知。
        CLIENT_ACKNOWLEDGE            客户端调用消息的acknowledge方法通知
        */
        session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
    }
    
    public void close() throws JMSException {
        if (connection != null) {
            connection.close();
        }
    }    
    
    public static void main(String[] args) throws JMSException {
        Consumer consumer = new Consumer();
        for (String stock : args) {
            Destination destination = consumer.getSession().createTopic("STOCKS." + stock);
            // A client uses a MessageConsumer object to receive messages from a destination
            MessageConsumer messageConsumer = consumer.getSession().createConsumer(destination);
            // 注册一个Message Listener,实现异步
            messageConsumer.setMessageListener(new Listener());
        }
    }
    
    public Session getSession() {
        return session;
    }

}

listener:

import java.text.DecimalFormat;
//A MessageListener object is used to receive asynchronously delivered messages
public class Listener implements MessageListener {

    // 处理message
    public void onMessage(Message message) {
        try {
            MapMessage map = (MapMessage)message;
            String stock = map.getString("stock");
            double price = map.getDouble("price");
            double offer = map.getDouble("offer");
            boolean up = map.getBoolean("up");
            DecimalFormat df = new DecimalFormat( "#,###,###,##0.00" );
            System.out.println(stock + "\t" + df.format(price) + "\t" + df.format(offer) + "\t" + (up?"up":"down"));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}

Publisher:

import java.util.Hashtable;

public class Publisher {
    
    protected int MAX_DELTA_PERCENT = 1;
    protected Map<String, Double> LAST_PRICES = new Hashtable<String, Double>();
    protected static int count = 10;
    protected static int total;
    
    protected static String brokerURL = "tcp://localhost:61616";
    protected static transient ConnectionFactory factory;
    protected transient Connection connection;
    protected transient Session session;
    protected transient MessageProducer producer;
    
    public Publisher() throws JMSException {
        factory = new ActiveMQConnectionFactory(brokerURL);
        connection = factory.createConnection();
        try {
        connection.start();
        } catch (JMSException jmse) {
            connection.close();
            throw jmse;
        }
        session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        producer = session.createProducer(null);
    }
    
    public void close() throws JMSException {
        if (connection != null) {
            connection.close();
        }
    }
    
    public static void main(String[] args) throws JMSException {
        Publisher publisher = new Publisher();
        while (total < 1000) {
            for (int i = 0; i < count; i++) {
                publisher.sendMessage(args);
            }
            total += count;
            System.out.println("Published '" + count + "' of '" + total + "' price messages");
            try {
              Thread.sleep(1000);
            } catch (InterruptedException x) {
            }
        }
        publisher.close();
    }

    protected void sendMessage(String[] stocks) throws JMSException {
        int idx = 0;
        while (true) {
            idx = (int)Math.round(stocks.length * Math.random());
            if (idx < stocks.length) {
                break;
            }
        }
        String stock = stocks[idx];
        // Topic和Queue都继承与Destination,也就这两种模式
        Destination destination = session.createTopic("STOCKS." + stock);
        Message message = createStockMessage(stock, session);
        System.out.println("Sending: " + ((ActiveMQMapMessage)message).getContentMap() + " on destination: " + destination);
        producer.send(destination, message);
    }

    protected Message createStockMessage(String stock, Session session) throws JMSException {
        Double value = LAST_PRICES.get(stock);
        if (value == null) {
            value = new Double(Math.random() * 100);
        }

        // lets mutate the value by some percentage
        double oldPrice = value.doubleValue();
        value = new Double(mutatePrice(oldPrice));
        LAST_PRICES.put(stock, value);
        double price = value.doubleValue();

        double offer = price * 1.001;

        boolean up = (price > oldPrice);

        MapMessage message = session.createMapMessage();
        message.setString("stock", stock);
        message.setDouble("price", price);
        message.setDouble("offer", offer);
        message.setBoolean("up", up);
        return message;
    }

    protected double mutatePrice(double price) {
        double percentChange = (2 * Math.random() * MAX_DELTA_PERCENT) - MAX_DELTA_PERCENT;

        return price * (100 + percentChange) / 100;
    }

}

 

 

--------------------------

让我们继续前行

 

 

分享到:
评论

相关推荐

    activemq安装包与demo

    包含activemq的安装包,JMS(java message service)的开发例子,java开发以及集成spring的开发,包含点对点与发布/订阅两种模式

    ActiveMQ从入门到精通(一)

    这是关于消息中间件ActiveMQ的一个系列专题文章,将涵盖JMS、ActiveMQ的初步入门及API详细使用、两种经典的消息模式(PTP andPub/Sub)、与Spring整合、ActiveMQ集群、监控与配置优化等。话不多说,我们来一起瞧一瞧...

    J2EE应用与BEA WebLogic Server 光盘part1

    全书共分为18章,每章分别对应于使用J2EE和WebLogic Server开发Web应用程序的各个阶段,并且都围绕特定的Java Enterprise技术来组织,包括模型-视图-控制器设计模式、JDBC数据库连接、远程方法调用、JMS、EJB、Java-...

    J2EE应用与BEA WebLogic Server 光盘part2

    全书共分为18章,每章分别对应于使用J2EE和WebLogic Server开发Web应用程序的各个阶段,并且都围绕特定的Java Enterprise技术来组织,包括模型-视图-控制器设计模式、JDBC数据库连接、远程方法调用、JMS、EJB、Java-...

    J2EE中文版指南 CHM格式 带全文检索

    两种访问方式的抉择 49 性能和访问方式 50 方法参数和访问方式 50 数据访问粒度 50 6,企业Bean的“内容” 51 7,企业Bean的命名约定 51 8,企业Bean的生存周期 52 有状态会话Bean的生命周期: 52 无状态会话Bean的...

    ofbiz综合技术文档

    一、soap应用的两种情况 91 1、ofbiz 将其他的webservice 封装成服务,这时ofbiz的应用系统可以直接调用该服务,以访问其他的Webservice: 92 2、其他的soap客户端可以通过soap访问ofbiz,这时ofbiz相当于Webservice...

    J2EE应用开发详解

    316 17.7 小结 318 第18章 Hibernate 319 18.1 Hibernate体系结构 319 18.2 Hibernate核心接口 321 18.3 一个简单的Hibernate例子 321 18.4 详解Hibernate配置文件 325 18.4.1 Hibernate的两种配置方式 325 18.4.2 ...

    springCloud

    消息代理(Message Broker)是一种消息验证、传输、路由的架构模式。消息代理是一个中间件产品,它的核心是一个消息的路由程序,用来实现接收和分发消息,并根据设定好的消息处理流来转发给正确的应用。它包括独立的...

    JAVA上百实例源码以及开源项目

    两个例子,无状态SessionBean可会话Bean必须实现SessionBean,获取系统属性,初始化JNDI,取得Home对象的引用,创建EJB对象,计算利息等;在有状态SessionBean中,用累加器,以对话状态存储起来,创建EJB对象,并将...

    JAVA上百实例源码以及开源项目源代码

    两个例子,无状态SessionBean可会话Bean必须实现SessionBean,获取系统属性,初始化JNDI,取得Home对象的引用,创建EJB对象,计算利息等;在有状态SessionBean中,用累加器,以对话状态存储起来,创建EJB对象,并将...

    JBoss Seam 工作原理、seam和hibernate的范例、RESTFul的seam、seam-gen起步、seam组件、配置组件、jsf,jboss、标签、PDF、注解等等

    1.2. 第一个例子:注册示例.............................................................................................................................................. 15 1.2.1. 了解代码.................

Global site tag (gtag.js) - Google Analytics