JMS收发

dd

public class JMSConsumer implements MessageListener{
	private ConnectionFactory connectionFactory;

	private boolean transacted = false;

	public JMSConsumer(ConnectionFactory connectionFactory)
			throws JMSException, IOException {
		this.connectionFactory = connectionFactory;
	}

	public void consumeQueueMessage(String queueName,
			MessageListener messageListener) throws JMSException {
		Connection connection = null;
		Session session = null;

		connection = this.connectionFactory.createConnection();
		session = connection
				.createSession(transacted, Session.AUTO_ACKNOWLEDGE);

		Destination destination = session.createQueue(queueName);
		MessageConsumer consumer = session.createConsumer(destination);

		connection.start();
		System.out.println("Consumer:->Begin listening...");
		/**
		 * 如果想主动的去接受消息,而不用消息监听的话,把consumer.setMessageListener(this) 改为Message
		 * message = consumer.receive(), 手动去调用MessageConsumer的receive方法即可.
		 */
		// 开始监听
		consumer.setMessageListener(messageListener); // 异步监听

	}
	
	public void consumeTopicMessage(String topicName,
			MessageListener messageListener) throws JMSException {
		Connection connection = null;
		Session session = null;

		connection = this.connectionFactory.createConnection();
		session = connection
				.createSession(transacted, Session.AUTO_ACKNOWLEDGE);

		Destination destination = session.createTopic(topicName);		
		MessageConsumer consumer = session.createConsumer(destination);

		connection.start();
		System.out.println("Consumer:->Begin listening...");
		/**
		 * 如果想主动的去接受消息,而不用消息监听的话,把consumer.setMessageListener(this) 改为Message
		 * message = consumer.receive(), 手动去调用MessageConsumer的receive方法即可.
		 */
		// 开始监听
		consumer.setMessageListener(messageListener); // 异步监听

	}

	public void onMessage(Message message) {
		// TODO Auto-generated method stub
		System.out.println("message:" + message.toString());
	}

 

public class JMSProducer {
	private ConnectionFactory connectionFactory;
	private boolean transacted = false;

	public JMSProducer(ConnectionFactory connectionFactory)
			throws JMSException, IOException {
		this.connectionFactory = connectionFactory;
	}

	/**
	 * 发送基于队列消息
	 * 
	 * @param message
	 * @param queueName
	 * @throws JMSException
	 * @throws JMSException
	 * @throws Exception
	 */
	public void produceQueueMessage(String message, String queueName)
			throws JMSException {
		Connection connection = null;
		Session session = null;

		try {
			connection = this.connectionFactory.createConnection();
			session = connection.createSession(transacted,
					Session.AUTO_ACKNOWLEDGE);

			Destination destination = session.createQueue(queueName);
			MessageProducer producer = session.createProducer(destination);

			connection.start();
			System.out.println("produceQueueMessage message = " + message);
			Message msg = session.createTextMessage(message);			

			producer.send(msg);
			producer.close();
			System.out.println("Producer:->Message sent complete!");
		} finally {
			if (session != null)
				session.close();
			if (connection != null)
				connection.close();
		}

	}

	public void produceQueueMessage(File file, String queueName)
			throws JMSException {
		Connection connection = null;
		Session session = null;

		try {
			connection = this.connectionFactory.createConnection();
			session = connection.createSession(transacted,
					Session.AUTO_ACKNOWLEDGE);

			Destination destination = session.createQueue(queueName);
			MessageProducer producer = session.createProducer(destination);

			System.out.println("produceQueueMessage file Message = "
					+ file.getName());
			byte[] bytes = this.getFileByte(file);
			

			connection.start();
			BytesMessage msg = session.createBytesMessage();
			msg.writeBytes(bytes);

			producer.send(msg);
			producer.close();
			System.out.println("Producer:->Message sent complete!");		
		} finally {
			if (session != null)
				session.close();
			if (connection != null)
				connection.close();
		}

	}

	/**
	 * 发送基于主题消息
	 * 
	 * @param message
	 * @param subject
	 * @throws JMSException *
	 * @throws Exception
	 */
	public void produceTopicMessage(String message, String topicName)
			throws JMSException {
		Connection connection = null;
		Session session = null;

		try {
			connection = this.connectionFactory.createConnection();
			session = connection.createSession(transacted,
					Session.AUTO_ACKNOWLEDGE);
			Destination destination = session.createTopic(topicName);

			MessageProducer producer = session.createProducer(destination);

			connection.setClientID("12345");
			connection.start();
			System.out.println("produceTopicMessage message = " + message);
			Message msg = session.createTextMessage(message);			

			producer.send(msg);
			producer.close();

			System.out.println("Producer:->Message sent complete!");
		} finally {
			if (session != null)
				session.close();
			if (connection != null)
				connection.close();
		}
	}

	public void produceTopicMessage(File file, String topicName)
			throws JMSException {
		Connection connection = null;
		Session session = null;

		try {
			connection = this.connectionFactory.createConnection();
			session = connection.createSession(transacted,
					Session.AUTO_ACKNOWLEDGE);

			Destination destination = session.createTopic(topicName);

			MessageProducer producer = session.createProducer(destination);

			connection.setClientID("12345");
			connection.start();

			System.out.println("produceTopicMessage file Message = "
					+ file.getName());
			byte[] bytes = this.getFileByte(file);
				
			connection.start();
			BytesMessage msg = session.createBytesMessage();

			msg.writeBytes(bytes);
			
			producer.send(msg);
			producer.close();
			System.out.println("Producer:->Message sent complete!");
		}finally {
			if (session != null)
				session.close();
			if (connection != null)
				connection.close();
		}

	}
	
	private byte[] getFileByte(File file){
        byte[] buffer = null;
        FileInputStream fin = null;
        try {            
            fin = new FileInputStream(file); 
            buffer = new byte[fin.available()];
            fin.read(buffer);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                fin.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return buffer;
    }

 

class QueueMessageListener implements MessageListener {

		public void onMessage(Message reciveMessage) {
			// TODO Auto-generated method stub
			try {
				if (reciveMessage instanceof TextMessage) {
					TextMessage new_name = (TextMessage) reciveMessage;

					String text = new_name.getText();
					System.out
							.println("QueueMessageListener onMessage:" + text);
					System.out.println("");
					assertEquals(text, message);

				} else if (reciveMessage instanceof BytesMessage) {
					BytesMessage bytesMessage = (BytesMessage) reciveMessage;
					FileOutputStream fileOutputStream;
					File file = new File(recieveFileDir + sendFileName);
					fileOutputStream = new FileOutputStream(file);					

					byte[] byteContent = readBytesContent(bytesMessage);
					fileOutputStream.write(byteContent);
					
					System.out.println("接收到消息 length:"
							+ byteContent.length);
					System.out.println();
					
					fileOutputStream.close();
				}
			} catch (JMSException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (FileNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
 

 

 

你可能感兴趣的:(jms)