Java 使用Consumer接口实现分页功能

public class HandlePage {

    public static void main(String[] args) {
        /**
         * 传入总条数和每页页数计算每页起始条数和结束条数
         * TriConsumer接口是重写的,可以以此分页功能为参考,自己重写Consumer接口实现别的功能
         */
        handleAsPage(3000, 100, null, (data, start, end) -> {
            System.out.println("start = [" + start + "] - end = [" + end + "]");
            /**
             * 此处写业务逻辑处理 
             */
        });
    }

    /**
     * 分页
     * @param total 总条数
     * @param pageSize 每页展示条数
     * @param data 需处理数据
     * @param consumer
     */
    private static void handleAsPage(int total, int pageSize, Object data, TriConsumer<Object, Integer, Integer> consumer) {
        if (total < 0 || pageSize <0 || null == consumer) {
            throw new IllegalArgumentException("Parameter(totalCount, pageSize) should be equals or grant than 0, and Parameter(consumer) should not be null");
        }

        int loopCount = total / pageSize;
        int begin = 1;
        for (int i = 0; i < loopCount; i++) {
            int end = begin + pageSize - 1;
            consumer.accept(data, begin, end);
            begin = begin + pageSize;
        }

        if (total % pageSize != 0) {
            // 截止条数越界处理,截止条数(end)计算方法可能会出现比总条数多1的情况(若SQL不严谨,可能会导致将不符合条件的数据处理掉)
            int end = (begin + total % pageSize) > total ? total : begin + total % pageSize;
            consumer.accept(data, begin, end);
        }
    }
}

// 重写TriConsumer接口,这里懒得引用log4j-api.jar所以重写了,不想写可以添加jar包依赖后使用
public interface TriConsumer<K, V, S> {
    void accept(K var1, V var2, S var3);
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java 中,可以使用消息中间件来实现聊天功能。常用的消息中间件有 ActiveMQ、RabbitMQ、Kafka 等。 以下是一个简单的使用 ActiveMQ 实现聊天功能的示例代码: 1. 首先,需要创建一个 ActiveMQ 的连接工厂: ``` ConnectionFactory factory = new ActiveMQConnectionFactory("tcp://localhost:61616"); ``` 2. 然后,创建一个连接和会话: ``` Connection connection = factory.createConnection(); Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); ``` 3. 创建两个消息队列,分别代表发送消息的队列和接收消息的队列: ``` Destination sendQueue = session.createQueue("sendQueue"); Destination receiveQueue = session.createQueue("receiveQueue"); ``` 4. 创建一个消息生产者和一个消息消费者: ``` MessageProducer producer = session.createProducer(sendQueue); MessageConsumer consumer = session.createConsumer(receiveQueue); ``` 5. 在消息消费者中注册一个监听器用来接收消息: ``` consumer.setMessageListener(new MessageListener() { public void onMessage(Message message) { try { TextMessage textMessage = (TextMessage) message; String content = textMessage.getText(); System.out.println("Received message: " + content); } catch (JMSException e) { e.printStackTrace(); } } }); ``` 6. 在消息生产者中发送消息: ``` TextMessage message = session.createTextMessage(); message.setText("Hello, world!"); producer.send(message); ``` 通过以上步骤,就可以使用 ActiveMQ 实现简单的聊天功能了。当消息生产者发送一条消息时,消息消费者会马上接收到该消息并输出消息内容。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值