⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 roundtripserver.java

📁 一个简单实用的开源C++消息中间件SAFMQ - [软件开发] - [开源 消息中间件 SAFMQ ] 2006-11-23 在很多网络应用中
💻 JAVA
字号:
/*
 * Created on Jun 3, 2005
 */

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.URI;

import com.safmq.MQBuilder;
import com.safmq.MQException;
import com.safmq.MessageQueue;
import com.safmq.QueueMessage;
import com.safmq.Safmq;

/**
 * Note: this example expects a SAFMQ server to be running on the "localhost"
 * or same computer as the example is being run.  It also expects a user to exist
 * with the name "username" and password "password".  Additionally there should
 * be two queues "query" and "resposne", and the user "username" should have
 * read and write access to to this queue. 
 * 
 * @author matt
 */
public class RoundTripServer {
	static URI queue_name;
	static String user_name = "username";
	static String password = "password";
	
	static {
		try {
			queue_name = new URI("safmq://localhost/query");
		} catch (Exception e) {
		}
	}

	public static void main(String[] args) {
		try {
			// Allocate a connection to the query queue, we'll read from this queue
			MessageQueue 	mq = MQBuilder.buildMessageQueue(queue_name,user_name,password);
			// Allocate a messaeg so we can read from it.
			QueueMessage	msg = new QueueMessage();
			int error;
			
			// Try and retreive a message from the queue
			error = mq.Retreive(true,-1,msg);
			// Close the queue we don't need it any more, this release resources on the server
			mq.Close(); 
			if (error == Safmq.EC_NOERROR) {
				// Output the contents of the emssage
				System.out.println("Message Data Follows");
				System.out.println("Label: " + msg.getLabel());
				InputStream in = msg.getInputStream();
				byte		data[] = new byte[1024];
				int			read;
				while ( (read=in.read(data)) > 0) {
					System.out.write(data,0,read);
				}
				
				// allocate a response message				
				QueueMessage response = new QueueMessage();
				
				// Set the message's body type to give readers a clue to contents
				response.setBodyType(Safmq.BT_TEXT);
				// Set a name for the message, note this is optional, but some receivers
				// may choose an action depending on the name specified here
				response.setLabel("Round Trip Response");
				// Note the next line is the critical step, it ties the response message
				// with the original query message. 
				response.setReciptID(msg.getMessageID());
				
				// Get the output stream to fill the body.
				// In this case wrap it with a PrintWriter so that we can have formated output.
				PrintWriter pw = new PrintWriter(new OutputStreamWriter(response.getOutputStream()));
				pw.println("This is the response to the round trip client's query");
				pw.flush();
				
				// Connect to the response queue
				MessageQueue responseQueue = MQBuilder.buildMessageQueue(new URI(msg.getResponseQueueName()),user_name,password);
				error = responseQueue.Enqueue(response);
				responseQueue.Close();
				if (error != Safmq.EC_NOERROR)
					System.out.println("Error sending: "+Safmq.errorDecode(error));
			} else {
				System.out.println("Error retreiving: "+Safmq.errorDecode(error));	
			}
		} catch (MQException mqe) {
			mqe.printStackTrace();	
		} catch (IOException ioe) {
			ioe.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();	
		}	
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -