org.apache.qpid.proton.engine.Session Java Examples

The following examples show how to use org.apache.qpid.proton.engine.Session. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: TransportImplTest.java    From qpid-proton-j with Apache License 2.0 6 votes vote down vote up
@Test
public void testOpenSessionBeforeOpenConnection()
{
    MockTransportImpl transport = new MockTransportImpl();
    Connection connection = Proton.connection();
    transport.bind(connection);

    Session session = connection.session();
    session.open();

    pumpMockTransport(transport);

    assertEquals("Unexpected frames written: " + getFrameTypesWritten(transport), 0, transport.writes.size());

    connection.open();

    pumpMockTransport(transport);

    assertEquals("Unexpected frames written: " + getFrameTypesWritten(transport), 2, transport.writes.size());

    assertTrue("Unexpected frame type", transport.writes.get(0) instanceof Open);
    assertTrue("Unexpected frame type", transport.writes.get(1) instanceof Begin);
}
 
Example #2
Source File: EventImplTest.java    From qpid-proton-j with Apache License 2.0 6 votes vote down vote up
@Test
public void testGetTransportWithDeliveryContext()
{
    Transport transport = Transport.Factory.create();
    Connection connection = Connection.Factory.create();
    transport.bind(connection);

    Session session = connection.session();
    Sender sender = session.sender("mySender");

    Delivery delivery = sender.delivery("tag".getBytes());

    EventImpl event = createEvent(delivery, Event.Type.DELIVERY);

    assertNotNull("No transport returned", event.getTransport());
    assertSame("Incorrect transport returned", transport, event.getTransport());
}
 
Example #3
Source File: Pool.java    From qpid-proton-j with Apache License 2.0 6 votes vote down vote up
private <T extends Link> T resolve(String remote, String local,
                                   LinkResolver<T> resolver,
                                   LinkConstructor<T> constructor) {
    String host = remote.substring(2).split("/", 2)[0];
    T link = resolver.resolve(remote);
    if (link == null) {
        Connection conn = connections.get(host);
        if (conn == null) {
            conn = Connection.Factory.create();
            conn.collect(collector);
            conn.setHostname(host);
            conn.open();
            connections.put(host, conn);
        }

        Session ssn = conn.session();
        ssn.open();

        link = constructor.create(ssn, remote, local);
        link.open();
    }
    return link;
}
 
Example #4
Source File: EventImpl.java    From qpid-proton-j with Apache License 2.0 6 votes vote down vote up
@Override
public Reactor getReactor() {
    if (context instanceof Reactor) {
        return (Reactor) context;
    } else if (context instanceof Task) {
        return ((Task)context).getReactor();
    } else if (context instanceof Transport) {
        return ((TransportImpl)context).getReactor();
    } else if (context instanceof Delivery) {
        return ((Delivery)context).getLink().getSession().getConnection().getReactor();
    } else if (context instanceof Link) {
        return ((Link)context).getSession().getConnection().getReactor();
    } else if (context instanceof Session) {
        return ((Session)context).getConnection().getReactor();
    } else if (context instanceof Connection) {
        return ((Connection)context).getReactor();
    } else if (context instanceof Selectable) {
        return ((Selectable)context).getReactor();
    }
    return null;
}
 
Example #5
Source File: EventImpl.java    From qpid-proton-j with Apache License 2.0 6 votes vote down vote up
@Override
public Transport getTransport()
{
    if (context instanceof Transport) {
        return (Transport) context;
    } else if (context instanceof Connection) {
        return ((Connection)context).getTransport();
    } else {
        Session session = getSession();
        if (session == null) {
            return null;
        }

        Connection connection = session.getConnection();
        if (connection == null) {
            return null;
        }

        return connection.getTransport();
    }
}
 
Example #6
Source File: AmqpConnection.java    From activemq-artemis with Apache License 2.0 6 votes vote down vote up
/**
 * Creates a new Session instance used to create AMQP resources like
 * senders and receivers.
 *
 * @return a new AmqpSession that can be used to create links.
 * @throws Exception if an error occurs during creation.
 */
public AmqpSession createSession() throws Exception {
   checkClosed();

   final AmqpSession session = new AmqpSession(AmqpConnection.this, getNextSessionId());
   final ClientFuture request = new ClientFuture();

   serializer.execute(new Runnable() {

      @Override
      public void run() {
         checkClosed();
         Session protonSession = getEndpoint().session();
         protonSession.setIncomingCapacity(sessionIncomingCapacity);
         session.setEndpoint(protonSession);
         session.setStateInspector(getStateInspector());
         session.open(request);
         pumpToProtonTransport(request);
      }
   });

   request.sync();

   return session;
}
 
Example #7
Source File: Send.java    From qpid-proton-j with Apache License 2.0 6 votes vote down vote up
@Override
public void onConnectionInit(Event event) {
    Connection conn = event.getConnection();

    // Every session or link could have their own handler(s) if we
    // wanted simply by adding the handler to the given session
    // or link
    Session ssn = conn.session();

    // If a link doesn't have an event handler, the events go to
    // its parent session. If the session doesn't have a handler
    // the events go to its parent connection. If the connection
    // doesn't have a handler, the events go to the reactor.
    Sender snd = ssn.sender("sender");
    conn.open();
    ssn.open();
    snd.open();
}
 
Example #8
Source File: EventImpl.java    From qpid-proton-j with Apache License 2.0 6 votes vote down vote up
@Override
public Connection getConnection()
{
    if (context instanceof Connection) {
        return (Connection) context;
    } else if (context instanceof Transport) {
        Transport transport = getTransport();
        if (transport == null) {
            return null;
        }
        return ((TransportImpl) transport).getConnectionImpl();
    } else {
        Session ssn = getSession();
        if (ssn == null) {
            return null;
        }
        return ssn.getConnection();
    }
}
 
Example #9
Source File: NettySimpleAmqpServer.java    From qpid-jms with Apache License 2.0 5 votes vote down vote up
private void processSessionClose(Session session) {
    ProtonSession protonSession = (ProtonSession) session.getContext();

    sessions.remove(protonSession.getId());

    session.close();
    session.free();
}
 
Example #10
Source File: Handshaker.java    From qpid-proton-j with Apache License 2.0 5 votes vote down vote up
@Override
public void onSessionRemoteClose(Event evt) {
    Session ssn = evt.getSession();
    if (ssn.getLocalState() != EndpointState.CLOSED) {
        ssn.close();
    }
}
 
Example #11
Source File: Handshaker.java    From qpid-proton-j with Apache License 2.0 5 votes vote down vote up
@Override
public void onSessionRemoteOpen(Event evt) {
    Session ssn = evt.getSession();
    if (ssn.getLocalState() == EndpointState.UNINITIALIZED) {
        ssn.open();
    }
}
 
Example #12
Source File: Pool.java    From qpid-proton-j with Apache License 2.0 5 votes vote down vote up
public Sender newOutgoing(Session ssn, String remote, String local) {
    Sender snd = ssn.sender(String.format("%s-%s", local, remote));
    Source src = new Source();
    src.setAddress(local);
    snd.setSource(src);
    Target tgt = new Target();
    tgt.setAddress(remote);
    snd.setTarget(tgt);
    return snd;
}
 
Example #13
Source File: Pool.java    From qpid-proton-j with Apache License 2.0 5 votes vote down vote up
public Receiver newIncoming(Session ssn, String remote, String local) {
    Receiver rcv = ssn.receiver(String.format("%s-%s", remote, local));
    Source src = new Source();
    src.setAddress(remote);
    rcv.setSource(src);
    Target tgt = new Target();
    tgt.setAddress(remote);
    rcv.setTarget(tgt);
    return rcv;
}
 
Example #14
Source File: EventImplTest.java    From qpid-proton-j with Apache License 2.0 5 votes vote down vote up
@Test
public void testGetTransportWithSessionContext()
{
    Transport transport = Transport.Factory.create();
    Connection connection = Connection.Factory.create();
    transport.bind(connection);

    Session session = connection.session();

    EventImpl event = createEvent(session, Event.Type.SESSION_INIT);

    assertNotNull("No transport returned", event.getTransport());
    assertSame("Incorrect transport returned", transport, event.getTransport());
}
 
Example #15
Source File: ConnectionImpl.java    From qpid-proton-j with Apache License 2.0 5 votes vote down vote up
@Override
public Session sessionHead(final EnumSet<EndpointState> local, final EnumSet<EndpointState> remote)
{
    if(_sessionHead == null)
    {
        return null;
    }
    else
    {
        LinkNode.Query<SessionImpl> query = new EndpointImplQuery<SessionImpl>(local, remote);
        LinkNode<SessionImpl> node = query.matches(_sessionHead) ? _sessionHead : _sessionHead.next(query);
        return node == null ? null : node.getValue();
    }
}
 
Example #16
Source File: ConnectionImpl.java    From qpid-proton-j with Apache License 2.0 5 votes vote down vote up
@Override
void doFree() {
    List<SessionImpl> sessions = new ArrayList<SessionImpl>(_sessions);
    for(Session session : sessions) {
        session.free();
    }
    _sessions = null;
}
 
Example #17
Source File: EventImpl.java    From qpid-proton-j with Apache License 2.0 5 votes vote down vote up
@Override
public Session getSession()
{
    if (context instanceof Session) {
        return (Session) context;
    } else {
        Link link = getLink();
        if (link == null) {
            return null;
        }
        return link.getSession();
    }
}
 
Example #18
Source File: TransportImplTest.java    From qpid-proton-j with Apache License 2.0 5 votes vote down vote up
void doOpenLinkBeforeOpenConnectionTestImpl(boolean receiverLink)
{
    MockTransportImpl transport = new MockTransportImpl();
    Connection connection = Proton.connection();
    transport.bind(connection);

    Session session = connection.session();
    session.open();

    Link link = null;
    if(receiverLink)
    {
        link = session.receiver("myReceiver");
    }
    else
    {
        link = session.sender("mySender");
    }
    link.open();

    pumpMockTransport(transport);

    assertEquals("Unexpected frames written: " + getFrameTypesWritten(transport), 0, transport.writes.size());

    // Now open the connection, expect the Open, Begin, and Attach frames
    connection.open();

    pumpMockTransport(transport);

    assertEquals("Unexpected frames written: " + getFrameTypesWritten(transport), 3, transport.writes.size());

    assertTrue("Unexpected frame type", transport.writes.get(0) instanceof Open);
    assertTrue("Unexpected frame type", transport.writes.get(1) instanceof Begin);
    assertTrue("Unexpected frame type", transport.writes.get(2) instanceof Attach);
}
 
Example #19
Source File: SessionImpl.java    From qpid-proton-j with Apache License 2.0 5 votes vote down vote up
@Override
public Session next(EnumSet<EndpointState> local, EnumSet<EndpointState> remote)
{
    LinkNode.Query<SessionImpl> query = new EndpointImplQuery<SessionImpl>(local, remote);

    LinkNode<SessionImpl> sessionNode = _node.next(query);

    return sessionNode == null ? null : sessionNode.getValue();
}
 
Example #20
Source File: TransportImplTest.java    From qpid-proton-j with Apache License 2.0 5 votes vote down vote up
@Test
public void testReceiverFlowBeforeOpenConnection()
{
    MockTransportImpl transport = new MockTransportImpl();
    Connection connection = Proton.connection();
    transport.bind(connection);

    Session session = connection.session();
    session.open();

    Receiver reciever = session.receiver("myReceiver");
    reciever.flow(5);

    pumpMockTransport(transport);

    assertEquals("Unexpected frames written: " + getFrameTypesWritten(transport), 0, transport.writes.size());

    // Now open the connection, expect the Open and Begin frames but
    // nothing else as we haven't opened the receiver itself yet.
    connection.open();

    pumpMockTransport(transport);

    assertEquals("Unexpected frames written: " + getFrameTypesWritten(transport), 2, transport.writes.size());

    assertTrue("Unexpected frame type", transport.writes.get(0) instanceof Open);
    assertTrue("Unexpected frame type", transport.writes.get(1) instanceof Begin);
}
 
Example #21
Source File: AmqpSession.java    From qpid-jms with Apache License 2.0 5 votes vote down vote up
public AmqpSession(AmqpConnection connection, JmsSessionInfo info, Session session) {
    super(info, session, connection);

    this.connection = connection;

    if (info.isTransacted()) {
        txContext = new AmqpTransactionContext(this, info);
    } else {
        txContext = null;
    }
}
 
Example #22
Source File: TransportImplTest.java    From qpid-proton-j with Apache License 2.0 5 votes vote down vote up
/**
 * Verify that no Begin frame is emitted by the Transport should a Session open
 * after the Close frame was sent.
 */
@Test
public void testSessionBeginAfterCloseSent()
{
    MockTransportImpl transport = new MockTransportImpl();
    Connection connection = Proton.connection();
    transport.bind(connection);

    connection.open();

    Session session = connection.session();

    pumpMockTransport(transport);

    assertEquals("Unexpected frames written: " + getFrameTypesWritten(transport), 1, transport.writes.size());

    assertTrue("Unexpected frame type", transport.writes.get(0) instanceof Open);

    // Send the necessary response to Open
    transport.handleFrame(new TransportFrame(0, new Open(), null));

    assertEquals("Unexpected frames written: " + getFrameTypesWritten(transport), 1, transport.writes.size());

    // Cause a Close frame to be sent
    connection.close();
    pumpMockTransport(transport);

    assertEquals("Unexpected frames written: " + getFrameTypesWritten(transport), 2, transport.writes.size());
    assertTrue("Unexpected frame type", transport.writes.get(1) instanceof Close);

    // Open the session and verify the transport doesn't
    // send any Begin frame, as a Close frame was sent already.
    session.open();
    pumpMockTransport(transport);

    assertEquals("Unexpected frames written: " + getFrameTypesWritten(transport), 2, transport.writes.size());
}
 
Example #23
Source File: AMQPConnectionContext.java    From activemq-artemis with Apache License 2.0 5 votes vote down vote up
@Override
public void onRemoteClose(Session session) throws Exception {
   handler.runLater(() -> {
      session.close();
      session.free();

      AMQPSessionContext sessionContext = (AMQPSessionContext) session.getContext();
      if (sessionContext != null) {
         sessionContext.close();
         sessions.remove(session);
         session.setContext(null);
      }
   });
}
 
Example #24
Source File: AMQPConnectionContext.java    From activemq-artemis with Apache License 2.0 5 votes vote down vote up
protected AMQPSessionContext getSessionExtension(Session realSession) throws ActiveMQAMQPException {
   AMQPSessionContext sessionExtension = sessions.get(realSession);
   if (sessionExtension == null) {
      // how this is possible? Log a warn here
      sessionExtension = newSessionExtension(realSession);
      realSession.setContext(sessionExtension);
      sessions.put(realSession, sessionExtension);
   }
   return sessionExtension;
}
 
Example #25
Source File: ReactorTest.java    From qpid-proton-j with Apache License 2.0 5 votes vote down vote up
@Override
public void onConnectionInit(Event event) {
    Connection conn = event.getConnection();
    Session ssn = conn.session();
    Sender snd = ssn.sender("sender");
    conn.open();
    ssn.open();
    snd.open();
}
 
Example #26
Source File: ConnectionTest.java    From qpid-proton-j with Apache License 2.0 5 votes vote down vote up
private void assertConnectionIsUsable()
{
    Session clientSesion = _clientConnection.session();
    clientSesion.open();
    _pumper.pumpAll();

    Session serverSession = _serverConnection.sessionHead(of(UNINITIALIZED), of(ACTIVE));
    serverSession.open();
    _pumper.pumpAll();

    assertEnpointState(clientSesion, ACTIVE, ACTIVE);
    assertEnpointState(serverSession, ACTIVE, ACTIVE);
}
 
Example #27
Source File: EventImplTest.java    From qpid-proton-j with Apache License 2.0 5 votes vote down vote up
@Test
public void testGetTransportWithLinkContext()
{
    Transport transport = Transport.Factory.create();
    Connection connection = Connection.Factory.create();
    transport.bind(connection);

    Session session = connection.session();
    Link link = session.receiver("myReceiver");

    EventImpl event = createEvent(link, Event.Type.LINK_INIT);

    assertNotNull("No transport returned", event.getTransport());
    assertSame("Incorrect transport returned", transport, event.getTransport());
}
 
Example #28
Source File: AmqpSessionBuilder.java    From qpid-jms with Apache License 2.0 5 votes vote down vote up
@Override
protected Session createEndpoint(JmsSessionInfo resourceInfo) {
    long outgoingWindow = getParent().getProvider().getSessionOutgoingWindow();

    Session session = getParent().getEndpoint().session();
    session.setIncomingCapacity(Integer.MAX_VALUE);
    if (outgoingWindow >= 0) {
        session.setOutgoingWindow(outgoingWindow);
    }

    return session;
}
 
Example #29
Source File: ProtonContainer.java    From qpid-proton-j with Apache License 2.0 4 votes vote down vote up
public void setSession(Session session) {
    this.session = session;
}
 
Example #30
Source File: NettySimpleAmqpServer.java    From qpid-jms with Apache License 2.0 4 votes vote down vote up
public Session getSession() {
    return session;
}