刚转到java,问一个c->java的问题(2)

bnwxf 2003-10-22 07:53:55
接上一个问题。
socket通信的东西在thinking in java里面找到了。可其它进程间通信的东西这本书里没有。比如我要用消息队列传递数据,在java里面该怎么做。
...全文
32 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
bnwxf 2003-10-23
  • 打赏
  • 举报
回复
多谢兄台,这个实现可以在两个进程之间传递数据么?
etre 2003-10-23
  • 打赏
  • 举报
回复
可是自己写实现msqQueue

public class msqQueue
{
/** Table for storing the message queues */
Hashtable ivQueueTable = new Hashtable();

/**
* Insert the method's description here.
* Creation date: (2000/06/13 11:20:17 AM)
* @return java.lang.String
*/
public synchronized HashMap getAllQueueLen()
{
HashMap lvQueueLenMap = new HashMap();

Iterator lvIter = ivQueueTable.keySet().iterator();

while (lvIter.hasNext())
{
String lvProcessorName = (String)lvIter.next();
try
{
int lvLen = getQueueLength(lvProcessorName);
lvQueueLenMap.put(lvProcessorName, new Integer(lvLen));
}
catch (Exception lvException)
{}
}

return lvQueueLenMap;
}

/**
* Gets the number of priority queue
* @param pQueueName is the name of the message queue
* @return Returns the number of priority queue
*/
public int getMaximumPriorityQueueSize(String pQueueName) throws Exception
{
msqQueueElement lvQueue = (msqQueueElement)ivQueueTable.get(pQueueName);
// Check whether the request queue is registered or not
// if the queue is not registered, exception will be thrown
if (lvQueue == null)
throw new errQueueNotFoundException(pQueueName);

return lvQueue.getMaximumPriorityQueueSize();
}

/**
* Gets message from the message queue
* @param pQueueName is the name of the message queue
* @return Returns the message extracted from the message queue
*/
public Object getMessage(String pQueueName) throws Exception
{
msqQueueElement lvQueue = (msqQueueElement)ivQueueTable.get(pQueueName);
// Check whether the request queue is registered or not
// if the queue is not registered, exception will be thrown
if (lvQueue == null)
throw new errQueueNotFoundException(pQueueName);

return lvQueue.get();
}

/**
* Gets the current queue length of the message queue
* @param pQueueName is the name of the message queue
* @return Returns the total number of message queued in the message queues
*/
public int getQueueLength(String pQueueName) throws errQueueNotFoundException
{
msqQueueElement lvQueue = (msqQueueElement)ivQueueTable.get(pQueueName);
// Check whether the request queue is registered or not
// if the queue is not registered, exception will be thrown
if (lvQueue == null)
throw new errQueueNotFoundException(pQueueName);

return lvQueue.getQueueLength();
}
/**
* Posts the message to the destination queue
* @param pQueueName is the name of the message queue
* @param pMessage is the message that needs to be queued
*/
public void postMessage(String pQueueName, Object pMessage) throws errQueueNotFoundException
{
msqQueueElement lvQueueElement = (msqQueueElement)ivQueueTable.get(pQueueName);
if (lvQueueElement != null)
lvQueueElement.put(pMessage);
else
throw new errQueueNotFoundException(pQueueName);
}
/**
* Posts the message to the destination queue with priority
* @param pQueueName is the name of the message queue
* @param pMessage is the message that needs to be queued
* @param pPriority is the priority of the queue
*/
public void postMessage(String pQueueName, Object pMessage, int pPriority) throws errQueueNotFoundException
{
msqQueueElement lvQueueElement = (msqQueueElement)ivQueueTable.get(pQueueName);
if (lvQueueElement != null)
lvQueueElement.put(pMessage, pPriority);
else
throw new errQueueNotFoundException(pQueueName);
}
/**
* Insert the method's description here.
* Creation date: (2000/06/13 11:20:17 AM)
* @return java.lang.String
*/
public void printAllQueueLen()
{
String lvResult = "";
TreeMap lvQueueLenMap = new TreeMap(getAllQueueLen());

Iterator lvIter = lvQueueLenMap.keySet().iterator();
while (lvIter.hasNext())
{
String lvProcessorName = (String)lvIter.next();

Integer lvLen = (Integer) lvQueueLenMap.get(lvProcessorName);

lvResult = lvResult + lvProcessorName + " = " + lvLen + "\r\n";
}

System.out.println(lvResult);
}
/**
* Creates a message queue with default number of priority and registers it
* @param pQueueName is the name of the message queue
*/
public synchronized void registerQueue(String pQueueName) throws errQueueSizeException, errQueueAlreadyRegisteredException
{
if (ivQueueTable.containsKey(pQueueName))
throw new errQueueAlreadyRegisteredException(pQueueName);
msqQueueElement lvQueueElement = new msqQueueElement();
ivQueueTable.put(pQueueName, lvQueueElement);
}
/**
* Creates a message queue with user defined number of priority and registers it
* @param pQueueName is the name of the message queue
* @param pPriorityQueueSize is the size of the priority queue
*/
public synchronized void registerQueue(String pQueueName, int pPriorityQueueSize) throws errQueueSizeException
{
msqQueueElement lvQueueElement = new msqQueueElement(pPriorityQueueSize);
ivQueueTable.put(pQueueName, lvQueueElement);
}
/**
* Destroys the specified message queue
* @param pQueueName is the name of the message queue
*/
public synchronized void unregisterQueue(String pQueueName)
{
if (ivQueueTable.containsKey(pQueueName))
ivQueueTable.remove(pQueueName);
}
}
bnwxf 2003-10-23
  • 打赏
  • 举报
回复
难道没有基于操作系统的消息队列的实现方式么,我看了jms的文档,这是基于中间件的。
Crystal_arrow 2003-10-22
  • 打赏
  • 举报
回复
kankan
jigsaw 2003-10-22
  • 打赏
  • 举报
回复
Sun有一个免费的MQ可以拿来用
上古玉清 2003-10-22
  • 打赏
  • 举报
回复
我也不太清楚
廖雪峰 2003-10-22
  • 打赏
  • 举报
回复
using JMS: Java Message Service
yangFrame 2003-10-22
  • 打赏
  • 举报
回复
但是java也有队列
bnwxf 2003-10-22
  • 打赏
  • 举报
回复
连message queue都不支持,那在linux下移植起c写的程序不是很麻烦。
littlecpu 2003-10-22
  • 打赏
  • 举报
回复
通用的做法是用socket联系起在本地的两个应用。
bnwxf 2003-10-22
  • 打赏
  • 举报
回复
up

62,616

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧