4. TIBCO EMS -simple sample

安装:

我的网盘里有6.0.1版本,安装之

启动EMS Server

Windows下:所有程序> TIBCO > TIBCO EMS 6.0 > Start EMS Server

或者运行tibemsd.bat

image

Start the EMS Administration Tool:

Windows下:所有程序> TIBCO > TIBCO EMS 6.0 > Start EMS Administration Tool

命令:

> connect

> show users

> create user user1

> create user user2

> create queue myQueue

> delete queue myQueue

> show queues

image

Java代码

1. study.cloudboy.tibcoEMS.sample1.Producer

package study.cloudboy.tibcoEMS.sample1;

import javax.jms.Connection;

import javax.jms.ConnectionFactory;

import javax.jms.Destination;

import javax.jms.JMSException;

import javax.jms.MessageProducer;

import javax.jms.Session;

import javax.jms.TextMessage;

public class Producer {

private String serverUrl = null;

private String userName = null;

private String password = null;

private String name = null;

public Producer() {

userName = "user1";

name = "myQueue";

    }

public void sendMessage(String[] messages) throws JMSException {

        ConnectionFactory factory = new com.tibco.tibjms.TibjmsConnectionFactory(

serverUrl);

        Connection connection = factory.createConnection(userName, password);

        Session session = connection.createSession(false,javax.jms.Session.AUTO_ACKNOWLEDGE);

        Destination destination = null;

        destination = session.createQueue(name);

        MessageProducer msgProducer = session.createProducer(null);

        TextMessage msg;

int i;

for (i = 0; i < messages.length; i++) {

            msg = session.createTextMessage();

            msg.setText(messages[i]);

            msgProducer.send(destination, msg);

            System.out.println("Published message: " + messages[i]);

        }

        connection.close();

    }

public static void main(String[] args) {

try {

            Producer producer = new Producer();

            String[] messages = {"Hello", "user2"};

             producer.sendMessage(messages);

        } catch(Exception e) {

            e.printStackTrace();

        }

    }

}

2. study.cloudboy.tibcoEMS.sample1.Consumer

package study.cloudboy.tibcoEMS.sample1;

import javax.jms.Connection;

import javax.jms.ConnectionFactory;

import javax.jms.Destination;

import javax.jms.ExceptionListener;

import javax.jms.JMSException;

import javax.jms.Message;

import javax.jms.MessageConsumer;

import javax.jms.Session;

import com.tibco.tibjms.Tibjms;

public class Consumer implements ExceptionListener {

private String serverUrl = null;

private String userName = null;

private String password = null;

private String name = null;

private int ackMode = Session.AUTO_ACKNOWLEDGE;

public Consumer() {

userName = "user2";

name = "myQueue";

    }

void run() throws JMSException {

        Message msg = null;

        System.out.println("Subscribing to destination: " + name + "\n");

        ConnectionFactory factory = new com.tibco.tibjms.TibjmsConnectionFactory(

serverUrl);

        Connection connection = factory.createConnection(userName, password);

        Session session = connection.createSession(false, ackMode);

        connection.setExceptionListener(this);

        Destination destination = null;

        destination = session.createQueue(name);

        MessageConsumer msgConsumer = session.createConsumer(destination);

        connection.start();

while (true) {

            msg = msgConsumer.receive();

if (msg == null) {

break;

            }

if (ackMode == Session.CLIENT_ACKNOWLEDGE

                    || ackMode == Tibjms.EXPLICIT_CLIENT_ACKNOWLEDGE

                    || ackMode == Tibjms.EXPLICIT_CLIENT_DUPS_OK_ACKNOWLEDGE) {

                msg.acknowledge();

            }

            System.out.println("Received message: " + msg);

        }

        connection.close();

    }

public void onException(JMSException e) {

        System.out.println("CONNECTION EXCEPTION: " + e.getMessage());

    }

public static void main(String[] args) {

try {

            Consumer comsuer = new Consumer();

            comsuer.run();

        } catch(Exception e) {

            e.printStackTrace();

        }

    }

}

你可能感兴趣的:(windows,style,simple,target,blank)