Search in sources :

Example 76 with Semaphore

use of com.swiftmq.tools.concurrent.Semaphore in project swiftmq-client by iitsoftware.

the class SessionDispatcher method close.

public void close() {
    if (pTracer.isEnabled())
        pTracer.trace(toString(), ", close ...");
    if (closeInProgress) {
        if (pTracer.isEnabled())
            pTracer.trace(toString(), ", close in progress, return");
        return;
    }
    closeLock.lock();
    closeInProgress = true;
    closeLock.unlock();
    Semaphore sem = new Semaphore();
    dispatch(new POSessionClose(sem));
    sem.waitHere();
    if (pTracer.isEnabled())
        pTracer.trace(toString(), ", close done");
}
Also used : Semaphore(com.swiftmq.tools.concurrent.Semaphore)

Example 77 with Semaphore

use of com.swiftmq.tools.concurrent.Semaphore in project swiftmq-client by iitsoftware.

the class DurableConsumer method unsubscribe.

/**
 * Unsubscribes the durable consumer and destroys the durable link.
 *
 * @throws AMQPException
 */
public void unsubscribe() throws AMQPException {
    if (!closed)
        close();
    Semaphore sem = new Semaphore();
    POAttachDurableConsumer po = new POAttachDurableConsumer(sem, name, source, linkCredit, qoS, false, null, TerminusExpiryPolicy.LINK_DETACH, deliveryMemory);
    mySession.getSessionDispatcher().dispatch(po);
    sem.waitHere();
    if (!po.isSuccess())
        throw new AMQPException(po.getException());
    DurableConsumer c = (DurableConsumer) po.getLink();
    c.close();
}
Also used : POAttachDurableConsumer(com.swiftmq.amqp.v100.client.po.POAttachDurableConsumer) Semaphore(com.swiftmq.tools.concurrent.Semaphore) POAttachDurableConsumer(com.swiftmq.amqp.v100.client.po.POAttachDurableConsumer)

Example 78 with Semaphore

use of com.swiftmq.tools.concurrent.Semaphore in project swiftmq-client by iitsoftware.

the class Producer method send.

/**
 * Send a message to the target. For transactional sends the method returns after settlement has been finished, otherwise when the message has been sent.
 *
 * @param msg        message
 * @param persistent whether the message should send/stored durable
 * @param priority   message priority (default is 5)
 * @param ttl        time to live (expiration) in milliseconds, default no expiration
 * @return delivery state of the message
 * @throws AMQPException on error
 */
public DeliveryStateIF send(AMQPMessage msg, boolean persistent, int priority, long ttl) throws AMQPException {
    verifyState();
    Header header = msg.getHeader();
    if (header == null) {
        header = new Header();
        msg.setHeader(header);
    }
    header.setDurable(new AMQPBoolean(persistent));
    header.setPriority(new AMQPUnsignedByte(priority));
    if (ttl >= 0)
        header.setTtl(new Milliseconds(ttl));
    Properties props = msg.getProperties();
    if (props == null) {
        props = new Properties();
        msg.setProperties(props);
    }
    if (props.getMessageId() == null)
        props.setMessageId(new MessageIdString(nextMsgId()));
    props.setTo(new AddressString(target));
    String userName = mySession.myConnection.getUserName();
    if (userName != null)
        props.setUserId(new AMQPBinary(userName.getBytes()));
    Semaphore sem = new Semaphore();
    try {
        POSendMessage po = new POSendMessage(sem, this, msg, msg.getTxnIdIF(), msg.getDeliveryTag());
        mySession.dispatch(po);
        sem.waitHere();
        if (!po.isSuccess())
            throw new AMQPException(po.getException());
        return po.getDeliveryState();
    } catch (Exception e) {
        e.printStackTrace();
        throw new AMQPException(e.toString());
    }
}
Also used : AddressString(com.swiftmq.amqp.v100.generated.messaging.message_format.AddressString) POSendMessage(com.swiftmq.amqp.v100.client.po.POSendMessage) MessageIdString(com.swiftmq.amqp.v100.generated.messaging.message_format.MessageIdString) MessageIdString(com.swiftmq.amqp.v100.generated.messaging.message_format.MessageIdString) AddressString(com.swiftmq.amqp.v100.generated.messaging.message_format.AddressString) Semaphore(com.swiftmq.tools.concurrent.Semaphore) Properties(com.swiftmq.amqp.v100.generated.messaging.message_format.Properties) Milliseconds(com.swiftmq.amqp.v100.generated.transport.definitions.Milliseconds) IOException(java.io.IOException) Header(com.swiftmq.amqp.v100.generated.messaging.message_format.Header)

Example 79 with Semaphore

use of com.swiftmq.tools.concurrent.Semaphore in project swiftmq-client by iitsoftware.

the class Session method createConsumer.

/**
 * Creates a message consumer on a source.
 *
 * @param source         the source, e.g. queue name
 * @param linkCredit     link credit
 * @param qoS            quality of service
 * @param noLocal        if true means it won't receive messages sent on the same topic and connection
 * @param selector       message selector (for SwiftMQ this would be a JMS message selector string)
 * @param deliveryMemory delivery memory for recovery
 * @return message consumer
 * @throws AMQPException on error
 */
public Consumer createConsumer(String source, int linkCredit, int qoS, boolean noLocal, String selector, DeliveryMemory deliveryMemory) throws AMQPException {
    verifyState();
    QoS.verify(qoS);
    Semaphore sem = new Semaphore();
    POAttachConsumer po = new POAttachConsumer(sem, source, linkCredit, qoS, noLocal, selector, deliveryMemory == null ? new DefaultDeliveryMemory() : deliveryMemory);
    sessionDispatcher.dispatch(po);
    sem.waitHere();
    if (!po.isSuccess())
        throw new AMQPException(po.getException());
    Consumer c = (Consumer) po.getLink();
    links.add(c);
    return c;
}
Also used : Semaphore(com.swiftmq.tools.concurrent.Semaphore)

Example 80 with Semaphore

use of com.swiftmq.tools.concurrent.Semaphore in project swiftmq-client by iitsoftware.

the class Session method finishHandshake.

protected void finishHandshake() throws SessionHandshakeException {
    Semaphore sem = new Semaphore();
    POObject po = new POBegin(sem);
    sessionDispatcher.dispatch(po);
    sem.waitHere();
    sem.reset();
    if (!po.isSuccess()) {
        cancel();
        throw new SessionHandshakeException(po.getException());
    }
}
Also used : POObject(com.swiftmq.tools.pipeline.POObject) Semaphore(com.swiftmq.tools.concurrent.Semaphore)

Aggregations

Semaphore (com.swiftmq.tools.concurrent.Semaphore)149 MsgNoVerifier (jms.base.MsgNoVerifier)18 IOException (java.io.IOException)8 SwiftletException (com.swiftmq.swiftlet.SwiftletException)6 ArrayList (java.util.ArrayList)6 CommitLogRecord (com.swiftmq.impl.store.standard.log.CommitLogRecord)5 DataStreamOutputStream (com.swiftmq.tools.util.DataStreamOutputStream)5 List (java.util.List)5 StartBackup (com.swiftmq.impl.store.standard.backup.po.StartBackup)2 StartShrink (com.swiftmq.impl.store.standard.cache.po.StartShrink)2 UnknownHostException (java.net.UnknownHostException)2 POAttachDurableConsumer (com.swiftmq.amqp.v100.client.po.POAttachDurableConsumer)1 POAuthenticate (com.swiftmq.amqp.v100.client.po.POAuthenticate)1 POCloseLink (com.swiftmq.amqp.v100.client.po.POCloseLink)1 POOpen (com.swiftmq.amqp.v100.client.po.POOpen)1 POProtocolRequest (com.swiftmq.amqp.v100.client.po.POProtocolRequest)1 POSendClose (com.swiftmq.amqp.v100.client.po.POSendClose)1 POSendMessage (com.swiftmq.amqp.v100.client.po.POSendMessage)1 AddressString (com.swiftmq.amqp.v100.generated.messaging.message_format.AddressString)1 Header (com.swiftmq.amqp.v100.generated.messaging.message_format.Header)1